Search one salesforce organization records from another salesforce organization using Rest API callouts

Access Token Snippet :


Public class Org_GlobalAccessToken_Cls {
    Public Static String clientId = EncodingUtil.urlEncode('3MVG9I5UQ_0k_hTm9p9qVC3ruJI49xkzjVd51JkCiNN7tPr.qUcphB8KZFvrQGm9XnO6r5PX6GJKyG_F9rRmK', 'UTF-8');
    Public Static String clientSecret = EncodingUtil.urlEncode('387843950858359729', 'UTF-8');
    Public Static String username = EncodingUtil.urlEncode('sourceorganization@yopmail.com', 'UTF-8');
    Public Static String password = EncodingUtil.urlEncode('Mardal786*', 'UTF-8');
    Public Static String reqbody = 'grant_type=password&client_id=' + clientId + '&client_secret=' + clientSecret + '&username=' + username + '&password=' + password;
    public static string cmdAccessToken() {
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setBody(reqbody);
        req.setMethod('POST');
        req.setEndpoint('https://eu9.salesforce.com/services/oauth2/token');
        HttpResponse res = h.send(req);
        if (res.getstatuscode() == 200) {
            GrantAccessToken accessToken = (GrantAccessToken) JSON.deserialize(res.getbody(),GrantAccessToken.class);
            system.debug('**********'+res.getbody());
            if (accessToken.access_token != null) {
                system.debug('**********'+accessToken.access_token);
                return accessToken.access_token;
            } else {
                return null;
            }
        }
        return null;
    } 
    public class GrantAccessToken {
        public String id {
            get;
            set;
        }
        public String issued_at {
            get;
            set;
        }
        public String instance_url {
            get;
            set;
        }
        public String signature {
            get;
            set;
        }
        public String access_token {
            get;
            set;
        }
    }   
}

Main apex logic :

Public class GlobalAccessToken_Cls {
   
    Public String acctId {
        get;
        set;
    }
    public void cmdListContact() {
        callgetContact(acctId);
    }
    public List <Contact> lstContact {
        get;
        set;
    }
    public String jsonString {get;set;}
    public List < Contact > callgetContact(String accId) {
        lstContact = new List <Contact>();
        String accessToken = Org_GlobalAccessToken_Cls.cmdAccessToken();  
        system.debug('~~~~~~~~~~~~~~~'+accessToken);
        if (accessToken != null) {
            system.debug('~~~~~~~~~~~~~~~'+accessToken);
            String endPoint = 'https://eu9.salesforce.com/services/apexrest/v1/getContacts/' + accId;
            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.setMethod('GET');
            req1.setEndpoint(endPoint);
            HttpResponse res1 = h2.send(req1);
            system.debug('~~~~~~~~~~~~~~~'+res1.getBody());
            String trimmedResponse = res1.getBody().unescapeCsv().remove('\\');
            system.debug('@@@RESPONSE@@' + trimmedResponse);
            JSONParser parser = JSON.createParser(res1.getBody());
            set < Contact > contList = new set < Contact > ();
            while (parser.nextToken() != null) {
                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)) {
                    Contact cont;
                    if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Id')) {
                        parser.nextToken();
                        string sId = parser.getText();
                        cont = new Contact();
                        cont.Id = sId;
                        parser.nextToken();
                        if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText() == 'Name')) {
                            parser.nextToken();
                            String sName = parser.getText();
                            cont.LastName = sName;
                        }
                        parser.nextToken();
                        if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText() == 'Email')) {
                            parser.nextToken();
                            string sEmail = parser.getText();
                            cont.Email = sEmail;
                        }
                    }
                    contList.add(cont);
                }
                contList.remove(null);
            }
            lstContact.AddAll(contList);
            system.debug('ContList@@@@' + Json.serialize(lstContact));
            jsonString = Json.serialize(lstContact);
        }
        return LstContact;
    }
}

Visualforce Page :

<apex:page controller="GlobalAccessToken_Cls">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:outputPanel >
                <apex:outputLabel value="Account ID"/>&nbsp;&nbsp;&nbsp;&nbsp;
                <apex:inputText value="{!acctId}" label="AccountID"/>&nbsp;&nbsp;&nbsp;&nbsp;
                <apex:commandButton value="Go" action="{!cmdListContact}"/>
                </apex:outputPanel>
            </apex:pageBlockSection>
            {!jsonString}
            <apex:pageBlockTable value="{!lstContact}" var="c">
               <apex:column value="{!c.Id}"/> 
               <apex:column value="{!c.LastName}"/>
               <apex:column value="{!c.Email}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Screenshot For Reference :


No comments:

Featured

What is Cryptography in salesforce and what are all the algorithms provided by them ?

A). It is a security protocal between two systems. Lets say we are integration two systems without any encrytion mechanism then hackers wil...

Popular