Call SFDC Web service Method using JavaScript
Problem Statement
Soap based webservice can be directly called using standard javascript ajax call or HTTP call.
The difference is only the request body Webservice only accept the SOAP format, so this call requires a valid SOAP envelop to make call.
In the below solution, I have used SFDC ‘Reset Password’ call which takes userid and reset user’s password.
Solution
SOAP Envelop
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com">
<soapenv:Header>
<urn:EmailHeader>
<urn:triggerAutoResponseEmail>1</urn:triggerAutoResponseEmail>
<urn:triggerOtherEmail>1</urn:triggerOtherEmail>
<urn:triggerUserEmail>1</urn:triggerUserEmail> <!—This headers has the value(0/1) to set if email notification should be generate for the activity - - >
</urn:EmailHeader>
<urn:SessionHeader>
<urn:sessionId>SESSIONID</urn:sessionId> <!-- SessionId -- >
</urn:SessionHeader>
</soapenv:Header>
<soapenv:Body>
<urn:resetPassword>
<urn:userId>USERID</urn:userId> <!—User Id, for which the password is to be set -- >
</urn:resetPassword>
</soapenv:Body>
</soapenv:Envelope>
Javascript Code Snippet
resetPassword:function(userId){
var currObject=this;
var responseArray=new Array();
var post_xml_request = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com">'
+ '<soapenv:Header>'
+ '<urn:EmailHeader>'
+ '<urn:triggerAutoResponseEmail>1</urn:triggerAutoResponseEmail>'
+ '<urn:triggerOtherEmail>1</urn:triggerOtherEmail>'
+ '<urn:triggerUserEmail>1</urn:triggerUserEmail>'
+ '</urn:EmailHeader>'
+ '<urn:SessionHeader>'
+ '<urn:sessionId>' + MyApp.connection.AjaxCallInitiator.access_token + '</urn:sessionId>'
+ '</urn:SessionHeader>'
+ '</soapenv:Header>'
+ '<soapenv:Body>'
+ '<urn:resetPassword>'
+ '<urn:userId>' + userId + '</urn:userId>'
+ '</urn:resetPassword>'
+ '</soapenv:Body>'
+ '</soapenv:Envelope>'
Ext.Ajax.request({
url: MyApp.connection.AjaxCallInitiator.instance_url + '/services/Soap/u/24.0 HTTP/1.1' ,
async: false,
headers: { 'Content-Type' : 'text/xml; charset="utf-8"','SOAPAction': '/services/Soap/u/24.0'},
params: post_xml_request,
success: function(response){
responseArray.push(true);
responseArray.push(response.responseText);
},
fail: function(response){
responseArray.push(false);
responseArray.push(response.responseText);
}
});
return responseArray;
}
Note: Have used EXT-Js for the javascript call to avoid Cross browser support
0 Comments