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 :
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
    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

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans