• Mayank Raj 22
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 1
    Replies
I am new to Integration. I am trying to update a record in one environment(Org - A) and reflect the same value in another environment (Org - B). Upon checking the debug log I get the following error.
+++Http Response[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]

Could you please point in the right direction.
Code on Org - A

public class restApiToUpdateRecords {
    
    String consumerKey = 'XXXXXXX';
    String consumerSecret = 'XXXX';
    String userName = 'XXX';
    String passwd = 'XXX';
    
    public class wrapperClass{
        public String identifier;
        public String valueToPut;
    }
    
    public class responseWrapper{
        public String id;
        public String access_token;
        public String instance_url;
        
    }
    
    public String getRequestToken(){
        String reqBody = 'grant_type=password&client_id=' + consumerKey + '&client_secret=' + consumerSecret + '&username=' + userName + '&password=' + passwd;
        Http http = new Http();
        HttpRequest request = new HttpRequest();
            request.setBody(reqBody);
            request.setMethod('POST');
            request.setEndpoint('https://login.salesforce.com/services/oauth2/token');
        HttpResponse response = http.send(request);
        responseWrapper jsonResponse = (responseWrapper)JSON.deserialize(response.getBody(), responseWrapper.Class);
        return jsonResponse.access_token;
    }
    
    @future(callout = True)
    public static void updateRecord(List<Id> recList){
        for(Rest_Integration__c rec: [SELECT Id,Identifier__c,Value_to_Lightning_Env__c FROM Rest_Integration__c WHERE ID IN: recList]){
        wrapperClass wc = new wrapperClass();
            wc.identifier = rec.Identifier__c;
            wc.valueToPut = rec.Value_to_Lightning_Env__c;
        restApiToUpdateRecords obj = new restApiToUpdateRecords();
        String accessToken = obj.getRequestToken();
        if(accessToken != NULL){
            System.debug('++++Getting the Access Token');
            Http http = new Http();
            HttpRequest request = new HttpRequest();
                request.setEndpoint('https://mayankraj1212-dev-ed.my.salesforce.com/services/apexrest/updateRecords/');
                request.setBody(JSON.serialize(wc));
                request.setHeader('Authorization', 'Bearer ' + accessToken);
                request.setHeader('Content-Type', 'application/json');
                request.setMethod('PUT');
            HttpResponse response = http.send(request);
            System.debug('+++Http Response'+response.getBody());
        }
        
        }
    }

}
I am unable to see the scroll bar on the right hand side of the page when I click on Edit button in Lightning Experience. The Edit button is the standard one.User-added image
IF( Format__c = 'qw ert',
    CASE(
        IF(INCLUDES(Local__r.XYZ__r.ABC__c, 'ABC 10'),'1','')+
        IF(INCLUDES(Local__r.XYZ__r.ABC__c, 'New Vision'),'2','')+
        IF(INCLUDES(Local__r.XYZ__r.ABC__c, 'ABC 10+'),'3','')+
        IF(INCLUDES(Front__r.ABC__c, 'ABC 10'),'4','')+
        IF(INCLUDES(Front__r.ABC__c, 'New Vision'),'5','')+
        IF(INCLUDES(Front__r.ABC__c, 'ABC 10+'),'6',''),
            '25','New Vision',
            '125','New Vision',
            '1235','New Vision',
            '245','New Vision',
            '2456','New Vision',
            '145','ABC 10',
            '1345','ABC 10',
            '1246','ABC 10',
            '1456','ABC 10',
            '1245','New Vision;ABC 10',
            '12345','New Vision;ABC 10',
            '12456','New Vision;ABC 10',
            '146','ABC 10;ABC 10+',
            '1346','ABC 10;ABC 10+',
            '13456','ABC 10;ABC 10+',
            '12346','New Vision;ABC 10;ABC 10+',
            '123456','New Vision;ABC 10;ABC 10+',
    NULL),
NULL)
The above formula gives an error stating " Compiled formula is too big to execute". If I check it using external tool, the count comes down to way within the character limit. Local__r.XYZ__r.ABC__c as well as Front__r.ABC__c points to a Multi Select picklist field. 
Controller:

/* This class is used to display a list of contacts from a rest api (https://api.androidhive.info/contacts/) to a VF Page and also has a checkbox to save the desired Contact wihtin the Org*/
public class restIntegrationPageController {
    
    List<Contact> conList{get;set;}
    public class wrapperforJSON{
        public string id{get;set;}
        public string name{get;set;}
        public String email{get;set;}
        public string address{get;set;}
        public string gender{get;set;}
        public string mobile{get;set;}
        public boolean isChecked{get;set;}
    }
    public List<wrapperforJSON> rList{get;set;}
    public wrapperforJSON singleRec{get;set;}
    public void  Method1(){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndPoint('https://api.androidhive.info/contacts/');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        List<String> idList = new List<String>();
        List<String> nameList = new List<String>();
        List<String> emailList = new List<String>();
        List<String> addressList = new List<String>();
        List<String> genderList = new List<String>();
        List<String> mobileList = new List<String>();
        JSONParser parser = JSON.createParser(response.getBody());
        while(parser.nextToken() != null){
            if(parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'id'){
                parser.nextToken();
                idList.add(parser.getText());
            }
            if(parser.getCurrentToken() == JSONTOKEN.FIELD_NAME && parser.getText() == 'Name'){
                parser.nextToken();
                nameList.add(parser.getText());
            }
            if(parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'Email'){
                parser.nextToken();
                emailList.add(parser.getText());
            }
            if(parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'Address'){
                parser.nextToken();
                addressList.add(parser.getText());
            }
            if(parser.getCurrentToken() == JSONToken.FIELD_Name && parser.getText() == 'Gender'){
                parser.nextToken();
                genderList.add(parser.getText());
            }
            if(parser.getCurrentToken() == JSONToken.FielD_Name && parser.getText() == 'Mobile'){
                parser.nextToken();
                mobileList.add(parser.getText());
            }
         
        }
       rList = new List<wrapperforJSON>();
       for(Integer i=0;i<idList.size();i++){
           singleRec = new wrapperforJSON();
               singleRec.id = idList[i];
               singleRec.name = nameList[i];
               singleRec.email = emailList[i];
               singleRec.Address = addressList[i];
               singleRec.Gender = genderList[i];
               singleRec.Mobile = mobileList[i];
               singleRec.isChecked = False;
           rList.add(singleRec);
               
       }    
    }
}


VF:

<apex:page controller="restIntegrationPageController">
<apex:form >
<apex:pageBlock title="Rest Integration Example 1">
    <apex:pageBlockTable value="{!rList}" var="r">
        <apex:column value="{!r.Id}" headerValue="Id"/>
    </apex:pageBlockTable>
</apex:pageBlock>

</apex:form>
</apex:page>
Controller:

/* This class is used to display a list of contacts from a rest api (https://api.androidhive.info/contacts/) to a VF Page and also has a checkbox to save the desired Contact wihtin the Org*/
public class restIntegrationPageController {
    
    List<Contact> conList{get;set;}
    public class wrapperforJSON{
        public string id{get;set;}
        public string name{get;set;}
        public String email{get;set;}
        public string address{get;set;}
        public string gender{get;set;}
        public string mobile{get;set;}
        public boolean isChecked{get;set;}
    }
    public List<wrapperforJSON> rList{get;set;}
    public wrapperforJSON singleRec{get;set;}
    public void  Method1(){
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndPoint('https://api.androidhive.info/contacts/');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        List<String> idList = new List<String>();
        List<String> nameList = new List<String>();
        List<String> emailList = new List<String>();
        List<String> addressList = new List<String>();
        List<String> genderList = new List<String>();
        List<String> mobileList = new List<String>();
        JSONParser parser = JSON.createParser(response.getBody());
        while(parser.nextToken() != null){
            if(parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'id'){
                parser.nextToken();
                idList.add(parser.getText());
            }
            if(parser.getCurrentToken() == JSONTOKEN.FIELD_NAME && parser.getText() == 'Name'){
                parser.nextToken();
                nameList.add(parser.getText());
            }
            if(parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'Email'){
                parser.nextToken();
                emailList.add(parser.getText());
            }
            if(parser.getCurrentToken() == JSONToken.FIELD_NAME && parser.getText() == 'Address'){
                parser.nextToken();
                addressList.add(parser.getText());
            }
            if(parser.getCurrentToken() == JSONToken.FIELD_Name && parser.getText() == 'Gender'){
                parser.nextToken();
                genderList.add(parser.getText());
            }
            if(parser.getCurrentToken() == JSONToken.FielD_Name && parser.getText() == 'Mobile'){
                parser.nextToken();
                mobileList.add(parser.getText());
            }
         
        }
       rList = new List<wrapperforJSON>();
       for(Integer i=0;i<idList.size();i++){
           singleRec = new wrapperforJSON();
               singleRec.id = idList[i];
               singleRec.name = nameList[i];
               singleRec.email = emailList[i];
               singleRec.Address = addressList[i];
               singleRec.Gender = genderList[i];
               singleRec.Mobile = mobileList[i];
               singleRec.isChecked = False;
           rList.add(singleRec);
               
       }    
    }
}


VF:

<apex:page controller="restIntegrationPageController">
<apex:form >
<apex:pageBlock title="Rest Integration Example 1">
    <apex:pageBlockTable value="{!rList}" var="r">
        <apex:column value="{!r.Id}" headerValue="Id"/>
    </apex:pageBlockTable>
</apex:pageBlock>

</apex:form>
</apex:page>