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
Aman Sharma 79Aman Sharma 79 

Apex integration callout error

Hi, I am new to salsforce intigration. I want to make callout in another salesforce org to create contacts. Can anyone help me how i will do this?
I have already craeted the connected app in my org and get the client id,client and security token.
Best Answer chosen by Aman Sharma 79
Malika Pathak 9Malika Pathak 9
Hii Aman,
I have created a sample code to make a callout in another org where you have created your API.
You only need to replace the variables in my code.
Here is the code:-


public class CalloutExampleToCreateMultipleContacts {
    public static final String CONSUMER_Key = ''; // paste your client id here
    public static final String CONSUMER_SECRET = '';// paste the client secret here
    public static final String USERNAME = 'deepanshu.gupta@cloudanalogy.com'; // paste your your name here
    public static final String PASSWORD = '';//paste the salesforce password and the security token
    

    // pass any list of contact here and call this method to perform the callout
    public static void insertContact(List<Contact> contactObjList){
        AccessTokenWrapper accessTokenWrapperObj = genrateToken(); //this is the wrapper class of token generation 
        system.debug('--access token->'+accessTokenWrapperObj);
        if(accessTokenWrapperObj != null && accessTokenWrapperObj.access_token != null){
            String endpoint = 'https://cloudanalogy123-dev-ed.my.salesforce.com/services/apexrest/createContact22/';
            String requestBody = JSON.serialize(contactObjList);
            HTTP http = new HTTP();
            HttpRequest request = new HttpRequest();
            request.setBody(requestBody);
            request.setMethod('POST');
            request.setHeader('Authorization', 'Bearer '+accessTokenWrapperObj.access_token);
            request.setHeader('Content-type','application/json');
            request.setHeader('Accept','application/json');
            request.setEndpoint(endpoint);
            HttpResponse response = http.send(request);
            System.debug('Status code:'+response.getStatusCode()+'==>'+response.getBody());
            
            
        }
    }
    
    // function to get the access token
    public static AccessTokenWrapper genrateToken(){
        String requestBody = 'grant_type=password&client_id='+CONSUMER_Key+'&client_secret='+CONSUMER_SECRET+'&username='+USERNAME+'&password='+PASSWORD;
        String endpoint = 'https://login.salesforce.com/services/oauth2/token';
        HTTP http = new HTTP();
        HttpRequest request = new HttpRequest();
        request.setBody(requestBody);
        request.setMethod('POST');
        request.setEndpoint(endpoint);
        
        HttpResponse response = http.send(request);
        System.debug('response==>'+response.getBody()+'Status code:'+response.getStatusCode());
        if(response.getStatusCode() == 200){
            system.debug('');
            return (AccessTokenWrapper)System.JSON.deserialize(response.getBody(), AccessTokenWrapper.class);
        }else{
            return null;
        }
        
    }
    
    // wrapper class to store the access token values
    public class AccessTokenWrapper{
        public string access_token;
        public string instance_url;
        public string id;
        public string token_type;
        public string issued_at;
        public string signature;
        
        
    } 
}
 


if you having a problem then Ask me 
Please mark Best Answer If Your Problem Is Solved

thanks, Regards,
Malika Pathak    

All Answers

Malika Pathak 9Malika Pathak 9
Hii Aman,
I have created a sample code to make a callout in another org where you have created your API.
You only need to replace the variables in my code.
Here is the code:-


public class CalloutExampleToCreateMultipleContacts {
    public static final String CONSUMER_Key = ''; // paste your client id here
    public static final String CONSUMER_SECRET = '';// paste the client secret here
    public static final String USERNAME = 'deepanshu.gupta@cloudanalogy.com'; // paste your your name here
    public static final String PASSWORD = '';//paste the salesforce password and the security token
    

    // pass any list of contact here and call this method to perform the callout
    public static void insertContact(List<Contact> contactObjList){
        AccessTokenWrapper accessTokenWrapperObj = genrateToken(); //this is the wrapper class of token generation 
        system.debug('--access token->'+accessTokenWrapperObj);
        if(accessTokenWrapperObj != null && accessTokenWrapperObj.access_token != null){
            String endpoint = 'https://cloudanalogy123-dev-ed.my.salesforce.com/services/apexrest/createContact22/';
            String requestBody = JSON.serialize(contactObjList);
            HTTP http = new HTTP();
            HttpRequest request = new HttpRequest();
            request.setBody(requestBody);
            request.setMethod('POST');
            request.setHeader('Authorization', 'Bearer '+accessTokenWrapperObj.access_token);
            request.setHeader('Content-type','application/json');
            request.setHeader('Accept','application/json');
            request.setEndpoint(endpoint);
            HttpResponse response = http.send(request);
            System.debug('Status code:'+response.getStatusCode()+'==>'+response.getBody());
            
            
        }
    }
    
    // function to get the access token
    public static AccessTokenWrapper genrateToken(){
        String requestBody = 'grant_type=password&client_id='+CONSUMER_Key+'&client_secret='+CONSUMER_SECRET+'&username='+USERNAME+'&password='+PASSWORD;
        String endpoint = 'https://login.salesforce.com/services/oauth2/token';
        HTTP http = new HTTP();
        HttpRequest request = new HttpRequest();
        request.setBody(requestBody);
        request.setMethod('POST');
        request.setEndpoint(endpoint);
        
        HttpResponse response = http.send(request);
        System.debug('response==>'+response.getBody()+'Status code:'+response.getStatusCode());
        if(response.getStatusCode() == 200){
            system.debug('');
            return (AccessTokenWrapper)System.JSON.deserialize(response.getBody(), AccessTokenWrapper.class);
        }else{
            return null;
        }
        
    }
    
    // wrapper class to store the access token values
    public class AccessTokenWrapper{
        public string access_token;
        public string instance_url;
        public string id;
        public string token_type;
        public string issued_at;
        public string signature;
        
        
    } 
}
 


if you having a problem then Ask me 
Please mark Best Answer If Your Problem Is Solved

thanks, Regards,
Malika Pathak    
This was selected as the best answer
Aman Sharma 79Aman Sharma 79
Hii malika,
I have try this in my org and this is working well. Thanks for your help.
Baya AdamBaya Adam
MyArticles is a writer’s community where writers can share their stories all over the world. Signup and share your stories to all over the world. Follow your favorite writers, create groups, forums, chat, and much much more!