web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by 1,532

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

I have the same question (0)
  • Guido Preite Profile Picture
    54,086 Moderator on at

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

  • rthompson Profile Picture
    1,532 on at
    I need to post the code with full editor
  • rthompson Profile Picture
    1,532 on at
    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() { }
  • Verified answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    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

  • Suggested answer
    Adrian Begovich Profile Picture
    1,027 Moderator on at

    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 += ")";
  • rthompson Profile Picture
    1,532 on at

    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
    1,027 Moderator on at

    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);

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans