Advertisement
azevedo_pedro

disassociate

Nov 5th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var disassociate = function (relationshipName, targetEntityName, targetId, relatedEntityName, relatedBusinessEntities, callback) {
  2.         ///<summary>
  3.         /// Sends synchronous/asynchronous request to disassociate records.
  4.         ///</summary>
  5.         ///<param name="relationshipName" type="String">
  6.         /// A JavaScript String corresponding to the relationship name
  7.         /// that is used for disassociate operations.
  8.         /// </param>
  9.         ///<param name="targetEntityName" type="String">
  10.         /// A JavaScript String corresponding to the schema name of the target entity
  11.         /// that is used for disassociate operations.
  12.         /// </param>
  13.         ///<param name="targetId" type="String">
  14.         /// A JavaScript String corresponding to the GUID of the target entity
  15.         /// that is used for disassociate operations.
  16.         /// </param>
  17.         ///<param name="relatedEntityName" type="String">
  18.         /// A JavaScript String corresponding to the schema name of the related entity
  19.         /// that is used for disassociate operations.
  20.         /// </param>
  21.         ///<param name="relationshipBusinessEntities" type="Array">
  22.         /// A JavaScript Array corresponding to the collection of the related entities as BusinessEntity
  23.         /// that is used for disassociate operations.
  24.         /// </param>
  25.         ///<param name="callback" type="Function">
  26.         /// A Function used for asynchronous request. If not defined, it sends a synchronous request.
  27.         /// </param>
  28.         var relatedEntities = relatedBusinessEntities;
  29.  
  30.         relatedEntities = isArray(relatedEntities) ? relatedEntities : [relatedEntities];
  31.  
  32.         var output = [];
  33.         for (var i = 0; i < relatedEntities.length; i++) {
  34.             if (relatedEntities[i].id != '') {
  35.                 output.push("<a:EntityReference>",
  36.                                 "<a:Id>", relatedEntities[i].id, "</a:Id>",
  37.                                 "<a:LogicalName>", relatedEntityName, "</a:LogicalName>",
  38.                                 "<a:Name i:nil='true' />",
  39.                             "</a:EntityReference>");
  40.             }
  41.         }
  42.  
  43.         var relatedXml = output.join("");
  44.  
  45.         var request = [
  46.             "<request i:type='a:DisassociateRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts'>",
  47.                 "<a:Parameters xmlns:b='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>",
  48.                     "<a:KeyValuePairOfstringanyType>",
  49.                         "<b:key>Target</b:key>",
  50.                         "<b:value i:type='a:EntityReference'>",
  51.                             "<a:Id>", targetId, "</a:Id>",
  52.                             "<a:LogicalName>", targetEntityName, "</a:LogicalName>",
  53.                             "<a:Name i:nil='true' />",
  54.                         "</b:value>",
  55.                     "</a:KeyValuePairOfstringanyType>",
  56.                     "<a:KeyValuePairOfstringanyType>",
  57.                         "<b:key>Relationship</b:key>",
  58.                         "<b:value i:type='a:Relationship'>",
  59.                             "<a:PrimaryEntityRole i:nil='true' />",
  60.                             "<a:SchemaName>", relationshipName, "</a:SchemaName>",
  61.                         "</b:value>",
  62.                     "</a:KeyValuePairOfstringanyType>",
  63.                     "<a:KeyValuePairOfstringanyType>",
  64.                     "<b:key>RelatedEntities</b:key>",
  65.                     "<b:value i:type='a:EntityReferenceCollection'>",
  66.                         relatedXml,
  67.                     "</b:value>",
  68.                     "</a:KeyValuePairOfstringanyType>",
  69.                 "</a:Parameters>",
  70.                 "<a:RequestId i:nil='true' />",
  71.                 "<a:RequestName>Disassociate</a:RequestName>",
  72.             "</request>"
  73.         ].join("");
  74.  
  75.         var async = !!callback;
  76.  
  77.         return doRequest(request, "Execute", async, function (resultXml) {
  78.             var response = jQuery(resultXml).find('ExecuteResult').eq(0);
  79.             var result = crmXmlDecode(response.text());
  80.             if (!async)
  81.                 return result;
  82.             else
  83.                 callback(result);
  84.             // ReSharper disable NotAllPathsReturnValue
  85.         });
  86.         // ReSharper restore NotAllPathsReturnValue
  87.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement