I'm using the modern SOAP endpoint and Sdk.Soap.js and Sdk.ExecuteMultiple.js to execute a bunch of create requests from Javascript. Everything works fine, except when I get the response items back, it logs out the same id for each response item. I can't figure out how to get the ids out of the response item. At the bottom, where I log out the ids, I get the same id logged out over and over again. Yet all of the records get created and they all have unique ids. Not sure how to get all of the response ids out. Here is my code:
createOpportunities = function(data) {
var createRequest = new Sdk.Collection(Sdk.OrganizationRequest);
var requestSettings = new Sdk.ExecuteMultipleSettings(true,true);
var pricelevel = {
'id': $("#season").find(':selected').data('pricelevelid'),
'name': $("#season").find(':selected').text()
};
var stage = {
'id': $("#stage").find(':selected').data('stageid'),
'name': $("#stage").find(':selected').text()
};
var product = {
'id': $("#product").find(':selected').data('productid'),
'name': $("#product").find(':selected').text()
};
var source = {
'id': $("#source").find(':selected').data('sourceid'),
'name': $("#source").find(':selected').text()
};
data.forEach(function(d,i){
var Opportunity = new Sdk.jms_opportunity();
Opportunity.JMS_ContactId.setValue(new Sdk.EntityReference('contact', d.contactid));
Opportunity.OwnerId.setValue(new Sdk.EntityReference('systemuser', d.ownerid.getId()));
Opportunity.JMS_pricelevelid.setValue(new Sdk.EntityReference('JMS_pricelevel', pricelevel.id));
Opportunity.JMS_stageid.setValue(new Sdk.EntityReference('str_ticketstage', stage.id));
Opportunity.JMS_ProductId.setValue(new Sdk.EntityReference('JMS_product', product.id));
Opportunity.JMS_sourceid.setValue(new Sdk.EntityReference('jms_source', source.id));
createRequest.add(new Sdk.CreateRequest(Opportunity));
});
var request = new Sdk.ExecuteMultipleRequest(createRequest, requestSettings);
Sdk.jQ.execute(request).done(function(resp) {
var responses = resp.getResponses();
responses.forEach(function(responseItem) {
var id = responseItem.getResponse().getId();
console.log(id);
});
}).fail(function(error) {
console.log(error);
});
};
EDIT
After doing some more debugging, the response body is coming back with the correct guids, but when that response gets parsed the same guid gets added each time the parse function loops...and that's where I am stuck.
Response body:
<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">
<s:Body>
<ExecuteResponse xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\">
<ExecuteResult xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
<a:ResponseName>ExecuteMultiple</a:ResponseName>
<a:Results xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">
<a:KeyValuePairOfstringanyType>
<b:key>IsFaulted</b:key>
<b:value i:type=\"c:boolean\" xmlns:c=\"http://www.w3.org/2001/XMLSchema\">false</b:value>
</a:KeyValuePairOfstringanyType>
<a:KeyValuePairOfstringanyType>
<b:key>Responses</b:key>
<b:value i:type=\"c:OrganizationResponseCollection\" xmlns:c=\"http://schemas.microsoft.com/xrm/2012/Contracts\">
<c:ExecuteMultipleResponseItem>
<c:Fault i:nil=\"true\"/>
<c:RequestIndex>0</c:RequestIndex>
<c:Response>
<a:ResponseName>Create</a:ResponseName>
<a:Results>
<a:KeyValuePairOfstringanyType>
<b:key>id</b:key>
<b:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">df07d3fb-862c-e511-bdfe-00155d01050d</b:value>
</a:KeyValuePairOfstringanyType>
</a:Results>
</c:Response>
</c:ExecuteMultipleResponseItem>
<c:ExecuteMultipleResponseItem>
<c:Fault i:nil=\"true\"/>
<c:RequestIndex>1</c:RequestIndex>
<c:Response>
<a:ResponseName>Create</a:ResponseName>
<a:Results>
<a:KeyValuePairOfstringanyType>
<b:key>id</b:key>
<b:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">e107d3fb-862c-e511-bdfe-00155d01050d</b:value>
</a:KeyValuePairOfstringanyType>
</a:Results>
</c:Response>
</c:ExecuteMultipleResponseItem>
</b:value>
</a:KeyValuePairOfstringanyType>
</a:Results>
</ExecuteResult>
</ExecuteResponse>
</s:Body>
</s:Envelope>
The ExecuteMultipleResponse function in Microsoft's Sdk.ExecuteMultiple.js taken from here: https://code.msdn.microsoft.com/SdkSoapjs-9b51b99a/sourcecode?fileId=113716&pathId=823928626 The parseResponse function is where I see the same guid get set but I don't know enough about parsing xml to know where this might be going wrong.
this.ExecuteMultipleResponse = function (responseXml) {
///<summary>
/// Response to ExecuteMultipleRequest
///</summary>
if (!(this instanceof Sdk.ExecuteMultipleResponse)) {
return new Sdk.ExecuteMultipleResponse(responseXml);
}
Sdk.OrganizationResponse.call(this)
// Internal properties
var _isFaulted = null;
var _responses = null;
// Internal property setter functions
function _setIsFaulted(xml) {
var valueNode = Sdk.Xml.selectSingleNode(xml, "//a:KeyValuePairOfstringanyType[b:key='IsFaulted']/b:value");
if (!Sdk.Xml.isNodeNull(valueNode)) {
_isFaulted = (Sdk.Xml.getNodeText(valueNode) == "true") ? true : false;
}
}
function _setResponses(xml) {
var valueNode = Sdk.Xml.selectSingleNode(xml, "//a:KeyValuePairOfstringanyType[b:key='Responses']/b:value");
if (!Sdk.Xml.isNodeNull(valueNode)) {
_responses = parseResponses(valueNode);
}
}
function parseResponses(xml) {
//Using Sdk.Collection rather than create a new class for Microsoft.Xrm.Sdk.ExecuteMultipleResponseItemCollection
var rv = new Sdk.Collection(Sdk.ExecuteMultipleResponseItem);
for (var i = 0; i < xml.childNodes.length; i++) {
var emri = new Sdk.ExecuteMultipleResponseItem();
emri.setRequestIndex(parseInt(Sdk.Xml.selectSingleNodeText(xml.childNodes[i], "l:RequestIndex"), 10));
var faultNode = Sdk.Xml.selectSingleNode(xml.childNodes[i], "l:Fault");
if (!Sdk.Xml.isNodeNull(faultNode)) {
emri.setFault(new Sdk.ExecuteMultipleFault(faultNode));
}
else {
var responseName = Sdk.Xml.selectSingleNodeText(xml.childNodes[i], "l:Response/a:ResponseName") + "Response";
var responseXml = Sdk.Xml.selectSingleNode(xml.childNodes[i], "l:Response/a:Results");
emri.setResponse(new Sdk[responseName](responseXml));
}
rv.add(emri);
}
return rv;
}
I am the author of the Sdk.Soap.js library and I can repro what you see. You should be able to fix this by editing the parseResponses function in the Sdk.ExecuteMultiple.js file. Replace the commented line below so it includes
.cloneNode(true)
.The problem is that when the node is passed through to the constructor of the Sdk.CreateResponse, the entire xml document was passed through and when it was evaluated using:
Sdk.Xml.selectSingleNode(xml, "//a:KeyValuePairOfstringanyType[b:key='id']/b:value");
this found the first instance of the whole document rather than just the specified childNode.Using cloneNode seems to address the issue, but I haven't done any testing on browsers other than IE. Please let me know if you find issues with other browsers.