Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

CRM 2016 TypeError: Cannot read property '0' of null when the lookup field

Posted on by 1,530

Hi,

I am doing a validation using javascript to check base on a lookup field condition.  If the condition fails I need to reset the lookup field back to null. 

I have reviewed other examples but I have not been able to fix the error message.  The field is getting set back to null.  However the following error message continues to display "Cannot read property '0' of null when the lookup field".

I changed over to "Legacy Form Rending" and the error message went away.  I do not want to use Legacy Form Rending even though that fixed my problem.

Here's by following code.

Xrm.Page.getAttribute("new_calibrationid").setRequiredLevel("none");
if (Xrm.Page.getAttribute("new_calibrationid").getValue() != null) {
   Xrm.Page.getAttribute("new_calibrationid").setValue(null);
}

Xrm.Page.getAttribute("new_calibrationid").setRequiredLevel("required");

Any ideal on how to fix this error message?
5123.ErrorMessage2.JPG

*This post is locked for comments

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: CRM 2016 TypeError: Cannot read property '0' of null when the lookup field

    Hi rthompson,

    On this line you get the value from "new_calibrationid", but this will be null if "new_calibrationid" does not have a value. You need to check that "new_calibrationid" is not null before getting the value.

    var calibrationId = Xrm.Page.getAttribute("new_calibrationid").getValue();

    The below line will error if calibrationId does not have a value.

    var ifilter = "&$filter=(new_CalibrationId/Id eq guid'" + FormatGuid(calibrationId[0].id) + "'";

    You might also find it easier to retrieve the guid like this.

    var calibrationId = Xrm.Page.getAttribute("new_calibrationid").getValue();
    var guid = calibrationId[0].id.substring(1, 37);
  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: CRM 2016 TypeError: Cannot read property '0' of null when the lookup field

    Hi Guido,

    I am getting the correct result.  The guid has a value and the lookup is getting set.  But the error message does not go away.

    So what am I missing.  I do understand what you are saying about checking if value is null.

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: CRM 2016 TypeError: Cannot read property '0' of null when the lookup field

    Hi rthompson,

    There is a problem with the CalibrationReadSDK function. I recommend debugging the JavaScript to find the exact problem, but from observation the below code is causing the issue.

        var ifilter = "&$filter=(new_CalibrationId/Id eq guid'" + FormatGuid(calibrationId[0].id) + "'";
        ifilter += " and new_InstrumentStatusCode/Value eq 100000004";   //Only select Out For Calibration
        ifilter += ")";
  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: CRM 2016 TypeError: Cannot read property '0' of null when the lookup field

    this line

    var ifilter = "&$filter=(new_CalibrationId/Id eq guid'" + FormatGuid(calibrationId[0].id) + "'";

    you don't check if calibrationId is not null before, if the lookup is null you get the error

    you should do also the same check for result[0] in case you don't get results from your query

  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: CRM 2016 TypeError: Cannot read property '0' of null when the lookup field
    Here's the code:

    //Go get the most current calibration function CalibrationReadSDK() { var caseId = Xrm.Page.getAttribute("new_caseid").getValue(); var caseSelectorId = Xrm.Page.getAttribute("new_caseselectorid").getValue(); var calibrationId = Xrm.Page.getAttribute("new_calibrationid").getValue(); var iselect = "?$select=new_CalibrationId, new_name, new_LastCalibrationOn, new_NextDueOn, new_InstrumentStatusCode, statuscode"; iselect += ",new_new_calibration_new_calibrationdetail_CalibrationId"; iselect += "&$expand=new_new_calibration_new_calibrationdetail_CalibrationId&$top=1" var ifilter = "&$filter=(new_CalibrationId/Id eq guid'" + FormatGuid(calibrationId[0].id) + "'"; ifilter += " and new_InstrumentStatusCode/Value eq 100000004"; //Only select Out For Calibration ifilter += ")"; var iorderby = "&$orderby=CreatedOn desc, new_LastCalibrationOn desc"; var options = iselect + ifilter + iorderby; SDK.REST.retrieveMultipleRecords("new_calibrationdetail", options, caseCalibrationLoadCalibration, function (error) { alert(error.message + "[" + options); }, caseCalibrationLoadCompleted); } function caseCalibrationLoadCalibration(result) { if (result[0].new_name != null) { var statusMessage = checkCalibrationStatus(result); if (statusMessage != null) { alert(statusMessage); //Xrm.Page.data.entity.attributes.get("new_calibrationid").setValue(""); //var frmType = Xrm.Page.ui.getFormType(); //if (frmType == 1) // Xrm.Page.getControl('new_calibrationid').getAttribute().setValue(); Xrm.Page.getAttribute("new_calibrationid").setRequiredLevel("none"); if (Xrm.Page.getAttribute("new_calibrationid").getValue() != null) { Xrm.Page.getAttribute("new_calibrationid").setValue(null); } Xrm.Page.getAttribute("new_calibrationid").setRequiredLevel("required"); } else { var lastCalibrationOn = result[0].new_LastCalibrationOn; var nextDueOn = result[0].new_NextDueOn; var makeLookup = result[0].new_new_calibration_new_calibrationdetail_CalibrationId.new_MakeId; var modelLookup = result[0].new_new_calibration_new_calibrationdetail_CalibrationId.new_ModelId; Xrm.Page.getAttribute("new_lastcalibrationon").setRequiredLevel("none"); Xrm.Page.getAttribute("new_lastcalibrationon").setValue(lastCalibrationOn); Xrm.Page.getAttribute("new_lastcalibrationon").setRequiredLevel("required"); Xrm.Page.getAttribute("new_nextdueon").setRequiredLevel("none"); Xrm.Page.getAttribute("new_nextdueon").setValue(nextDueOn); Xrm.Page.getAttribute("new_nextdueon").setRequiredLevel("required"); Xrm.Page.getAttribute("new_makeid").setRequiredLevel("none"); var fieldName = "new_makeid"; var fieldId = makeLookup.Id; var valueName = makeLookup.Name; var entityType = makeLookup.LogicalName; SetLookupValue(fieldName, fieldId, valueName, entityType); Xrm.Page.getAttribute("new_makeid").setRequiredLevel("required"); Xrm.Page.getAttribute("new_modelid").setRequiredLevel("none"); var fieldName = "new_modelid"; var fieldId = modelLookup.Id; var valueName = modelLookup.Name; var entityType = modelLookup.LogicalName; SetLookupValue(fieldName, fieldId, valueName, entityType); Xrm.Page.getAttribute("new_modelid").setRequiredLevel("required"); } } else { alert("No Data"); } } function checkCalibrationStatus(result) { var changeInstrumentStatus = result[0].new_InstrumentStatusCode.Value; var statusCode = result[0].new_new_calibration_new_calibrationdetail_CalibrationId.statuscode.Value.toString(); var statusMessage = null; switch (statusCode) { case "100000004": // Out for calibration statusMessage = "This instrument is out for calibration."; break; case "100000003": // Out for repair statusMessage = "This instrument is out for repair."; break; case "100000002": // Removed from service statusMessage = "This instrument is out of service."; break; } return statusMessage; } function caseCalibrationLoadCompleted() { }
  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: CRM 2016 TypeError: Cannot read property '0' of null when the lookup field
    I need to post the code with full editor
  • Guido Preite Profile Picture
    Guido Preite 54,081 Super User 2024 Season 1 on at
    RE: CRM 2016 TypeError: Cannot read property '0' of null when the lookup field

    the code you posted is not the one giving you the error, please post all the code of that function

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans