Easy to use JavaScript Retrieve and RetrieveMultiple functions
گاهی اوقات نیاز دارید که هنگامی که مثلا یک فیلد Lookup را مقدار دهی میکنید، مقادیری از دیگر فیلدهای رکورد انتخاب شده در آن فیلد در توابع جاوا مورد استفاده قرار گیرد یا در فیلد های فرم حاظر به نمایش در آیند که در CRM 2013 این کار به راحتی با ایجاد Quick Create Form قابل انجام است اما با استفاده از توابع زیر امکان Retieve کردن فیلد های مختلف از یک رکورد Lookup شده امکان پذیر میشود.
در مثال زیر فیلد شماره موبایل یک Contact که loockup شده است Retrieve میگردد
من از SOAP استفاده کردم اما اینکار را با REST و JSON هم میتونیم به راحتی انجام بدیم. RESTKit.js توابع Retrieve , RetrieveMultiple ,Insert,Delete, Update را به صورت آماده در کتابخانه خود دارد.
موفق باشید
These functions are built around SOAP implementations of the Retrieve and RetrieveMultiple messages. I haven't annotated my code, so forgive any obvious lack of description of what the code is doing. I'll try to briefly summarize here:
MischiefMayhemSOAP
Parameters:
xmlSoapBody - (string) XML containing the body of the SOAP call
soapActionHeader - (string) The 'SOAPAction' specifying the type of SOAP call
Purpose:
Serves as a generic XMLHTTP interface to CRM's SOAP functions.
Returns:
(string) XML result from the CRM server.
RetrieveRecord
Parameters:
entityName - (string) The CRM-platform name of the desired entity
entityId - (string) The GUID of the record to retrieve
attrArray - (string[]) An Array of attributes to retrieve for the record
Purpose:
Easily retrieves a single record from CRM.
Returns:
(string[] or null) An Array of values from CRM, index-matching the attributes provided in attrArray. Returns null if there is an error with, or invalid return from, the SOAP call.
RetrieveMultiple
Parameters:
entityName - (string) The CRM-platform name of the desired entity
attrArray - (string[]) An Array of attributes to retrieve for the records
distinct - (string:"true" or "false") Directs CRM to return with or without duplicate records
criteriaXml - (string) XML containing any combination of Criteria or LinkEntities elements pertaining to the "RetrieveMultiple" SOAP message
Purpose:
Easily retrieves multiple records from CRM which match the rules defined in the criteriaXml.
Returns:
(string[n][] or null) An Array, with n matching-record elements, of an array of values from CRM, index-matching the attributes provided in attrArray. Returns null if there is an error with, or invalid return from, the SOAP call.
function ret_mobile()
{
function MischiefMayhemSOAP(xmlSoapBody, soapActionHeader) {
var xmlReq = ""
+ " + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"
+ " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"
+ Xrm.Page.context.getAuthenticationHeader()
+ " "
+ xmlSoapBody
+ " "
+ "";
var httpObj = new ActiveXObject("Msxml2.XMLHTTP");
httpObj.open('POST', '/mscrmservices/2007/crmservice.asmx', false);
httpObj.setRequestHeader('SOAPAction', soapActionHeader);
httpObj.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
httpObj.setRequestHeader('Content-Length', xmlReq.length);
httpObj.send(xmlReq);
var resultXml = httpObj.responseXML;
var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0) {
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert("The following error was encountered: " + msg);
return null;
} else {
return resultXml;
}
}
function RetrieveRecord(entityName, entityId, attrArray) {
var xmlSoapBody = ""
+ " "
+ " " + entityName + ""
+ " " + entityId + ""
+ " "
+ " ";
for (index in attrArray) {
xmlSoapBody += " " + attrArray[index] + "";
}
xmlSoapBody += ""
+ " "
+ " "
+ " ";
var resultXml = MischiefMayhemSOAP(xmlSoapBody, 'http://schemas.microsoft.com/crm/2007/WebServices/Retrieve');
if (resultXml != null) {
var resultArray = new Array();
for (index in attrArray) {
if (resultXml.selectSingleNode("//q1:" + attrArray[index]) != null) {
resultArray[index] = resultXml.selectSingleNode("//q1:" + attrArray[index]).nodeTypedValue;
} else {
resultArray[index] = null;
}
}
return resultArray;
} else {
return null;
}
}
function RetrieveMultiple(entityName, attrArray, distinct, criteriaXml) {
var xmlSoapBody = ""
+ " "
+ " "
+ " " + entityName + ""
+ " "
+ " ";
for (index in attrArray) {
xmlSoapBody += " " + attrArray[index] + "";
}
xmlSoapBody += ""
+ " "
+ " "
+ " " + distinct + ""
+ criteriaXml
+ " "
+ " ";
var resultXml = MischiefMayhemSOAP(xmlSoapBody, 'http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple');
if (resultXml != null) {
var resultArray = new Array();
var entityNodes = resultXml.selectNodes("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");
if (entityNodes.length > 0) {
for (var index = 0; index < entityNodes.length; index++) {
var entityNode = entityNodes[index];
resultArray[index] = new Array();
for (attrIndex in attrArray) {
if (entityNode.selectSingleNode("q1:" + attrArray[attrIndex]) != null) {
resultArray[index][attrIndex] = entityNode.selectSingleNode("q1:" + attrArray[attrIndex]).nodeTypedValue;
} else {
resultArray[index][attrIndex] = null;
}
}
}
return resultArray;
} else {
return null;
}
} else {
return null;
}
}
var mobile="";
mobile=RetrieveRecord('contact', Xrm.Page.getAttribute("new_contact").getValue()[0].id, new Array("mobilephone"));
Xrm.Page.getAttribute("new_mobile").setValue(String(mobile));
}