如何实现partylist效果
在crm4.0中我们看到我们设置的字段类型只有lookup类型,并没有系统实体里面所创建的那种partylist类型的数据,最近客户提出了这方面的需求。所以,我们用js+lookup+varchar类型模拟实现了partylist类型。以下是实现方法:
1.首先我们需要建立:一个lookup类型的属性(提供给用户选择数据,命名为new_power_flow_new_user_power),一个nvarchar类型属性(保存用户选择的数据,命名为new_power_flow)。
2.在实体的界面上将两个属性添加到界面的相应位置(nvarchar类型属性位置可以随便存放,但一定要放在界面上)。
3.在实体的onload事件中注入脚本:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
var CRM_FORM_TYPE_CREATE = 1;
var CRM_FORM_TYPE_UPDATE = 2;
var CRM_FORM_TYPE_READ_ONLY =3;
var CRM_FORM_TYPE_DISABLED = 4;
var CRM_FORM_TYPE_QUICK_CREATE=5;
var CRM_FORM_TYPE_BULK_EDIT=6;
crmForm.all.new_power_flow_c.style.display = "none";
crmForm.all.new_power_flow_d.style.display = "none";
//设置为partylist样式
crmForm.all.new_power_flow_new_user_power.lookupstyle ='multi';
if(crmForm.FormType != CRM_FORM_TYPE_QUICK_CREATE && crmForm.FormType != CRM_FORM_TYPE_BULK_EDIT)
{
var bccField = crmForm.all.new_power_flow_new_user_power;
var strBcc=crmForm.all.new_power_flow.DataValue;
if (strBcc!=null)
{
var strField=strBcc.split(";#");
var strArray = new Array();
var lookupItem = new Array();
for (i=0; i<strField.length; i++)
{
strArray=strField[i].split("*#");
if (strArray[0]!=""){
lookupItem[i] = new LookupControlItem (strArray[1], 10025, strArray[2],null,null,null,'new_power_flow');
}
}
crmForm.all.new_power_flow_new_user_power.DataValue = lookupItem ;
}
4.在new_power_flow_new_user_power的onchange时间里面添加如下js脚本:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
var CRM_FORM_TYPE_CREATE = 1;
var CRM_FORM_TYPE_UPDATE = 2;
var CRM_FORM_TYPE_READ_ONLY =3;
var CRM_FORM_TYPE_DISABLED = 4;
var CRM_FORM_TYPE_QUICK_CREATE=5;
var CRM_FORM_TYPE_BULK_EDIT=6;
//debugger;
if(crmForm.FormType != CRM_FORM_TYPE_QUICK_CREATE && crmForm.FormType != CRM_FORM_TYPE_BULK_EDIT)
{
var bccField = crmForm.all.new_power_flow_new_user_power;//抄送人员
var strBcc="";
var strShow="";
var bccResult;
if (bccField.DataValue==null){crmForm.all.new_power_flow.DataValue=""}
else{
for (i=0; i<bccField.DataValue.length; i++)
{
bccResult = RetrieveSingleEntity(bccField.DataValue[i].typename, bccField.DataValue[i].id, null);
var arruomidLookup=new Array();
arruomidLookup=GetLookupFieldValue(bccResult, "new_flow_id");
if (arruomidLookup!=undefined)
{
//alert(arruomidLookup[0].id);
strBcc+=arruomidLookup[0].id+"*#"+bccField.DataValue[i].id+"*#"+bccField.DataValue[i].name+";#";
strShow+=arruomidLookup[0].id+";";
}
}
crmForm.all.new_power_flow.DataValue=strBcc;
//crmForm.all.new_power_show.DataValue=strShow;
}
}
5.在实体onsave中加入如下事件:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
crmForm.all.new_power_flow_new_user_power.DataValue = null;
6.其中我们使用到一些公用方法,大家可以写到CRM的脚本文件中,或者直接在界面上注册:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
function GetLookupFieldValue(oResult,fieldScheduleName)
{
for(var i=0; i<oResult.length; i++)
{
if(oResult[i] == undefined)
{
continue;
}
if(oResult[i][0] == undefined)
{
continue;
}
if(oResult[i][0] ==fieldScheduleName )
{
var item = new Object();
item.id = oResult[i][1];
item.name = oResult[i][2];
item.typename=oResult[i][3];
var data = new Array();
data[0] = item;
break;
}
}
return data;
}
function RetrieveSingleEntity(entityName, entityId, columnSet)
{
var soapNS = "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"";
var xsiNS = "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
var xsdNS = "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"";
var defaultNS = "xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\"";
var queryNS = "xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\"";
var async = false;
var sMessage = BuildStart() + BuildBody(entityName, entityId, columnSet) + BuildEnd();
var oHttp = new ActiveXObject("Msxml2.XMLHTTP");
SendMessage(oHttp);
return BuildResult(oHttp);
function SendMessage(oHttp)
{
var url = "/mscrmservices/2007/crmservice.asmx";
// send the POST to the Web service
oHttp.open("POST", url, async);
oHttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
oHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
oHttp.setRequestHeader("Content-Length", sMessage.length);
oHttp.Send(sMessage);
}
function BuildResult(oHttp)
{
var response = oHttp.responseText;
//alert(response);
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
xmlDoc.async = async;
xmlDoc.loadXML(response);
if (xmlDoc.parseError.errorCode != 0)
{
var xmlErr = xmlDoc.parseError;
alert("Error retrieving Recipient: " + xmlErr.reason);
}
else
{
var arrResult = new Array();
var returnNode = xmlDoc.documentElement.selectSingleNode("/soap:Envelope/soap:Body/ExecuteResponse/Response/BusinessEntity/Properties");
for (var i = 0; i < returnNode.childNodes.length; i++)
{
arrResult[i] = new Array();
arrResult[i][0] = returnNode.childNodes[i].attributes[1].value;
arrResult[i][1] = returnNode.childNodes[i].text;
if (returnNode.childNodes[i].attributes[0].value=="PicklistProperty")
{
if ( returnNode.childNodes[i].childNodes[0].attributes[0]!=undefined)
{
arrResult[i][2] = returnNode.childNodes[i].childNodes[0].attributes[0].value;
}
}
if (returnNode.childNodes[i].attributes[0].value=="LookupProperty")
{
if ( returnNode.childNodes[i].childNodes[0].attributes[0]!=undefined)
{
arrResult[i][2] = returnNode.childNodes[i].childNodes[0].attributes[0].value;
if (returnNode.childNodes[i].childNodes[0].attributes[1]!=undefined)
{
arrResult[i][3] = returnNode.childNodes[i].childNodes[0].attributes[1].value;
}
}
}
}
}
return arrResult;
}
function BuildStart()
{
var sMessage = "";
sMessage += "<soap:Envelope ";
sMessage += soapNS + " ";
sMessage += xsiNS + " ";
sMessage += xsdNS + ">";
sMessage += GenerateAuthenticationHeader();
return sMessage;
}
function BuildBody(entityName, entityId, columnSet)
{
var sMessage = "";
sMessage += "<soap:Body>";
sMessage += "<Execute " + defaultNS + ">";
sMessage += "<Request xsi:type=\"RetrieveRequest\" ReturnDynamicEntities=\"true\">";
sMessage += "<OptionalParameters>";
sMessage += "<OptionalParameter xsi:nil=\"true\" />";
sMessage += "</OptionalParameters>";
sMessage += "<Target xsi:type=\"TargetRetrieveDynamic\">";
sMessage += "<EntityName>" + entityName + "</EntityName>";
sMessage += "<EntityId>" + entityId + "</EntityId>";
sMessage += "</Target>";
if (columnSet == null || columnSet.length == 0)
{
sMessage += "<ColumnSet " + queryNS + " xsi:type=\"q1:AllColumns\" />";
}
else
{
sMessage += "<ColumnSet " + queryNS + " xsi:type=\"q1:ColumnSet\">";
sMessage += "<q1:Attributes>";
for (var i = 0; i < columnSet.length; i++)
{
sMessage += "<q1:Attribute>";
sMessage += columnSet[i];
sMessage += "</q1:Attribute>";
}
sMessage += "</q1:Attributes>";
sMessage += "</ColumnSet>";
}
sMessage += "</Request>";
sMessage += "</Execute>";
sMessage += "</soap:Body>";
return sMessage;
}
function BuildEnd()
{
return "</soap:Envelope>";
}
}
7.运行后效果如下图所示