+ Start a Discussion
Will Jones 18Will Jones 18 

When doing a call out based on a bulk update, not all information made it to external site

I need to update all the opportunities in my sandbox to test the bulk capability of sending the information to our custom portal via an HTTP Call Out.

Individually it works fine. However when I did a bulk update of 2800+ opportunities via DemandTools, only a hand full went over to our portal. My callout receives a set of IDs from a trigger (which is also bulkified I think). It should then process all of these IDs.

Anyone know if my trigger or callout is flawed or if it was a Salesforce limit hit?

Trigger
trigger CustomPortalOpp on Opportunity (after insert, after update) {
    
List<opportunity> oppList =  new List<opportunity>();

//prevents an infinite loop
if(Recursion.objectRecursion)
    return;
Recursion.objectRecursion = TRUE;

//Create list of opportunity
List<opportunity> opp_list = [SELECT Id, AccountId, Account_Manager_ID__c,  Client_Total__c, Event_Address__c,
                              Event_Consultant__c, Event_Date__c, Event_Start_Time__c, Event_End_Time__c,
                              Event_Status__c, Event_Type__c, Invite_Custom_Fields__c,
                              Invite_Headline__c, Invite_Request_Form__c, Invite_Yes_Limit__c,
                              Market__c, Name, Send_from_Email__c, Send_from_Name__c,
                              SG_Invite_ID__c, StageName, Time_Zone__c, Venue_Name__c FROM Opportunity WHERE Id IN: Trigger.newmap.keyset()];

   for (Opportunity o : opp_list) {
            opplist.add(o);
    }
    
    Set<Id> opptyIds = (new Map<Id,Opportunity>(oppList)).keySet();
    
    //process updates
    if (opp_list.isEmpty()==FALSE){
        
        if(!Test.isRunningTest()){
               IF(System.IsBatch() == false && System.isFuture() == false){ 
                    MySforceHttpCallOut.putDataToRemote_Opportunity(opptyIds); //call a method with @future annotation

               }

    }  
    
}
}


Apex Class - Call Out
public static void putDataToRemote_Opportunity(Set<Id> opptyIds){

String requestform;   
    
//Create list of opportunity
List<opportunity> opp_list = [SELECT Id, AccountId, Account_Manager_ID__c,  Client_Total__c, Event_Address__c,
                              Event_Consultant__c, Event_Date__c, Event_Start_Time__c, Event_End_Time__c,
                              Event_Status__c, Event_Type__c, Invite_Custom_Fields__c,
                              Invite_Headline__c, Invite_Request_Form__c, Invite_Yes_Limit__c,
                              Market__c, Name, Send_from_Email__c, Send_from_Name__c,
                              SG_Invite_ID__c, StageName, Time_Zone__c, Venue_Name__c 
                              FROM Opportunity WHERE Id IN: opptyIds];

   for (Opportunity o : opp_list) {
        String Stg = 'https://**********.com/api/v1/page2/' +o.Id+ '?token=123';
        String QA = 'https://**********.com/api/v1/page2/' +o.Id+ '?token=123';
        String PRD = 'https://**********.com/api/v1/page2/' +o.Id+ '?token=123';
             
             //Prepare https req
             httpRequest req = new httpRequest(); 
             req.setEndpoint(QA);
             //req.setHeader('X-HTTP-Method-Override', 'PATCH');
             req.setHeader('Content-Type','application/json');
             req.setMethod('PUT');
             req.setCompressed(false);
            
             if(o.Event_Start_Time__c == null)
                 o.Event_Start_Time__c = '';
             
             if(o.Invite_Custom_Fields__c == null)
                 o.Invite_Custom_Fields__c = '';
       
             if(o.Invite_Headline__c == null)
                 o.Invite_Headline__c = '';
       
            if(o.Invite_Yes_Limit__c == null)
                 o.Invite_Yes_Limit__c = 0;
       
             if(o.Send_From_Email__c == null)
                 o.Send_From_Email__c = '';
       
            if(o.Send_From_Name__c == null)
                 o.Send_From_Name__c = '';
       
            if(o.SG_Invite_ID__c == null)
                 o.SG_Invite_ID__c = '';
       
             if(o.Time_Zone__c == null)
                 o.Time_Zone__c = '';
       
            if(o.Venue_Name__c == null)
                 o.Venue_Name__c = '';
       
             if(o.Event_Address__c == null)
                 o.Event_Address__c = '';
       
            if(o.Event_Address__c == null)
                 o.Event_Address__c = '';
       
            requestForm = o.Invite_Request_Form__c;
       
            if(requestForm.contains('rsvp.stg.kapowevents.com')){
                requestForm = requestForm.replace('\"','\\"');
                SYSTEM.DEBUG(requestform);
            }
            else{
                requestForm = '';
                SYSTEM.DEBUG(requestform);
            }
                                                    

            

       
            //Build JSON String to send    
            String JSONString = '{\"Account_Manager_ID\":\"' + o.Account_Manager_ID__c +'\",\"Account_Id\":\"' + o.AccountId 
                +'\", \"Client_Total\":\"' + o.Client_Total__c +'\",\"event_consultant\":\"' 
                + o.event_consultant__c +'\",\"event_date\":\"' + o.event_date__c +'\",\"event_end_time\":\"' 
                + o.event_end_time__c +'\",\"event_start_time\":\"' 
                + o.event_start_time__c +'\",\"event_status\":\"' + o.event_status__c +'\",\"event_type\":\"' 
                + o.event_type__c +'\",\"opportunity_record_id\":\"' 
                + o.Id +'\",\"invite_custom_fields\":\"' + o.invite_custom_fields__c +'\",\"invite_headline\":\"' 
                + o.invite_headline__c +'\",\"invite_request_form\":\"' + requestform +'\",\"invite_yes_limit\":\"' 
                + o.invite_yes_limit__c +'\",\"market\":\"' + o.market__c +'\",\"name\":\"' 
                + o.name +'\",\"send_from_email\":\"' + o.send_from_email__c +'\",\"send_from_name\":\"' 
                + o.send_from_name__c +'\",\"sg_invite_id\":\"' + o.sg_invite_id__c +'\",\"stage_name\":\"' 
                + o.stagename +'\",\"time_zone\":\"' + o.time_zone__c +'\",\"venue_name\":\"' + o.venue_name__c +'\"}';
                                               
            System.debug('JSONString: ' + JSONString); //make sure format is correct
        
            
            req.setBody(JSONString);
     
            
            http http = new http();
            httpResponse res = http.send(req);
            
            System.debug(res.getBody());
            
        
  }

 
Andy BoettcherAndy Boettcher

You may have hit a limit - you can have a max of 50 future methods per APEX invocation.  I would add some more System.Debugs in there with data from the Limits class to see your counters spin up.

Other than that - I would watch the Developer Console/Logs to see those Future methods execute and monitor your remote endpoint to see if there was a problem receiving.  Salesforce is like a firehose - I've been on a number of implementations where Salesforce simply overpowers a remote endpoint with traffic.