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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Cannot read properties of null (reading '0')

(1) ShareShare
ReportReport
Posted on by 12
I have added this piece of code to populate a single line of text field with the name from a related lookup. It works fine when you put a value in the lookup but if you clear the value from the look up it throws this error: Cannot read properties of null (reading '0'). I know it is throwing that because the field is null. What do I need to do to this code to make it not execute if the lookup is null?
 
function planName(executionContext) {
    var household = formContext.getAttribute(/si_household/).getValue()[0].id;
    var householdEntityType = formContext.getAttribute(/si_household/).getValue()[0].entityType;
    var householdName = formContext.getAttribute(/si_household/).getValue()[0].name;
    var hhLookup = new Array();
    hhLookup[0] = new Object();
    hhLookup[0].id = household;
    hhLookup[0].entityType = householdEntityType;
    hhLookup[0].name = householdName;
    formContext.getAttribute(/ah_planname/).setValue(hhLookup[0].name);
}
I have the same question (0)
  • OmPrakash Profile Picture
    4 on at
    Cannot read properties of null (reading '0')
    Add a condtion before getting the id, entity etc..,
     
    if(formContext.getAttribute(/si_household/).getValue() != null  && 
      formContext.getAttribute(/si_household/).getValue() != undefined)
    {
    //your code
    }
  • Verified answer
    Nishkarsh Vaish Profile Picture
    335 on at
    Cannot read properties of null (reading '0')
    Hi 
     
    Change your function as below.
     
    function planName(executionContext) {
        var houseHoldLookup = formContext.getAttribute(/si_household/).getValue();
        if(houseHoldLookup != null) 
        {
            var household = houseHoldLookup[0].id;
            var householdEntityType = houseHoldLookup[0].entityType;
            var householdName = houseHoldLookup[0].name;
            var hhLookup = new Array();
            hhLookup[0] = new Object();
            hhLookup[0].id = household;
            hhLookup[0].entityType = householdEntityType;
            hhLookup[0].name = householdName;
            formContext.getAttribute(/ah_planname/).setValue(hhLookup[0].name);
        }
    }
     
    Please mark as verified, if it resolves your problem.

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 258

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 178

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 104 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans