Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Sales forum / Cannot read properties...
Sales forum
Answered

Cannot read properties of null (reading '0')

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);
}
Categories:
  • OmPrakash Profile Picture
    OmPrakash 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
    Nishkarsh Vaish 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.

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,339 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,177 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans