Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
JavaScript - REST retrieveRecord error - "This control only accepts numbers or null as an input."

Answered JavaScript - REST retrieveRecord error - "This control only accepts numbers or null as an input."

  • Saturday, March 02, 2013 2:08 PM
     
      Has Code

    I am using the SDK.REST.retrieveRecord function from the SDK to retrieve the amount for a weekly budget (currency or money field) from a custom entity.  It seems to be working, however, once I grab the value from the custom entity and try to set it to the field it throws this error:

    Here is my code below:

    // Auto-fills the details of the Weekly Budget when a value is selected, clears fields when null
    function weeklyBudget_onchange() {
        var validation = Xrm.Page.data.entity.attributes.get('new_weeklybudgetprocedureid').getValue();
    
        if ((validation != null) && (validation != "undefined")) {
            var budgetNameId = Xrm.Page.data.entity.attributes.get('new_weeklybudgetprocedureid').getValue()[0];
            weeklyBudgetDetails(budgetNameId.id);
        } else {
            Xrm.Page.getAttribute("new_budgetamount").setValue(null);
        }
    
    }
    
    // Get Weekly Budget Amount values from Weekly Budget Calculator record
    function weeklyBudgetDetails(weeklybudgetId) {
        SDK.REST.retrieveRecord(
         weeklybudgetId,
         "new_weeklybudget",
         null, null,
         function (budget) {
             Xrm.Page.getAttribute("new_budgetamount").setValue(budget.new_Amount);
         },
         function () {
             alert('Error occurred. ' + weeklyBudgetDetails);
             //console.log("Error occurred. - weeklyBudgetDetails");
         }
       );
    }

    From what I have read it is easy to get a currency/money field from an entity, but to set it is harder.  Can someone help me with this?  Thanks!

All Replies

  • Sunday, March 03, 2013 3:07 AM
    Moderator
     
      Has Code

    Try:

    Xrm.Page.getAttribute("new_budgetamount").setValue(parseFloat(budget.new_Amount));


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

  • Sunday, March 03, 2013 4:47 AM
     
     
    Still got the same error.  Any other ideas? Also, to note--when I put an alert before I set the value to try to see if I was getting the right value, I see an "[object Object]" sort of error.  Not sure if that helps troubleshoot or not
  • Sunday, March 03, 2013 6:02 AM
    Moderator
     
     

    I'd put a break point on the line and look the the structure of the data being returned.

    How to Debug JScript in Microsoft Dynamics CRM 2011


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

  • Monday, March 04, 2013 7:34 AM
     
     

    Hello wikky

    debug the script and check the prob ther exactly and check the datatype of the field

    for money you need to use

    Xrm.Page.getAttribute(attribute)setValue(parseFloat(eval(value)));


    ms crm

  • Tuesday, March 05, 2013 2:45 PM
     
     

    What I don't understand is that I have calculations (simple addition) being done on another custom form and I am able to set the value of a money field using:

    Xrm.Page.getAttribute(attribute).setValue(value);

    Why would getting a money field from a separate entity be any different?  Once I grab the money value from another entity using REST, I thought I would be able to just set an addtional money field using the .setValue() method.

    JLattimer--Would it be possible to move this to the CRM Development forum?  I put it in CRM Deployment by mistake!



    • Edited by wikky2007 Tuesday, March 05, 2013 2:47 PM
    •  
  • Wednesday, March 06, 2013 2:25 PM
    Moderator
     
     Answered Has Code

    When you are doing calculations within the form itself - when you grab the values they are already the correct data type. When you get the results back from a REST call - they are coming back as a string. As you figured out CRM doesn't want you to assign a string value to a currency field. 

    Back to your original question - try:

    Xrm.Page.getAttribute("new_budgetamount").setValue(budget.new_Amount.Value);

    I didn't test that using SDK.REST but the result should be the same - as an example I grabbed the Revenue field from one Account and populated it in another Account. Look for the .Value of the returned element and then add the parseFloat around that. 

    function Go() {
        var odataSelect = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet?$select=Revenue&$filter=AccountId eq guid'E84757AB-6D8D-E111-9B4E-00505685006C'";
        var retrieveReq = new XMLHttpRequest();
        retrieveReq.open("GET", odataSelect, false);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        retrieveReq.onreadystatechange = function () {
            getResult(this);
        };
        retrieveReq.send();
    }
    
    function getResult(retrieveReq) {
        if (retrieveReq.readyState == 4) {
            if (retrieveReq.status == 200) {
                var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d;
                var retrievedValue = retrieved.results[0].Revenue.Value;
                Xrm.Page.getAttribute("revenue").setValue(parseFloat(retrievedValue));
            }
        }
    }

    Will move forums as well :)


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

  • Wednesday, March 06, 2013 3:01 PM
     
     Answered Has Code

    This worked!:

    Xrm.Page.getAttribute("new_budgetamount").setValue(parseFloat(budget.new_Amount.Value));


    Thank you very much for your help!

    • Marked As Answer by wikky2007 Wednesday, March 06, 2013 3:01 PM
    •