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,...
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)
  • Verified answer
    Nishkarsh Vaish Profile Picture
    335 on at
    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.
  • OmPrakash Profile Picture
    4 on at
    Add a condtion before getting the id, entity etc..,
     
    if(formContext.getAttribute(/si_household/).getValue() != null  && 
      formContext.getAttribute(/si_household/).getValue() != undefined)
    {
    //your code
    }

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 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans