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
Nitin sharma 425Nitin sharma 425 

Callouts and Integration

Hi All, Below given code is part of the code which I have used to create and update contact records in another salesforce Org.It between salesforce to salesforce and I am able to create and update records in another org.However,After I made a callout I debug the response and tried to pull out External Id field from it...I wanted to update External ID field in Org1 with the id of the contact created in Org2 So when I used system.debug to see what's in the response. I could see External Id field with a value in it but after I looped through the returned contacts I only saw null in the External Id....Not sure what's going on.....How do I pull out external Id field from the response and update the contact field with the Id of the contact from org 2 as as External Id field in org 1?....I know I can use map to map external id with the Id of contact in map from Org1 and then update the contact field with the Id of the contact from org2...If somebody can help me in telling why I am not able to see External id field in reponse
System.debug('The response is********************'+response.getbody());

The above line has external Id value

The below line give me null

system.debug('The contact variable Conrec has the following values'+conReturned.Contact_Id_As_ExternalId__c);

    if(accessTokenWrapperObj != null)
    {
        String endpoint = 'https://min72-dev-ed.my.salesforce.com/services/apexrest/createContacts/';
        String requestBody = JSON.serialize(ContactList);
        HTTP http = new HTTP();
        HttpRequest request = new HttpRequest();
        request.setBody(requestBody);
        request.setMethod('POST');
        request.setHeader('Authorization', 'Bearer '+accessTokenWrapperObj);
        request.setHeader('Content-type','application/json');
        request.setHeader('Accept','application/json');
        request.setEndpoint(endpoint);
        HttpResponse response = http.send(request);
        System.debug('The response is********************'+response.getbody());
        list<contact>updatecontactlist=new list<contact>();
        list<contact> con=(List<contact>) System.JSON.deserialize(response.getbody(),List<contact>.class);

        for(contact conReturned:con)
        {

         system.debug('The contact variable Conrec has the following values'+conReturned.Contact_Id_As_ExternalId__c);
          system.debug('The contact variable Conrec has the following values'+conReturned.id);
            contact c=new contact();
            //c=MapofContact.get(conReturned.Contact_Id_As_ExternalId__c);
            c.Contact_Id_As_ExternalId__c=conReturned.Id;
            //updatecontactlist.add(c);
        }
        //update updatecontactlist;
        //System.debug('The updated record is'+updatecontactlist);
        //System.debug('Status code:'+response.getStatusCode()+'==>'+response.getBody());

    }
}
}




 
Ajay Patel 54Ajay Patel 54

hi  Nitin 

hope these will help you 


                
                if( httpRes.getStatusCode()==200){
                    Map<String,object> data1 = (Map<String, object>) JSON.deserializeUntyped(httpRes.getBody());
                    system.debug('mapToken'+data1);
                    if(!data1.isEmpty()){
                        contact  c = new contact ();
                        c.Id = conReturned.Id;
                        system.debug('--idResponse--'+data1.get('id'));
                        c.Contact_Id_As_ExternalId__c =string.valueof(data1.get('id'));
                        system.debug('id'+c.Contact_Id_As_ExternalId__c);
                        toBeUpdated.add(c);  
                    }
                }
                system.debug('---Update Id--'+toBeUpdated);
                if(!toBeUpdated.isEmpty() && toBeUpdated.size()>0){
                    update toBeUpdated;
                }