MS CRM 2011 JScript getValue 与 setValue方法

简介:

在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。












本文转自JF Zhu博客园博客,原文链接:http://www.cnblogs.com/jfzhu/archive/2013/02/07/2909012.html    ,如需转载请自行联系原作者



相关文章
|
机器学习/深度学习 BI 数据格式
|
开发框架 JavaScript .NET
|
JSON 数据格式 网络架构
|
6月前
|
人工智能 物联网 BI
诊断设备企业必看!垂直医疗行业的CRM软件有哪些?
2025年,诊断设备企业竞争核心转向精细化服务。传统CRM难堪重任,垂直医疗CRM成破局关键。本文深度解析八骏医疗云等五大解决方案,揭秘如何通过设备全周期管理、代理商管控、智能耗材预警与私有化部署,构建以客户为中心的服务体系,抢占增长制高点。
398 124