SOAP Inbound and Outbound:
=================
Login into Org1 Destination org: sfdc338@gmail.com
Step 1: Create SOAP inbound web service:
global class SoapInboundWebService {
webservice static String createAccount(String accountName, String accountNumber,
String accountPhone, String accountDescription)
{
Account acc = new Account();
acc.Name = accountName;
acc.AccountNumber = accountNumber;
acc.Phone = accountPhone;
acc.Description = accountDescription;
insert acc;
return acc.Name;
}
}
Step 2: Generate Partner wsdl and SoapInboundWebService wsdl files.
Go to setup ==> Quick Find ==> APIs ==>
Generate Partner wsdl.
Generate wsdl for "SoapInboundWebService" class. ==> Go to Apex class "SoapInboundWebService" ==> click on "Generate WSDL" button and save file.
Step 3: Now go to source org and generate apex classes for Partner WSDL and SoapInboundWebService wsdl.
When we generate apex classes using SoapInboundWebService wsdl, salesforce will generate two apex classes.
They are
soapSforceComSchemasClassSoapinboun
AsyncSoapSforceComSchemasClassSoapinboun
Now go to class "soapSforceComSchemasClassSoapinboun" and get the elements to create Soap outbound call.
Before making a callout ==> we need to create remote site settings and store url of target org in client org(from here making callout).
Remote Site Settings: Remote Site Name SOHELSFDC338
Remote Site URL: https://ap24.salesforce.com
Below is Soap outbound call: Go to developer console and execute below from anonymous block. An account will be created in target org.
partnerSoapSforceCom.Soap sp = new partnerSoapSforceCom.Soap();
partnerSoapSforceCom.LoginResult log = sp.login('sfdc338@gmail.com', 'sohel42011BW2rEx55O33yFxYkfc8QLOv2');
soapSforceComSchemasClassSoapinboun.SessionHeader_element SessionHeader = new soapSforceComSchemasClassSoapinboun.SessionHeader_element();
SessionHeader.sessionId = log.sessionId;
soapSforceComSchemasClassSoapinboun.SoapInboundWebService ws = new soapSforceComSchemasClassSoapinboun.SoapInboundWebService();
ws.SessionHeader = SessionHeader;
ws.createAccount('SA1','1111','9999999999','SA1 Description');
Now let's call this logic from Trigger.:
trigger SoapInboundWebServiceTrigger on Account (before insert) {
for(Account acc: Trigger.new){
SoapInboundWebServiceTriggerHandler.createAccountMethod(acc.Name, acc.AccountNumber, acc.Phone, acc.Description);
}
}
==========
public class SoapInboundWebServiceTriggerHandler {
@future(callout=true)
public static void createAccountMethod(String accName, String accNumber, String accPhone, String accDescription){
partnerSoapSforceCom.Soap sp = new partnerSoapSforceCom.Soap();
partnerSoapSforceCom.LoginResult log = sp.login('sfdc338@gmail.com', 'sohel42011BW2rEx55O33yFxYkfc8QLOv2');
soapSforceComSchemasClassSoapinboun.SessionHeader_element SessionHeader = new soapSforceComSchemasClassSoapinboun.SessionHeader_element();
SessionHeader.sessionId = log.sessionId;
soapSforceComSchemasClassSoapinboun.SoapInboundWebService ws = new soapSforceComSchemasClassSoapinboun.SoapInboundWebService();
ws.SessionHeader = SessionHeader;
ws.createAccount(accName, accNumber, accPhone, accDescription);
}
}
Created an Account record from source and account has been created in target org successfully.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rest Inbound Web Service:
==> Login into Salesforce 1 org - Destination org - SFDC338@gmail.com
Created below class:
@RestResource(urlMapping='/Account10/*')
global with sharing class RestInboundWebService {
@HttpPost
global static String doPost(String name, String phone, String website)
{
Account account = new Account();
account.Name = name;
account.phone = phone;
account.website = website;
insert account;
return 'Congrats... New Account has been created in SF1 instance. Here is Account Id :' +account.Id;
}
}
Create Connected App with below details:
Name: SFDC338
Callback URL: https://sohel-sf-dev-ed.my.salesforce.com/services/oauth2/callback
After creation of Connected App, Consumer Key and Consumer Secret will be generated by Salesforce.
Consumer Key: 3MVG9fe4g9fhX0E7RnFaHALdmsbgONcNa1sfLPMUbW10tKIzxjcto8McjyvJx0bPlUMqoeh7P8ZAUc7LNgtNY
Consumer Secret: F20C3C0347E54A07C7E3A989266535821607E3CB43B6BA216934D9E31030E620
==================================================
Rest Outbound Web Service:
==> Login into Salesforce 2 org - Source org - sohel@sfdc2.com
Create "Remote Site Settings"
Remote Site Name: SOHELSFDC338
Remote Site URL: https://sohel-sf-dev-ed.my.salesforce.com
Create below classes:
public class RestAccessToken{
String clientId = '3MVG9fe4g9fhX0E7RnFaHALdmsbgONcNa1sfLPMUbW10tKIzxjcto8McjyvJx0bPlUMqoeh7P8ZAUc7LNgtNY';
//Use your Client Secret
String clientsecret='F20C3C0347E54A07C7E3A989266535821607E3CB43B6BA216934D9E31030E620';
//Your Destination org username
String username='sfdc338@gmail.com';
//Your Destination orgPassword+Security Token
String password='sohel42011BW2rEx55O33yFxYkfc8QLOv2';
String accesstoken_url='https://login.salesforce.com/services/oauth2/token';
String authurl='https://login.salesforce.com/services/oauth2/authorize';
public class deserializeResponse {
public String id;
public String access_token;
}
public String ReturnAccessToken(RestAccessToken Acc){
String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
Http h= new Http();
HttpRequest req= new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
//Change "ap4" in url to your Destination Org Instance
req.setEndpoint('https://sohel-sf-dev-ed.my.salesforce.com/services/oauth2/token');
HttpResponse res=h.send(req);
System.debug(res.getBody()+'By-AP-1986-Response');
deserializeResponse resp1= (deserializeResponse)JSON.deserialize(res.getBody(),deserializeResponse.class);
System.debug(resp1+'By-AP-deserializeresponse');
System.debug(resp1.access_token+'Access token');
return resp1.access_token;
}
}
////////////
public class CalloutForRestInboundWebService {
@future(callout=true)
public static void createAccount(String accName, String accPhone, String accWebsite){
RestAccessToken acc1= new RestAccessToken();
String accessToken=acc1.ReturnAccessToken(acc1);
Httprequest req=new httprequest();
Http http = new Http();
String endPointURL='https://sohel-sf-dev-ed.my.salesforce.com/services/apexrest/Account10/';
req.setendpoint(endPointURL);
req.setHeader('Content-Type', 'application/xml; charset=utf-8');
req.setBody('<?xml version="1.0" encoding="UTF-8" ?><request><name>'+accName+'</name><phone>'+accPhone+'</phone><website >'+accWebsite+'</website > </request>');
req.setmethod('POST');
req.setHeader('Authorization','Authorization: Bearer '+accessToken);
system.debug('Access token @@@@@@@------->'+accessToken);
String response;
HTTPResponse res = http.send(req);
response=res.getbody();
System.debug('****************res.getStatusCode();'+res.getStatusCode());
System.debug('****************res.getbody();'+res.getbody());
//return null;
}
}
///////////////
trigger CalloutForRestInboundWebService_Trigger on Account (before insert) {
for(Account acc: Trigger.new){
CalloutForRestInboundWebService.createAccount(acc.Name, acc.Phone, acc.Website);
}
}
No comments:
Post a Comment