4

我真的找不到我如何從貨幣字段檢索值並將其設置爲另一個實體的另一個貨幣字段的值。CRM 2011 - 使用javascript設置貨幣字段值

我下面的代碼是不工作:

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

     } 
    } 

最高審計機關的錯誤,只有除了數字或零控制。

任何幫助將非常感謝!謝謝!

回答

6

我曾經使用過這個,儘管我不是eval的粉絲。

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

這是一篇關於設置帶查詢值的表單字段的博客文章。

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

+0

謝謝!大文章也:) – ThdK

+1

你應該只需要使用parseFloat,你不應該需要當前在該函數中的評估。選擇列表值和使用parseInt也是一樣(注意在使用parseInt時應該指定基數)。 – GotDibbs

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

由於錯誤狀態的屬性只需要數字或空。要遵守第一個是NaN檢查是否 parseFloat返回一個數字。如果它返回未定義,它會嘗試從默認值(如果提供)獲取數字。 如果這是未定義的,而不是一個數字,那麼它分配一個空值。 如果您不需要默認值或默認值總是已知(即空值或0.0),您可以省略第二個isNaN測試

相關問題