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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Cannot read property 0 or null javascript error.

(0) ShareShare
ReportReport
Posted on by

hi,

i get lookup filed value and set in single line of text field through java script in on save event.

when there is no value in lookup field on save its throwing error like " can not ready property "0" or null.

Please help.

below screen shots of code and error for reference.

3414.Capture-1.PNG4571.Capture2.PNG

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Try to replace lookupfield.getValue with lookupfield.getValue()

  • thonda Profile Picture
    Microsoft Employee on at

    if(lookupfield.getValue() != null && lookupfield.getValue()[0] != null){

    var lookupname = lookupfield.getValue()[0].name;

    }

    The above should solve the issue, in your logic if(lookupfield.getValue != null) it only checks if array object exists, but fails to check if any elements exists in array before you call name property on 0th element.

    Regards,

    Praveen Thonda

    D365 Support Engineer

    Microsoft Corporation

  • Suggested answer
    LeoAlt Profile Picture
    16,331 Moderator on at

    Hi partner,

    You should replace the "getValue" with "getValue()" in the condition.

    "can not read property 0" means "lookupfield.getValue()" is null, which means your lookup field may not contians value.

    About how to use js to work with lookup field, please refer to the following code.

        // GET and SET Lookup / Customer field value
        var functionName = function (executionContext) {

            // Access the field on the form
            var field = executionContext.getFormContext().getAttribute("parentaccountid");

            // Verify it does exist on the form
            if (field != null) {

                // Get its field value; Returns the Lookup object
                var value = field.getValue();
                //Make sure the lookup field contains data
                if(value!=null){
                // To get the Id, Name and Entity Name (account/contact)
                var reocord_id = field.getValue()[0].id;
                var record_name = field.getValue()[0].name;
                var record_entityName = field.getValue()[0].entityType;
                }
                

                // Set its field value
                field.setValue([{
                    id: reocord_id,
                    name: record_name,
                    entityType: record_entityName
                }]);
            }
        }

    Best Regards,

    Leo

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 184 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 125

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans