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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Model driven apps - we...
Power Apps
Unanswered

Model driven apps - web resource JS - cannot read property 0 of null - when lookup field value is cleared

(1) ShareShare
ReportReport
Posted on by 69

Hi,

 

I am receiving error as "cannot read property 0 of null - when lookup field value is cleared" while editing and adding record in  form. Kindly help in resolving the issue.

I searched in many forums but it didn't help in resolving the issue. 

 

Name 1 - text field

Name2 - lookup field.

 

The if function works properly. When lookup field is cleared, it throws error as "cannot read property 0 of null "

 

My code is below:

 

//name validation
function namevalidation (executionContext) {
 
var formContext = executionContext.getFormContext();
var BPN = formContext.getAttribute("name1").getValue();
var PBPNname = formContext.getAttribute("name2").getValue()[0].name;
 
if (!PBPNname) {
return;
}
 
if (BPN == PBPNname)
{
alert ("Hi");
alert(BPN);
alert(PBPNname);
Xrm.Navigation.openAlertDialog({ text: "Name1 cannot be same as Name2" });
Xrm.Page.getAttribute("name2").setValue(null);
 
}
 
}
Categories:
I have the same question (0)
  • poweractivate Profile Picture
    11,078 Most Valuable Professional on at

    @VeeraVaishnavi 

     

    1) On which specific line does it say it throws the error (to check this, press F12 on keyboard then reproduce the error)?

     

    2) In case, what is a screenshot of your form where it is happening (if you are allowed to give it).

     

     

  • VeeraVaishnavi Profile Picture
    69 on at

    The error thrown is as follows:

    One of the scripts for this record has caused an error. For more details, download the log file.
    Cannot read property '0' of nullSession Id: c9106d20-41db-4879-923e-158f1fe8f696Correlation Id: dc1c11a1-3481-4837-98f5-a46bb93649cbEvent Name: onchangeWeb Resource Name: XXXXXXX  Solution Name: ActivePublisher Name: DefaultPublishercorizance2Time: Sun Nov 29 2020 13:34:04 GMT+0530 (India Standard Time)Download Log File
    Cannot read property '0' of null
    Session Id: c9106d20-41db-4879-923e-158f1fe8f696
    Correlation Id: dc1c11a1-3481-4837-98f5-a46bb93649cbEvent Name: onchangeWeb
    Resource Name: XXXXXXXXX
    Solution Name: ActivePublisher Name: DefaultPublishercorizance2Time: Sun Nov 29 2020 13:34:04 GMT+0530 (India Standard Time)
     
    I have added this JS for onchange event handling for the lookup field name2.
     
    While entering lookup field - "name2" (optional field), I need to validate it against name1 (required field) whether they are equal.
     
    If equal - throw error.
    If not equal - no action required.
    If I clear the lookup field - the script throws error.
  • poweractivate Profile Picture
    11,078 Most Valuable Professional on at

    @VeeraVaishnavi 

     

    When you download the log file what does it say in there? Or if allowed to, can you attach the log file here in next reply or paste its contents in next reply?

  • VeeraVaishnavi Profile Picture
    69 on at
  • Verified answer
    VeeraVaishnavi Profile Picture
    69 on at

    Hi, Senior helped in resolving the issue. Posting the code. "Cannot read 0 property of null" is not occurring and the functionality works as expected. Thanks.

     

    // name validation

    function namevalidation (executionContext) {

    var formContext = executionContext.getFormContext(); 

    var BPN = formContext.getAttribute("name1").getValue();

    var PBPNnameObj = formContext.getAttribute("name2").getValue();

    if(PBPNnameObj != null){

    var PBPNname = formContext.getAttribute("name2").getValue()[0].name;

    //alert(PBPNname);

    if (!PBPNname) {

            return;

        }

    if (!BPN) {

            return;

    }

     if  (BPN == PBPNname)

    {

    alert ("Hi");

    alert(BPN);

    alert(PBPNname);

    Xrm.Navigation.openAlertDialog({ text: " Name1 cannot be same as Name2" });

    Xrm.Page.getAttribute("name2").setValue(null);  

    }

    }

    }

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 717 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 329 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard