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

Announcements

News and Announcements icon
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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 119 Super User 2026 Season 2

#2
Syed Aqib Raza Profile Picture

Syed Aqib Raza 72

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 66 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans