'Empty field causes error while saving page

As I am new to JavaScript and Dynamic365, I need some suggestion how can I resolve error. I have the following code which shows some warning message for some specific analysis code:

var ShowWarning = new Object();

ShowWarning.AnalysisCode = {
    onSave: (executionContext) => {
        var analysisCode = Xrm.Page.getAttribute("rf_analysisCode").getValue()[0].keyValues;
        if (analysisCode) {
            if (typeof (analysisCode) == "string") {
                var rfCode = (JSON.parse(analysisCode).rf_code).value;
            } else {
                var rfCode = analysisCode.rf_code.value;
            }
            var analysisType = Xrm.Page.getAttribute("rf_analysistype").getValue();
            var matchingTransaction = Xrm.Page.getAttribute("rf_matchingtransaction").getValue();
            //specific analysis codes for which warning shows
            let analysisCodeDefined = ["A", "B", "C", "D", "E",];
            for (let i = 0; i < analysisCodeDefined.length; i++) {
                if (rfCode == analysisCodeDefined[i]) {
                    if (analysisType != null && matchingTransaction == null) {
                         alert("Some warning message");
                         break;
                     }
                     else {
                         break;
                     }
                 }
                 else {
                     continue;
                 }
             }
         }
     }
 };

So if the analysis code field in Xrm page is empty, then when try to save, it gives the following error:

TypeError: Cannot read properties of null (reading '0') at Object.onSave (http://CRMUrl/ShowWarning?ver=-202203:6:76)

I am not sure how can I resolve this error. Because the page is supposed to save even when it does not have value in analysis code field.



Solution 1:[1]

Error is in this line:

var analysisCode = Xrm.Page.getAttribute("rf_analysisCode").getValue()[0].keyValues;

When anlysis code field is empty, then

Xrm.Page.getAttribute("rf_analysisCode").getValue(); 

returns null but code use this value as an Array. So just need to insure field has value.

Solution for this is check the value before use it:

var ShowWarning = new Object();

ShowWarning.AnalysisCode = {
    onSave: (executionContext) => {
        //Check field value before use value as an Array
        if(Xrm.Page.getAttribute("rf_analysisCode").getValue()){
            var analysisCode = Xrm.Page.getAttribute("rf_analysisCode").getValue()[0].keyValues;
            if (analysisCode) {
                if (typeof (analysisCode) == "string") {
                    var rfCode = (JSON.parse(analysisCode).rf_code).value;
                } else {
                    var rfCode = analysisCode.rf_code.value;
                }
                var analysisType = Xrm.Page.getAttribute("rf_analysistype").getValue();
                var matchingTransaction = Xrm.Page.getAttribute("rf_matchingtransaction").getValue();
                //specific analysis codes for which warning shows
                let analysisCodeDefined = ["A", "B", "C", "D", "E",];
                for (let i = 0; i < analysisCodeDefined.length; i++) {
                    if (rfCode == analysisCodeDefined[i]) {
                        if (analysisType != null && matchingTransaction == null) {
                             alert("Some warning message");
                             break;
                         }
                         else {
                             break;
                         }
                     }
                     else {
                         continue;
                     }
                 }
             }
        }
        
     }
 };

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 juagicre