+ Start a Discussion
Rocks_SFDCRocks_SFDC 

Too many callouts: 101 Error in REST API call

Hi Team,

We built the code where it will fetch the Metadata Components(Like Custom Fields, Workflows, Validation Rules) based on the Last Modified Date upon the button click on detail page.

But we are facing the following error:
Too many callouts: 101
Error is in expression '{!SaveRecord}' in component <apex:page> in page metadataretrieval3: Class.MetadataRetrieval3_Controller

MetadataRetrieval3_Controller code:

Public PageReference SaveRecord(){
 if(metaSync.selected_Metadata_Components__c.contains('Validation Rules')){
        String vId, ValMetadata='<?xml version="1.0" encoding="UTF-8"?>'+'\n'+'<Package xmlns="http://soap.sforce.com/2006/04/metadata">'+'\n'+'<types>'+'\n';
        Set<Id> ValRuleIds=new Set<Id>();
        Set<String> ValNames=new Set<String>();
        String text, text1;
        String valName;
        System.JSONToken token;
        SFDCToken tk = generateSFDCToken();
        List<String> ObjList=metaSync.selected_Objects__c.split(';');
        for(String ObjName: ObjList){
        Http h = new Http();
        HttpRequest req = new HttpRequest(); 
        req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
            String Inturl=tk.instance_url+'/services/data/v36.0/tooling/query?q=Select+Id+From+ValidationRule+where+LastModifiedDate>';
            String startDate=Inturl+String.Valueof(metaSync.start_Date__c).substringBeforeLast(' ')+'T00:00:00.000Z'+'+'+'AND'+'+';
            String endate=startDate+'LastModifiedDate<'+String.Valueof(metaSync.End_Date__c).substringBeforeLast(' ')+'T23:59:59.000Z'+'+'+'AND'+'+';
            String FullURL=endate+'EntityDefinition.DeveloperName=\'' +String.escapeSingleQuotes(objName)+'\'';   
       req.setEndpoint(FullURL);
        req.setMethod('GET'); 
        req.setHeader('Authorization', 'OAuth ' + tk.access_token);
        req.setTimeout(60000);
        HttpResponse res= new HttpResponse();
        res.setHeader('Content-Type', 'application/json');  
        res.setStatusCode(200);
        res = h.send(req); 
         if(res.getStatusCode() == 200)
           {
                JSONParser parser = JSON.createParser(res.getBody());   
                    while((token = parser.nextToken()) != null) {
                           if ((token = parser.getCurrentToken()) != JSONToken.END_OBJECT) {
                            text = parser.getText();
                           if (token == JSONToken.FIELD_Name && text == 'Id') {
                                token=parser.nextToken();
                                text1=parser.getText();
                                ValRuleIds.add(text1);
                             }
                 }
        }
       }
     }
        for(String valId : ValRuleIds){
        SFDCToken tk1 = generateSFDCToken();
          
            Http h1 = new Http();
            HttpRequest req1 = new HttpRequest(); 
            req1.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionID());
            String url1=tk1.instance_url+'/services/data/v36.0/tooling/query?q=Select+FullName+From+ValidationRule+where+Id=\'' +String.escapeSingleQuotes(valId)+'\'';
            req1.setEndpoint(url1); 
            req1.setMethod('GET'); 
            req1.setHeader('Authorization', 'OAuth ' + tk1.access_token);
            req1.setTimeout(60000);
            HttpResponse res1= new HttpResponse();
            res1.setHeader('Content-Type', 'application/json');  
            res1.setStatusCode(200);
            res1 = h1.send(req1); 
            if(res1.getStatusCode() == 200)
                {
                    JSONParser parser1 = JSON.createParser(res1.getBody());   
                    while((token = parser1.nextToken()) != null) {
                    if ((token = parser1.getCurrentToken()) != JSONToken.END_OBJECT) {
                        text = parser1.getText();
                        if (token == JSONToken.FIELD_Name && text == 'FullName') {
                            token=parser1.nextToken();
                            valName=parser1.getText();
                           ValMetadata=ValMetadata+'<members>'+escapeSpecialChars(valName)+'</members>'+'\n';
                                    
                         }
                     }
                 }
                 
              }
          
          }
            ValMetadata2=ValMetadata+'<name>ValidationRule</name>'+'\n'+'</types>'+'\n'+'<version>34.0</version>'+'\n'+'</Package>';
      
      }
Update metaSync;
 if(ValMetadata2!=null){
            Attachment attachment=new Attachment();
            attachment.Body = Blob.valueof(ValMetadata2);
            attachment.Name = String.valueOf('Validation Rules-Metadata.txt');
            attachment.ParentId =metaSync.id;
            insert attachment; 
    }
}      

Could anyone please look on it and let us know the best approch for this.

Thanks,
Anil

 
Rocks_SFDCRocks_SFDC
Hi Team,

Could anyone please look on this and let me know best approch to resolve this issue.

Thanks,
Anil