You need to sign in to do that
Don't have an account?
Management 20
Rest API Integration - Opportunity
Hi All,
I am looking to integrate 2 Salesforce Orgs using rest API - I am completely new to coding and apex - I have managed to integrate org 1 and org 2 for the account object but I now want to do the same for opportunity. Below is the code I have used for the account object which works - when creating an account in 1 org it automatically creates in org 2. How do i do the same for Opportunity?
APEX CLASS
public class SendAccount {
private final String clientId = '********';
private final String clientSecret = '****';
private final String username = '***';
private final String password = '*****';
public class deserializeResponse
{
public String id;
public String access_token;
}
public String ReturnAccessToken (SendAccount acount)
{
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');
req.setEndpoint('https://*****/services/oauth2/token');
HttpResponse res = h.send(req);
deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
return resp1.access_token;
}
@future(callout=true)
public static void createAccount(String accName, String accPhone, String accWebsite, String accType, String accIndustry, String accSource, String accDescription, String accNotes, String accAddress, String accId)
{
SendAccount acount = new SendAccount();
String accessToken = acount.ReturnAccessToken (acount);
if(accessToken != null)
{
String endPoint = 'https://********/services/data/v32.0/sobjects/Account/';
String jsonstr = '{"Name" : "' + accName + '","Phone" : "' + accPhone + '","Website" : "' + accWebsite + '","Type" : "' + accType + '","Industry" : "' + accIndustry + '","Source__c" : "' + accSource + '","Description__c" : "' + accDescription + '","Notes__c" : "' + accNotes + '","Address__c" : "' + accAddress + '"}';
Http h2 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization','Bearer ' + accessToken);
req1.setHeader('Content-Type','application/json');
req1.setHeader('accept','application/json');
req1.setBody(jsonstr);
req1.setMethod('POST');
req1.setEndpoint(endPoint);
HttpResponse res1 = h2.send(req1);
deserializeResponse resp2 = (deserializeResponse)JSON.deserialize(res1.getbody(),deserializeResponse.class);
Account a = [SELECT Phone, Website, Type, Industry, Source__c, Description__c, Notes__c, Address__c, Id FROM Account WHERE Id = :accId];
a.externalid__c = resp2.id;
update a;
}
}
}
APEX TRIGGER
trigger SendAccount on Account (after insert) {
for(Account a:Trigger.new){
SendAccount.createAccount(a.name, a.Phone, a.Website, a.Type, a.Industry, a.Source__c, a.Description__c, a.Notes__c, a.Address__c, a.Id);
}
}
I am looking to integrate 2 Salesforce Orgs using rest API - I am completely new to coding and apex - I have managed to integrate org 1 and org 2 for the account object but I now want to do the same for opportunity. Below is the code I have used for the account object which works - when creating an account in 1 org it automatically creates in org 2. How do i do the same for Opportunity?
APEX CLASS
public class SendAccount {
private final String clientId = '********';
private final String clientSecret = '****';
private final String username = '***';
private final String password = '*****';
public class deserializeResponse
{
public String id;
public String access_token;
}
public String ReturnAccessToken (SendAccount acount)
{
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');
req.setEndpoint('https://*****/services/oauth2/token');
HttpResponse res = h.send(req);
deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
return resp1.access_token;
}
@future(callout=true)
public static void createAccount(String accName, String accPhone, String accWebsite, String accType, String accIndustry, String accSource, String accDescription, String accNotes, String accAddress, String accId)
{
SendAccount acount = new SendAccount();
String accessToken = acount.ReturnAccessToken (acount);
if(accessToken != null)
{
String endPoint = 'https://********/services/data/v32.0/sobjects/Account/';
String jsonstr = '{"Name" : "' + accName + '","Phone" : "' + accPhone + '","Website" : "' + accWebsite + '","Type" : "' + accType + '","Industry" : "' + accIndustry + '","Source__c" : "' + accSource + '","Description__c" : "' + accDescription + '","Notes__c" : "' + accNotes + '","Address__c" : "' + accAddress + '"}';
Http h2 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization','Bearer ' + accessToken);
req1.setHeader('Content-Type','application/json');
req1.setHeader('accept','application/json');
req1.setBody(jsonstr);
req1.setMethod('POST');
req1.setEndpoint(endPoint);
HttpResponse res1 = h2.send(req1);
deserializeResponse resp2 = (deserializeResponse)JSON.deserialize(res1.getbody(),deserializeResponse.class);
Account a = [SELECT Phone, Website, Type, Industry, Source__c, Description__c, Notes__c, Address__c, Id FROM Account WHERE Id = :accId];
a.externalid__c = resp2.id;
update a;
}
}
}
APEX TRIGGER
trigger SendAccount on Account (after insert) {
for(Account a:Trigger.new){
SendAccount.createAccount(a.name, a.Phone, a.Website, a.Type, a.Industry, a.Source__c, a.Description__c, a.Notes__c, a.Address__c, a.Id);
}
}
Hey there, welcome to coding. you are learning the right way. Use this code and learn what you can. Simply replace all the places where the term Account is used, with Opportunity, the replace all the account fields with opportunity fields. Be very careful not to mess with the format, if it saves at the end you did it right. If you get errors, you must have a slight copy and paste error or a field name error, but the code writer error compiler should lead you in the right direction.
If you post your go at changing the code, I will have a look at it and see if I can spot any issues.
All the best
Mikie
E.G.
Account a = [SELECT Phone, Website, Type, Industry, Source__c, Description__c, Notes__c, Address__c, Id FROM Account WHERE Id = :accId];
becomes
Opportunity o = [SELECT Id, Name, *add other opportunity fields* FROM Opportunity WHERE Id = :opId]; //Dont forget to change where the accid is created and set and call it opId instead.
thanks for your help with this!
I have changed the code which you can see below and there arent any errors but it wont seem to push the data over like the account does.
APEX CLASS - OPPORTUNITY
public class SendOpportunity {
private final String clientId = '*******';
private final String clientSecret = '*******';
private final String username = '******';
private final String password = '*****';
public class deserializeResponse
{
public String id;
public String access_token;
}
public String ReturnAccessToken (SendOpportunity acount)
{
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');
req.setEndpoint('https://*****/services/oauth2/token');
HttpResponse res = h.send(req);
deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
return resp1.access_token;
}
@future(callout=true)
public static void createOpportunity(String opName, String opStageName, Date opCloseDate, String opId)
{
SendOpportunity acount = new SendOpportunity();
String accessToken = acount.ReturnAccessToken (acount);
if(accessToken != null)
{
String endPoint = 'https://mauvegroup.my.data/v32.0/sobjects/Opportunity/';
String jsonstr = '{"Name" : "' + opName + '","StageName" : "' + opStageName + '","CloseDate" : "' + opCloseDate + '"}';
Http h2 = new Http();
HttpRequest req1 = new HttpRequest();
req1.setHeader('Authorization','Bearer ' + accessToken);
req1.setHeader('Content-Type','application/json');
req1.setHeader('accept','application/json');
req1.setBody(jsonstr);
req1.setMethod('POST');
req1.setEndpoint(endPoint);
HttpResponse res1 = h2.send(req1);
deserializeResponse resp2 = (deserializeResponse)JSON.deserialize(res1.getbody(),deserializeResponse.class);
opportunity o = [SELECT StageName, CloseDate, Id FROM opportunity WHERE Id = :opId];
o.Opportunity_ExternalID__c = resp2.id;
update o;
}
}
}
APEX TRIGGER - OPPORTUNITY
trigger SendOpportunity on Opportunity (after insert) {
for(Opportunity o:Trigger.new){
SendOpportunity.createOpportunity(o.Name, o.StageName, o.CloseDate, o.Id);
}
}