You need to sign in to do that
Don't have an account?
tulasiram ch
get response back to the Server in Webservices integration using REST API
i used below code for--- when i was created a record1 in Org1, it will automatically created in Org2.
org1:
1. i used trigger to pass new record id's to the webservice calss below.
2.webservice class:
Org2:
@RestResource(urlmapping='/createAccounts/*')
global with sharing class AccountsManager {
@HttpPost global static void createAccounts(List<Account> Accounts) {
List<Id> accIds = new List<Id>();
List<Account> accList = new List<Account>(); .
for(Account a1 : Accounts) {
Account a = new Account();
a.Name = a1.Name;
a.Website = a1.Website;
a.Org1__c = a1.Id;
accList.add(a); }
insert accList;
Map<String,String> Org1Org2Map = new Map<String,String>();
for(Account a : accList) {
Org1Org2Map.put(a.Org1__c,a.id);
}
system.debug('ORGS MAP'+Org1Org2Map);
RestContext.response.responsebody = Blob.valueOf(JSON.serialize(Org1Org2Map));
}
}
i want to get back the response to org1 when the record inserted in Org2, based on that response i want to update a field on the object in the org1.
how can i achieve that. and how can i write test class for this. i refered some docs for writing test classes for web services but i didn't understand those. please help i am new to integration. Thank you!
org1:
1. i used trigger to pass new record id's to the webservice calss below.
2.webservice class:
- public class deserializeResponse{
- public string id;
- public string access_token;
- }
- public string ReturnAccessToken(sendAccountsToWebservice account){
- system.debug('entered accestoken methd');
- 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://ap5.salesforce.com/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 makePostCallout(set<id> accIds){
- String str;
- List<Account> accList = [select Name,Website from Account where Id IN :accIds];
- sendAccountsToWebservice acount1 = new sendAccountsToWebservice();
- String accesstoken;
- accesstoken = acount1.ReturnAccessToken(acount1);
- JSONGenerator gen = JSON.createGenerator(true);
- gen.writeStartObject();
- gen.writeFieldName('Accounts');
- gen.writeStartArray();
- for(Account a : accList)
- {
- gen.writeStartObject();
- gen.writeStringField('Name', a.Name);
- gen.writeStringField('Website', a.Website);
- gen.writeStringField('Id', a.id);
- gen.writeEndObject();
- }
- gen.writeEndArray();
- gen.writeEndObject();
- str = gen.getAsString();
- //json ends
- if(accesstoken!=null)
- {
- system.debug('AT not null');
- Http http = new Http();
- HttpRequest request = new HttpRequest();
- request.setEndpoint('https://ap5.salesforce.com/services/apexrest/createAccounts/');
- request.setMethod('POST');
- request.setHeader('Authorization','Bearer '+accesstoken);
- request.setHeader('Content-Type','application/json');
- request.setHeader('accept','application/json');
- request.setBody(str);
- system.debug('JS'+str);
- system.debug('REQ BODY'+request.getBody());
- HttpResponse response = http.send(request);
- system.debug('RESPONSE BODY'+response.getBody());
- Map<String,String> Org1Org2Map = new Map<String,String>();
- // myVar.bool = false;
- update accList;
- }
- }
Org2:
@RestResource(urlmapping='/createAccounts/*')
global with sharing class AccountsManager {
@HttpPost global static void createAccounts(List<Account> Accounts) {
List<Id> accIds = new List<Id>();
List<Account> accList = new List<Account>(); .
for(Account a1 : Accounts) {
Account a = new Account();
a.Name = a1.Name;
a.Website = a1.Website;
a.Org1__c = a1.Id;
accList.add(a); }
insert accList;
Map<String,String> Org1Org2Map = new Map<String,String>();
for(Account a : accList) {
Org1Org2Map.put(a.Org1__c,a.id);
}
system.debug('ORGS MAP'+Org1Org2Map);
RestContext.response.responsebody = Blob.valueOf(JSON.serialize(Org1Org2Map));
}
}
i want to get back the response to org1 when the record inserted in Org2, based on that response i want to update a field on the object in the org1.
how can i achieve that. and how can i write test class for this. i refered some docs for writing test classes for web services but i didn't understand those. please help i am new to integration. Thank you!
To send the response about the insertion of the record you can put insert statement in try catch block in org2 if and after insert statement set some string s= ' record inserted' else in catch block set string s= 'failure'
and send the string in response body.
Let me know if this works.
Thanks,
Sukanya Banekar