RSS

Tag Archives: XML

Closing Opportunity using SOAP Request with Close Date

Project scenario

Using SOAP request I need to close opportunity from a custom HTML page. While closing opportunity, I need to send date also as close date for ‘Close Opportunity’.

Explanation of Code

Here format of date is very much different which is used to send through SOAP. I faced problem and stuck for 3 to 4 hours for this.

Finally I did.

Suppose,

var dateToSend = new Date();

Take a Date object and format it as :

dateToSend.format("yyyy-MM-ddThh:mm:ss.sszzz");

Only this Date format is taken by SOAP request.

Total SOAP request is as follows :

function CreateSoapXml(Id, Description, ActualRevenue, CloseDate, CompetitorId, Subject, Status) {
    var callback;

    xml += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
    xml += " <s:Body>";
    xml += " <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
    xml += " <request i:type=\"b:LoseOpportunityRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
    xml += " <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
    xml += " <a:KeyValuePairOfstringanyType>";
    xml += " <c:key>OpportunityClose</c:key>";
    xml += " <c:value i:type=\"a:Entity\">";
    xml += " <a:Attributes>";
    xml += " <a:KeyValuePairOfstringanyType>";
    xml += " <c:key>opportunityid</c:key>";
    xml += " <c:value i:type=\"a:EntityReference\">";
    xml += " <a:Id>" + Id + "</a:Id>";
    xml += " <a:LogicalName>opportunity</a:LogicalName>";
    xml += " <a:Name i:nil=\"true\" />";
    xml += " </c:value>";
    xml += " </a:KeyValuePairOfstringanyType>";
    xml += " <a:KeyValuePairOfstringanyType>";
    xml += " <c:key>description</c:key>";
    xml += " <c:value i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">" + Description + "</c:value>";
    xml += " </a:KeyValuePairOfstringanyType>";
    xml += " <a:KeyValuePairOfstringanyType>";
    xml += " <c:key>competitorid</c:key>";
    xml += " <c:value i:type=\"a:EntityReference\">";
    xml += " <a:Id>" + CompetitorId + "</a:Id>";
    xml += " <a:LogicalName>competitor</a:LogicalName>";
    xml += " <a:Name i:nil=\"true\" />";
    xml += " </c:value>";
    xml += " </a:KeyValuePairOfstringanyType>";
    xml += " <a:KeyValuePairOfstringanyType>";
    xml += " <c:key>actualrevenue</c:key>";
    xml += " <c:value i:type=\"a:Money\">";
    xml += " <a:Value>" + ActualRevenue + "</a:Value>";
    xml += " </c:value>";
    xml += " </a:KeyValuePairOfstringanyType>";
    xml += " <a:KeyValuePairOfstringanyType>";
    xml += " <c:key>subject</c:key>";
    xml += " <c:value i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">" + Subject + "</c:value>";
    xml += " </a:KeyValuePairOfstringanyType>";
    xml += " <a:KeyValuePairOfstringanyType>";
    xml += " <c:key>actualend</c:key>";
    xml += " <c:value i:type=\"d:dateTime\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">" + CloseDate + "</c:value>";
    xml += " </a:KeyValuePairOfstringanyType>";
    xml += " </a:Attributes>";
    xml += " <a:EntityState i:nil=\"true\" />";
    xml += " <a:FormattedValues />";
    xml += " <a:Id>00000000-0000-0000-0000-000000000000</a:Id>";
    xml += " <a:LogicalName>opportunityclose</a:LogicalName>"
    xml += " <a:RelatedEntities />";
    xml += " </c:value>";
    xml += " </a:KeyValuePairOfstringanyType>";
    xml += " <a:KeyValuePairOfstringanyType>";
    xml += " <c:key>Status</c:key>";
    xml += " <c:value i:type=\"a:OptionSetValue\">";
    xml += " <a:Value>" + Status + "</a:Value>";
    xml += " </c:value>"
    xml += " </a:KeyValuePairOfstringanyType>";
    xml += " </a:Parameters>";
    xml += " <a:RequestId i:nil=\"true\" />";
    xml += " <a:RequestName>LoseOpportunity</a:RequestName>";
    xml += " </request>";
    xml += " </Execute>";
    xml += " </s:Body>";
    xml += "</s:Envelope>";
}

//Then send SOAP request as :

var req = new XMLHttpRequest();
req.open("POST", GetServerUrl(), (callback != null));
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
req.send(xml);

if (req.readyState == 4) {
    if (req.status == 200) {
        alert("Opportunity Closed");
        Cancel();
        //window.opener.location.reload();
    }
    else {
        //errorCallback(_getError(req.responseXML));
        alert("There is an error occured");
    }
}

By Anish Thakur
Senior Software Engineer @Team DynamicsCRM.
Mindfire Solutions

Advertisements
 

Tags: , , , , , , , , , , , , , , , , ,

 
%d bloggers like this: