4

I really can't find how i can retrieve the value from a currency field and set it as a value for another currency field for another entity.

My following code is not working:

var entities = retrieveRecords("trainingSet?$filter=trainingId eq guid'" + GetLookUpData("trainingid").id + "'");
    if (entities != null) {
        if (entities.d.results.length > 0) {
            if (entities.d.results[0]["Price"] != null) {
                alert(entities.d.results[0]["Price"]);
                Xrm.Page.getAttribute("price").setValue(entities.d.results[0]["Price"].getValue());
                Xrm.Page.getAttribute("price").setSubmitMode("always");
            }

        }
    }

Error sais that the control only except numbers or null.

Any help would be really appreciated! Thanks!

ThdK
  • 9,916
  • 23
  • 74
  • 101

2 Answers2

6

I have used this before even though I am not a fan of the eval.

function SetMoneyAttribute(value, attribute) {
                      Xrm.Page.getAttribute(attribute)
                    .setValue(parseFloat(eval(value)));
        }

here is a blog post about setting form fields with queried values.

http://crmscape.blogspot.com/2011/03/crm-2011-odata-json-and-crm-forms.html

Bryan Weaver
  • 4,455
  • 1
  • 29
  • 39
  • Thank you! Great article too :) – ThdK Aug 25 '11 at 14:04
  • 1
    You should only need to use parseFloat, you shouldn't need the eval that you currently have in that function. Same goes for picklist values and using parseInt (note you should specify the radix when using parseInt). – GotDibbs Aug 30 '11 at 04:22
1
//mimic crm object model
var Xrm = { 
    Page : { 
        getAttribute : function(sAttr) { 
            return { 
                setValue : function(nValue) { 
                    alert(sAttr + ': ' + nValue);
                }
            }; 
        }  
    } 
};

function mySetValue(sAttr, nValue, nDefault) {
    Xrm.Page.getAttribute(sAttr)
        .setValue(
        !isNaN(nValue = parseFloat(nValue)) || 
        !isNaN(nValue = nDefault)
        ? nValue
        : null);                
} 

//call with various types of values
mySetValue("new_attr1",0);
mySetValue("new_attr2","");
mySetValue("new_attr3",4);
mySetValue("new_attr4",34.3434);
mySetValue("new_attr5","545.43");
mySetValue("new_attr6",{},0);
//mySetValue("new_attr7",entities.d.results[0]["Price"], 100.00);

As the error states the attributes requires only numbers or null. To comply the first isNaN checks if parseFloat returns a number. If it returns undefined it tries to get the number from the default value (if supplied). If that is undefined and not a number then it assign a null value. You may omit the second isNaN test if you don’t need a default or if the default is always known (i.e. null or 0.0)

Adi Katz
  • 548
  • 3
  • 9