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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Thomas David Dayman Profile Picture
    11,323 on at

    Hi,

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

  • Community Member Profile Picture
    on at

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

  • Suggested answer
    Community Member Profile Picture
    on at

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

  • Community Member Profile Picture
    on at

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


  • Gopalan Bhuvanesh Profile Picture
    11,401 on at

    Hi

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

  • ashlega Profile Picture
    34,477 on at

    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?

  • Community Member Profile Picture
    on at

    decimal

  • Suggested answer
    Gopalan Bhuvanesh Profile Picture
    11,401 on at

    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.



  • Verified answer
    Community Member Profile Picture
    on at

    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. 

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans