Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

"Unable to get property 'setValue' of undefined or null reference"

(0) ShareShare
ReportReport
Posted on by 2

I have the following script that keeps error out when it trys to set the name field to null.  The error I get is "Unable to get property 'setValue' of undefined or null reference"  I have made sure that the field is spelled correctly and exists on the form.  When stepping through the logic in debugger, it does pick up the text field of Name just fine.  Issue is when I try to set it to null that it doesn't like it.  Please help, thank you.

function onSaveSetName ()
{
 var name = Xrm.Page.getAttribute("nhs_description").getValue();
 var measurement = Xrm.Page.getAttribute("nhs_measurement").getText(); 
 var separator = ' - ';
    var contractliinetitle = Xrm.Page.getAttribute("nhs_contractdetailtype").getValue();
 var contractdetailtype = Xrm.Page.getAttribute("nhs_resultscontractdetailtype").getSelectedOption().text;
 var month = Xrm.Page.getAttribute("nhs_resultseffectivemonth").getSelectedOption().text;
 var quarter = Xrm.Page.getAttribute("nhs_resulteffectivequarter").getSelectedOption().text;
 var year = Xrm.Page.getAttribute("nhs_resulteffectiveyear").getSelectedOption().text;
 
 
    var dateFieldValue = new Date(enddate);          
    var year = dateFieldValue.getFullYear() + "";
    var month = (dateFieldValue.getMonth() + 1) + "";
    var day = dateFieldValue.getDate() + "";
    var enddate = month + "/" + day + "/" + year;
 
 /*Contract Detail Type: Guarantee = 127130000
   Measurement: Monthly = 127130000, Quarterly = 100000000, Annually = 100000001, By Audit Request Only = 100000002*/
  
 
 if (contractdetailtype == "Guarantee" && measurement == "Monthly" && contractliinetitle !=null)
 {
  Xrm.Page.getAttribute("name").setValue(null);
  Xrm.Page.getAttribute("name").setValue(contractliinetitle + separator + month + separator + quarter + year);
                Xrm.Page.ui.controls.get("name").setDisabled (true);
 }

else if (measurement != "Guarantee")
 {
  Xrm.Page.ui.controls.get("nhs_description").setDisabled (false);
 }

}

*This post is locked for comments

  • USA80 Profile Picture
    USA80 2 on at
    RE: "Unable to get property 'setValue' of undefined or null reference"

    It was a field that was looking up to another field on a previous entity, so thank you, this worked now.  Thank you all for your help, much appreciated.

  • Verified answer
    gdas Profile Picture
    gdas 50,089 Moderator on at
    RE: "Unable to get property 'setValue' of undefined or null reference"

    Hi,

    What is the field type of nhs_contractdetailtype attribute? Seems this is lookup that's why.

    You can retrieve like below.

    var contractdetailtype = Xrm.Page.getAttribute('nhs_contractdetailtype').getValue()[0].name;

    (you can also use "id":  Xrm.Page.getAttribute('<FIELD_NAME>').getValue()[0].id;)

  • USA80 Profile Picture
    USA80 2 on at
    RE: "Unable to get property 'setValue' of undefined or null reference"

    I don't get the error anymore ,but having another issue:

    My contractlinetitle variable is a text field but when I do the debugger the result that it shows is "[[object Object]]" and not the actually what is in the field currently.  

    Any ideas as to why?

  • EmployeeOcta Profile Picture
    EmployeeOcta 2 on at
    RE: "Unable to get property 'setValue' of undefined or null reference"

    One more point guys,

    in place of using alert method you can use

    console.log('Your Expected text / variable name').

    this will not pop up alert message rather the log will get generated within f12 debugger toll.

    check by using this wherever you feel the error comes in.

  • Arpit Shrivastava Profile Picture
    Arpit Shrivastava 7,518 User Group Leader on at
    RE: "Unable to get property 'setValue' of undefined or null reference"

    As highlighted below:

    function onSaveSetName ()
    {
    var name = Xrm.Page.getAttribute("nhs_description").getValue();
    var measurement = Xrm.Page.getAttribute("nhs_measurement").getText();
    var separator = ' - ';
    var contractliinetitle = Xrm.Page.getAttribute("nhs_contractdetailtype").getValue();
    var contractdetailtype = Xrm.Page.getAttribute("nhs_resultscontractdetailtype").getSelectedOption().text;
    var month = Xrm.Page.getAttribute("nhs_resultseffectivemonth").getSelectedOption().text;
    var quarter = Xrm.Page.getAttribute("nhs_resulteffectivequarter").getSelectedOption().text;
    var year = Xrm.Page.getAttribute("nhs_resulteffectiveyear").getSelectedOption().text;


    var dateFieldValue = new Date(enddate);
    var year = dateFieldValue.getFullYear() + "";
    var month = (dateFieldValue.getMonth() + 1) + "";
    var day = dateFieldValue.getDate() + "";
    var enddate = month + "/" + day + "/" + year;

    /*Contract Detail Type: Guarantee = 127130000
    Measurement: Monthly = 127130000, Quarterly = 100000000, Annually = 100000001, By Audit Request Only = 100000002*/


    if (contractdetailtype == "Guarantee" && measurement == "Monthly" && contractliinetitle !=null)
    {

    alert(Xrm.Page.getAttribute("name"));


    Xrm.Page.getAttribute("name").setValue(null);
    Xrm.Page.getAttribute("name").setValue(contractliinetitle + separator + month + separator + quarter + year);
    Xrm.Page.ui.controls.get("name").setDisabled (true);
    }

    else if (measurement != "Guarantee")
    {
    Xrm.Page.ui.controls.get("nhs_description").setDisabled (false);
    }

    }

  • USA80 Profile Picture
    USA80 2 on at
    RE: "Unable to get property 'setValue' of undefined or null reference"

    Where does the alert go?

  • Verified answer
    Rawish Kumar Profile Picture
    Rawish Kumar 13,756 on at
    RE: "Unable to get property 'setValue' of undefined or null reference"

    few things :

    1. make sure your name and description field and others are on the form

    2. if its a custom field as highlighted by Goutam - it has to be like nhs_name

    3. try using Xrm.Page.data.entity.attributes.get("nhs_name or name").setValue(null);

    4. or simply say Xrm.Page.getAttribute("nhs_name or name").setValue(" ");

  • Verified answer
    Arpit Shrivastava Profile Picture
    Arpit Shrivastava 7,518 User Group Leader on at
    RE: "Unable to get property 'setValue' of undefined or null reference"

    Add a alert to check whether you can find field  using below script.

    alert( Xrm.Page.getAttribute("name"));

    If you see null as alert that means field does not exist in the form. You can not set value to the field that does not exists in the form. 

    An attribute/field needs to be present on a form to be reachable by Xrm.Page.getAttribute(attributeName).

    However, it can be hidden, or placed in a hidden section or tab.


    If found useful, please mark the answer as verified.


    Cheers
    Arpit
    https://arpitmscrmhunt.blogspot.com

     

  • USA80 Profile Picture
    USA80 2 on at
    RE: "Unable to get property 'setValue' of undefined or null reference"

    So it should be like this?

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

    Xrm.Page.getAttribute("nhs_description").setValue(contractliinetitle + separator + month + separator + quarter + year);

                   Xrm.Page.ui.controls.get("nhs_description").setDisabled (true);

  • Verified answer
    gdas Profile Picture
    gdas 50,089 Moderator on at
    RE: "Unable to get property 'setValue' of undefined or null reference"

    Hi,

    There are no field name without publisher in Dynamics CRM for custom entity, so you should add publisher name before "name". Replace with below lines .

    Xrm.Page.getAttribute("nhs_name").setValue(null);
    Xrm.Page.getAttribute("nhs_name").setValue(contractliinetitle + separator + month + separator + quarter + year);
    Xrm.Page.ui.controls.get("nhs_name").setDisabled (true);

    Hope this helps.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

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

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans