Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Setting a decimal value on a form doesnt always work - Dynamics CRM 2013

Posted on by Microsoft Employee

Hope someone can help with this. I know how to set a field value on a form and it works in a way. My query is around the fact that it only works if I put this line below into the form load event as a line by itself.

Xrm.Page.getAttribute('cal_entrynumber').setValue(1.01);

However, in reality I need to have some conditional checks before I set this value and when I put this line into those conditional checks, then what I notice is that the textbox is blank but if I look at the value in the data entity it does indicate it has the value of 1.01.

alert("No. in entity:" + Xrm.Page.data.entity.attributes.get('cal_entrynumber').getValue());


So I wanted to know if anyone can pinpoint me to anything that potentially might be the cause for this ... here is the code in which the line sits (and doesnt work).

var queryString = "?$select=cal_EntryNumber&$filter=cal_SafetyProgrammeNo eq '" + spNo + "'&top=1&$orderby=cal_EntryNumber desc";
SDK.REST.retrieveMultipleRecords(
    "cal_operation",
    queryString,
    function (results) {
        //alert(results.length);
        if (results.length == 0) {
            //var entry = 1.01;
            //alert("Entry No:" + Number(entry));
            Xrm.Page.getAttribute('cal_entrynumber').setValue(1.01);

        }
        else {
            // Code to go here.                              
        }
    },
    errorHandler,
    function () {
        // OnComplete handler
    }
    );

*This post is locked for comments

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Setting a decimal value on a form doesnt always work - Dynamics CRM 2013

    I've found out why this doesnt work (even though the code is correct) and I'm posting my answer here to help anyone else who might have the same issue.

    The Entry Number field gets focus when the form loads and this was emptying the field of its (javascript set) value. I fixed this by putting another textbox field as the first field on the form and now the field is showing the javascript set value (no code changes were required).

    I suspect this worked when I put the code into the form load event by itself as it gets run synchronously but when I put the same line of code into retrieveMultipleRecords success callback function, as this runs Asynchronously, it presents this issue. Hope this helps anyone else with this problem.

    A big thank you to Alex whose suggestion helped me to understand where the issue was and to Gopalan for his response. 

  • Suggested answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Setting a decimal value on a form doesnt always work - Dynamics CRM 2013

    Hi

    It is correct, and should set the value.

    Xrm.Page.getAttribute('cal_entrynumber').setValue(1.01);


    Please check any other function (example: a validation function) sets this field (onChange event of this field), after you set the value for this.



  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Setting a decimal value on a form doesnt always work - Dynamics CRM 2013

    decimal

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: Setting a decimal value on a form doesnt always work - Dynamics CRM 2013

    Hi,

      you code seems to be working fine - I tried it here as well. Wondering if you have some other code / business rules which would be resetting your field to "null". 

      If you create a new decimal field and use your original version of the code (with setValue called directly in the callback to make sure nothing else is interfering), would that new field still stay empty?

  • Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Setting a decimal value on a form doesnt always work - Dynamics CRM 2013

    Hi

    What is the data type of the field 'cal_entrynumber'?

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Setting a decimal value on a form doesnt always work - Dynamics CRM 2013

    An update... to make things more clear...

    If I uncomment out the line SetEntryNumber(1.01); in the code below, it works and the field gets populated.

    if (Xrm.Page.data.entity.attributes.get('cal_entrynumber').getValue() == null) {            
                //SetEntryNumber(1.01);
                getPreviousOperation(speRecord.cal_spe_sno);
            }


    If however, I comment the code above as is, then it doesnt work and the Entry Number field remains blank.

    function getPreviousOperation(spNo) {
        var queryString = "?$select=cal_EntryNumber&$filter=cal_SafetyProgrammeNo eq '" + spNo + "'&top=1&$orderby=cal_EntryNumber desc";
        SDK.REST.retrieveMultipleRecords(
            "cal_operation",
            queryString,
            function (results) {
                //alert(results.length);
                if (results.length == 0) {
                    SetEntryNumber(1.01);
                    
                }
                else {
                    SetEntryNumber(Number(parseInt(results[0].cal_EntryNumber) + 1));
                    
                }
            },
            errorHandler,
            function () {
                // OnComplete handler
            }
            );
    }
     
    function SetEntryNumber(number) {
        Xrm.Page.getAttribute('cal_entrynumber').setValue(1.01);
        //alert(number);
        //alert("Value in entity:" + Xrm.Page.data.entity.attributes.get('cal_entrynumber').getValue());
    }


  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Setting a decimal value on a form doesnt always work - Dynamics CRM 2013

    Try Xrm.Page.getAttribute('cal_entrynumber').setValue("1.01");

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Setting a decimal value on a form doesnt always work - Dynamics CRM 2013

    Yes. I have and it goes onto the line and doesn't throw any error.

  • Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: Setting a decimal value on a form doesnt always work - Dynamics CRM 2013

    Hi,

    Have you tried to debug this code in the browser using F12 and placing breakpoints?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans