Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Setting a field value issue

(0) ShareShare
ReportReport
Posted on by 7,316

HI,

I am facing a very odd issue. After upgrading to CRM 2016(onprem) from 2015 one of the scripts stopped working or I should say working intermittently.

I've debug the script line by line and everything seems to flow completely fine. The last line of script is setting the value of a field and it does show the variable that holds the field value has the correct value in it and yet the field remains blank.

var countryNumber1 = countryNumber(countryId);
    if (countryNumber1 != null) {
        Xrm.Page.getAttribute("new_countrynumber").setValue(countryNumber1); // values exists in countryNumber1 and yet field remains blank on the form
    }

Sometimes it populates the field values and sometimes not. I wasn't sure how else to troubleshoot hence asking for some guidance.

Thanks for any help.

*This post is locked for comments

  • meenoo Profile Picture
    meenoo 7,316 on at
    RE: Setting a field value issue

    Yeah, I am using synchronous. I did not try timeout yet but meanwhile I tried placing another condition after setting the value to see if the value exists in the field or not and if not then set the value again. Like this:

       var countryNumber1 = FillCountryNumber(countryId);

       if (countryNumber1 != null) {

    alert(countryNumber1 );

           Xrm.Page.getAttribute("new_countrynumber").setValue(countryNumber1);

    var checkCountryNum= Xrm.Page.getAttribute("new_countrynumber").getValue();

    if(checkCountryNum==null){

    Xrm.Page.getAttribute("new_countrynumber").setValue(countryNumber1);

    }

       }

    What happens is, checkCountryNum does have the value in it and if fails the if condition and comes out. On the form, the field remains blank. Again, this happens intermittently.. field remains blank when +New button is clicked on the form. From other places value gets set in the field.

    I am not sure what else to try!

    Thanks.

  • Aric Levin Profile Picture
    Aric Levin 30,188 Moderator on at
    RE: Setting a field value issue

    Hi meenoo,

    The last part of the question is irrelevant based on the fact the alert is getting it every time.

    Can you try and put a short timeout before you call the setValue?

    I am wondering if this has anything to do with waiting for webapi to complete, but I doubt it since it seems like you are running the webapi synchronously.

  • meenoo Profile Picture
    meenoo 7,316 on at
    RE: Setting a field value issue

    Hi Aric,

    I've tried placing an alert before setting the value. It does alert the countrynumber every time but it fails to display the number in the field sometimes.

    Data type of country id is the id(_new_country_value) of the country lookup field.

    Since, I am getting the countrynumber in the alert box for sure, does it make any difference whether it is primary key or unique identified?

    Thank you.

  • Aric Levin Profile Picture
    Aric Levin 30,188 Moderator on at
    RE: Setting a field value issue

    A couple of things.

    In your comment on the set value line you are saying the value exists in the countryNumber1 field and remains blank on the form:

    Can you try to put an alert(countryNumber1); before the following line?

    Xrm.Page.getAttribute("new_countrynumber").setValue(countryNumber1);

    This will show your when it works and when it doesn't (inside the isnull).

    Additional questions:

    What is the data type of country id? Is it the primary key of the new_countries entity, a unique identifier or other data type.

    If primary key, then you should replace your query so that you pass the guid as follows: new_countries(guid).

    If unique identifier, see Francesco's response, or if other data type your code should be fine.

    Hope this helps.

  • meenoo Profile Picture
    meenoo 7,316 on at
    RE: Setting a field value issue

    Thanks Shashank. There are no business rules on that field and this is the only script associated to that field.

  • meenoo Profile Picture
    meenoo 7,316 on at
    RE: Setting a field value issue

    Thanks Manoj,

    I did debug and watched the value coming in until the step where it sets the value to the field. Everything looks correct but the value won't set. I've noticed this has been happening for the '+ New' button on the form. All the other buttons like save & new, + buttons on the dashboard and view are setting the value correctly to the field on form load. Any other suggestions would be great.

  • meenoo Profile Picture
    meenoo 7,316 on at
    RE: Setting a field value issue

    Thanks Francesco,

    I did go through all those guid normalizing steps and reached this point. It was a whole different story(pain) all together. It did give me trouble in writing proper query involving guids but was able to figure it out.

  • meenoo Profile Picture
    meenoo 7,316 on at
    RE: Setting a field value issue

    Hi Alex,

    I just tried using toString but it still didn't work.

    More testing shows that the script won't work with '+ New' button on the form.

    'Save & New' on the form, 'New' button on the view and '+' button from dashboard are all working fine.

    What could be the reason with '+New' button on the form not working? Anybody experienced this before?

  • ashlega Profile Picture
    ashlega 34,477 on at
    RE: Setting a field value issue

    Hi meenoo,

     So did you try calling toString?

  • Suggested answer
    RE: Setting a field value issue

    Hi menoo,

    i think this issue may caused by this line:

    userRequest.open("GET", ODataPath + "/new_countries?$select=new_countrynumber&$filter=new_countryid eq " + countryId + "", false);

    How to deal with guid belongs to CRM version, so you could try the following lines:

    userRequest.open("GET", ODataPath + "/new_countries?$select=new_countrynumber&$filter=new_countryid eq '" + countryId + "'", false);

    userRequest.open("GET", ODataPath + "/new_countries?$select=new_countrynumber&$filter=new_countryid eq (guid'" + countryId + "')", false);

    Before to pass guid take the good habit to use a method to normalize guid, like the following, because you have to deal with sources which return different guid format:

    var NormalizeGuid = function (guid, brackets, uppercase) {
        var normalized = guid;
        normalized = normalized.toLowerCase();
        normalized = normalized.replace("{", "");
        normalized = normalized.replace("}", "");
        if (brackets)
            normalized = "{" + normalized + "}";
        if (uppercase)
            normalized = normalized.toUpperCase();
        return normalized;
    }

    Three examples follow:

    • NormalizeGuid('2d1f968b-5ac7-4a40-b54c-2d86d7b7b368', true, false) -->{2d1f968b-5ac7-4a40-b54c-2d86d7b7b368}
    • NormalizeGuid('2d1f968b-5ac7-4a40-b54c-2d86d7b7b368', false, true) -->2D1F968B-5AC7-4A40-B54C-2D86D7B7B368
    • NormalizeGuid('2d1f968b-5ac7-4a40-b54c-2d86d7b7b368', true, true) -->{2D1F968B-5AC7-4A40-B54C-2D86D7B7B368}

    Hope it helps.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

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,432 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans