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 :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by 26

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

I have the same question (0)
  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    Seems you are missing publisher name in the name field. What is your entity publisher? try to add prefix with name field like below. Hope this helps.

    nhs_name

     

     

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    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.

  • USA80 Profile Picture
    26 on at

    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
    Arpit Shrivastava Profile Picture
    7,518 User Group Leader on at

    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

     

  • Verified answer
    Rawish Kumar Profile Picture
    13,758 on at

    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(" ");

  • USA80 Profile Picture
    26 on at

    Where does the alert go?

  • Arpit Shrivastava Profile Picture
    7,518 User Group Leader on at

    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);
    }

    }

  • EmployeeOcta Profile Picture
    6 on at

    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.

  • USA80 Profile Picture
    26 on at

    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?

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    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;)

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans