@RestResource(urlMapping='/createAccountRecord/*')
global with sharing class createAccountRecord {
@HttpPost
global Static string createAccount(){
RestRequest req = RestContext.request;
RestResponse res = Restcontext.response;
string jsonString=req.requestBody.tostring();
system.debug('jsonString'+jsonString);
boolean successCheck;
List<responseWrapper> wRespList=(List<responseWrapper>) JSON.deserialize(jsonString,List<responseWrapper>.class);
Account obj=new Account();
List<Account> accList= new List<Account>();
for(responseWrapper wResp: wRespList){
obj.Name=wResp.Name;
obj.Description=wResp.Description;
obj.Type=wResp.Type;
obj.Industry=wResp.Industry;
accList.add(obj);
}
try{
if(accList.size() > 0){
Insert accList;
successCheck=true;
}
}
catch(Exception e){
successCheck=false;
}
if(successCheck)
return 'Success';
else
return 'Failure';
}
global class responseWrapper{
global string Name;
global string Description;
global string Type;
global string Industry;
}
}
1) Create an apex trigger and apex controller.
2) Create a remote site setting for the URL of ORG B.
public class restApiToCreateRecord {
private string cKey;
private string cSecret;
private string uName;
private string passwd;
public string instanceURL;
public restApiToCreateRecord () {
}
public class responseWrapper {
public string id;
public string access_token;
public string instance_url;
}
public responseWrapper getRequestToken() {
List < Store_Cred__mdt > connectionParam = [SELECT Id, MasterLabel, client_id__c, client_secret__c, username__c, password__c from Store_Cred__mdt];
if(connectionParam.size() >0){
cKey=connectionParam[0].client_id__c;
cSecret=connectionParam[0].client_secret__c;
uName=connectionParam[0].username__c;
passwd=connectionParam[0].password__c ;
}
System.debug('Store_Cred__mdt' + connectionParam);
string reqBody = 'grant_type=password&client_id=' + cKey + '&client_secret=' + cSecret + '&username=' + uName + '&password=' + passwd;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqBody);
req.setMethod('POST');
req.setEndpoint('https://login.salesforce.com/services/oauth2/token');
HttpResponse hresp = h.send(req);
responseWrapper wResp = (responseWrapper) JSON.deserialize(hresp.getBody(), responseWrapper.class);
system.debug('reqBody '+reqBody );
system.debug('wResp'+wResp );
system.debug('Instance url' + wResp.instance_url);
system.debug('session id' + wResp.access_token);
return wResp;
}
@future (callout=true)
public static void createAccount(Set < id > accIdSet) {
list < account > accList = new list < account > ();
String accToken;
String instanceUrl;
string responseBody;
string email = 'testemail123@gmail.com';
string phone = '123';
restApiToCreateRecord obj = new restApiToCreateRecord();
responseWrapper obj1= obj.getRequestToken();
accToken = obj1.access_token;
instanceUrl = obj1.instance_url;
string endPoint = instanceURL+'/services/apexrest/createAccountRecord';
system.debug('endPoint'+endPoint );
system.debug('access token' + accToken);
if (accToken != '') {
for (Account acc: [SELECT Id,Name,Description,Type,Industry from account where id in: accIdSet]) {
accList.add(acc);
}
Http h1 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization', 'Bearer ' + accToken);
req1.setHeader('Content-Type', 'application/json');
//req1.setHeader('Accept','application/json');
req1.setMethod('POST');
req1.setEndpoint(endPoint);
req1.setBody(JSON.serialize(accList));
HttpResponse hresp1 = h1.send(req1);
system.debug('hresp1'+hresp1 );
system.debug('hresp1.getStatusCode()'+hresp1.getStatusCode());
system.debug('hresp1.getBody()'+hresp1.getBody());
if (hresp1.getStatusCode() == 200) {
system.debug('Callout Success');
}
}
}
}
thank you.
ReplyDeleteI have an error in trigger variable does not exists:callWebserviceClass
ReplyDelete