MS CRM 2011 JScript getValue 与 setValue方法

 

原创地址:http://www.cnblogs.com/jfzhu/archive/2013/02/07/2909012.html

转载请注明出处

 

在CRM Form中,如果使用attributeObj.getAttributeType()方法,你大概可以获得以下几种类型:

  • boolean
  • datetime
  • decimal
  • double
  • integer
  • lookup
  • memo
  • money
  • optionset
  • string

decimal, double, integer, string 类型都比较简单,我在本文中介绍一下对其他几种类型使用getValue和setValue方法。

 

 

(一) getValue方法

1. boolean

boolean对应的是CRM的Two Options 类型,比如contact entity的 donotemail field。

var donotemail = Xrm.Page.getAttribute("donotemail").getValue(); 
alert(typeof donotemail); // boolean 
alert(donotemail); // false 

 

2. datetime

datetime 对应的是 CRM 中的DateTime field,比如contact的birthdate。

var birthdate = Xrm.Page.getAttribute("birthdate").getValue(); 
alert(typeof birthdate); // object 
alert(birthdate); // Thu Feb 7 00:00:00 UTC+0100 2013


3. lookup

lookup 对应的是 CRM的 lookup field, 比如contact 的 parentcustomerid。getValue返回的是一个数组,如果是multi lookup field, 比如 activity party,你需要遍历返回的数组,如果是single lookup,你可以返回第一个元素(比如parentcustomer[0])。数组中的每个元素都有entityType,name,和id属性。

var parentcustomer = Xrm.Page.getAttribute("parentcustomerid").getValue(); 
alert(typeof parentcustomer); // object 
if (parentcustomer != null) { 
    for (var i = 0; i < parentcustomer.length; i++) { 
        alert(parentcustomer[i].entityType); // account 
        alert(parentcustomer[i].name);  // Test account 
        alert(parentcustomer[i].id); // {33BFAE2E-324F-E111-B4BA-00155DA83B1F} 
    } 
} 


4. money

money对应CRM中的Currency field,比如contact 的 creditlimit。

var creditlimit = Xrm.Page.getAttribute("creditlimit").getValue(); 
alert(typeof creditlimit); // number 
alert(creditlimit);  // 9999.99

 

5. optionset

参见 MS CRM 2011 如何获得Option Set的Label与Value

 

(二) setValue方法

1. boolean

var donotemail = Xrm.Page.getAttribute("donotemail").getValue(); 
Xrm.Page.getAttribute("donotbulkemail").setValue(donotemail); 
Xrm.Page.getAttribute("donotphone").setValue(true); 

 

2. datetime

//Xrm.Page.getAttribute("anniversary").setValue(null);

//Xrm.Page.getAttribute("anniversary").setValue(new Date());

//Xrm.Page.getAttribute("anniversary").setValue(new Date("October 13, 1975 11:13:00"));

var birthdate = Xrm.Page.getAttribute("birthdate").getValue(); 
//Xrm.Page.getAttribute("anniversary").setValue(birthdate);

var newdate = new Date(birthdate); 
// + 1 day 
//    newdate.setDate(newdate.getDate() + 1); 
//    Xrm.Page.getAttribute("anniversary").setValue(newdate);

// + 3 months 
newdate.setMonth(newdate.getMonth() + 3); 
Xrm.Page.getAttribute("anniversary").setValue(newdate); 

 

3. lookup

    Xrm.Page.getAttribute("parentcustomerid").setValue(null); 
//    var lookupReference = []; 
//    lookupReference[0] = {}; 
//    lookupReference[0].id = "{33BFAE2E-324F-E111-B4BA-00155DA83B1F}"; 
//    lookupReference[0].entityType = "account"; 
//    lookupReference[0].name = "Test"; 
//    Xrm.Page.getAttribute("parentcustomerid").setValue(lookupReference);

 

4. money

//Xrm.Page.getAttribute("creditlimit").setValue(null); 
Xrm.Page.getAttribute("creditlimit").setValue(99.99);


5. optionset

注意 getOptions方法返回的option values是字符串类型。如果你想在setValue方法中使用它们,你必须用parseInt将字符串cast为int。

参见 MS CRM 2011 如何获得Option Set的Label与Value

 

posted @ 2013-02-07 22:10  AI观星台  阅读(1995)  评论(0编辑  收藏  举报