function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tulasiram chtulasiram 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:
  1. public class deserializeResponse{
  2.         public string id;
  3.         public string access_token;
  4.     }
  5.     public string ReturnAccessToken(sendAccountsToWebservice account){
  6.         system.debug('entered accestoken methd');
  7.       String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
  8.       Http h = new Http();
  9.       HttpRequest req = new HttpRequest();
  10.       req.setBody(reqbody);
  11.       req.setMethod('POST');
  12.       req.setEndpoint('https://ap5.salesforce.com/services/oauth2/token');
  13.  
  14.       HttpResponse res = h.send(req);
  15.  
  16.       deserializeResponse resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
  17.  
  18.       return resp1.access_token;
  19.     }
  20.     @future(callout=true)
  21.     public static void makePostCallout(set<id> accIds){
  22.         
  23.           String str;
  24.         List<Account> accList = [select Name,Website from Account where Id IN :accIds];
  25.        
  26.         sendAccountsToWebservice acount1 = new sendAccountsToWebservice();
  27.         String accesstoken;
  28.         accesstoken = acount1.ReturnAccessToken(acount1);
  29.         
  30.         JSONGenerator gen = JSON.createGenerator(true);
  31.             gen.writeStartObject();  
  32.             gen.writeFieldName('Accounts');
  33.  
  34.             gen.writeStartArray();
  35.             for(Account a : accList)
  36.             {
  37.                 gen.writeStartObject();
  38.                     gen.writeStringField('Name', a.Name);
  39.                     gen.writeStringField('Website', a.Website);
  40.                     gen.writeStringField('Id', a.id);
  41.                 gen.writeEndObject();
  42.     
  43.             }
  44.             
  45.             gen.writeEndArray();
  46.          gen.writeEndObject();
  47.  
  48. str = gen.getAsString();
  49.  
  50.   
  51.         //json ends
  52.         if(accesstoken!=null)
  53.         {
  54.         system.debug('AT not null');
  55.             
  56.             Http http = new Http();
  57.             HttpRequest request = new HttpRequest();
  58.             request.setEndpoint('https://ap5.salesforce.com/services/apexrest/createAccounts/');
  59.             
  60.             request.setMethod('POST');
  61.             request.setHeader('Authorization','Bearer '+accesstoken);
  62.             request.setHeader('Content-Type','application/json');
  63.  
  64.             request.setHeader('accept','application/json');
  65.  
  66.             request.setBody(str);
  67.             
  68.             system.debug('JS'+str);
  69.             
  70.              system.debug('REQ BODY'+request.getBody());
  71.             
  72.            
  73.              HttpResponse response = http.send(request);
  74.                         
  75.                         system.debug('RESPONSE BODY'+response.getBody());
  76.                         Map<String,String> Org1Org2Map = new Map<String,String>();
  77.                     
  78.              // myVar.bool = false;
  79.              update accList; 
  80.                        
  81.         }
  82.     }   
I used username and password for establishing the connection between two orgs in the above webservice:

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!
Sukanya BanekarSukanya Banekar
Hi,
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