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
Kamaldeep SehrawatKamaldeep Sehrawat 

How to Bulkify the Apex Code

Hi All, 

I am in process of developing a code to get the information from the Case Object and update it on our Website. I have developed all the code (Apex Classes and Apex Trigger) and the code is working well but I am getting an Error while I am doing a mass update of more than 51 records on the Case Object due to the classes and triggers. Can any body Help me in bulkfying the code or even suggest me ways I can improve the code so that I doesn't get any errors while mass updating the records in Case Object. 

Below are the Apex Classes and Trigger that I have developed 

Apex Class 1

public class GGUSettings
{
    public static string USERNAME = 'abc';
    public static string PASSWORD = 'abc';
    public static string GGU_END_POINT = 'https://www.ggu.edu';
}

Apex Class 2 

//Generated by wsdl2apex

public class gguEduIntegration {
    public class Exception_x {
        public String message;
        private String[] message_type_info = new String[]{'message','http://ggu.edu/integration',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://ggu.edu/integration','false','false'};
        private String[] field_order_type_info = new String[]{'message'};
    }
    public class result {
        public String[] error;
        public String status;
        private String[] error_type_info = new String[]{'error','http://ggu.edu/integration',null,'0','-1','true'};
        private String[] status_type_info = new String[]{'status','http://ggu.edu/integration',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://ggu.edu/integration','false','false'};
        private String[] field_order_type_info = new String[]{'error','status'};
    }
    public class updateCase {
        public String caseId;
        public String status;
        public String resolution;
        public String resolutionMethod;
        public String closeDate;
        private String[] caseId_type_info = new String[]{'caseId','http://ggu.edu/integration',null,'0','1','false'};
        private String[] status_type_info = new String[]{'status','http://ggu.edu/integration',null,'0','1','false'};
        private String[] resolution_type_info = new String[]{'resolution','http://ggu.edu/integration',null,'0','1','false'};
        private String[] resolutionMethod_type_info = new String[]{'resolutionMethod','http://ggu.edu/integration',null,'0','1','false'};
        private String[] closeDate_type_info = new String[]{'closeDate','http://ggu.edu/integration',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://ggu.edu/integration','false','false'};
        private String[] field_order_type_info = new String[]{'caseId','status','resolution','resolutionMethod','closeDate'};
    }
    public class updateCaseResponse {
        public gguEduIntegration.result return_x;
        private String[] return_x_type_info = new String[]{'return','http://ggu.edu/integration',null,'0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://ggu.edu/integration','false','false'};
        private String[] field_order_type_info = new String[]{'return_x'};
    }
    public class SFDCHandlerPort {
        public String endpoint_x = 'https://www.ggu.edu/integration';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'http://ggu.edu/integration', 'gguEduIntegration'};
        public gguEduIntegration.result updateCase(String caseId,String status,String resolution,String resolutionMethod,String closeDate) {
            gguEduIntegration.updateCase request_x = new gguEduIntegration.updateCase();
            request_x.caseId = caseId;
            request_x.status = status;
            request_x.resolution = resolution;
            request_x.resolutionMethod = resolutionMethod;
            request_x.closeDate = closeDate;
            gguEduIntegration.updateCaseResponse response_x;
            Map<String, gguEduIntegration.updateCaseResponse> response_map_x = new Map<String, gguEduIntegration.updateCaseResponse>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              '',
              'http://ggu.edu/integration',
              'updateCase',
              'http://ggu.edu/integration',
              'updateCaseResponse',
              'gguEduIntegration.updateCaseResponse'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.return_x;
        }
    }
}


Apex Class 3 

public class GGUWSCall
{
    private static gguEduIntegration.SFDCHandlerPort login(){
        gguEduIntegration.SFDCHandlerPort port = new gguEduIntegration.SFDCHandlerPort();
         
        port.inputHttpHeaders_x = new Map<String, String>();

        Blob headerValue = Blob.valueOf(GGUSettings.USERNAME + ':' + GGUSettings.PASSWORD );
         
        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);

        port.inputHttpHeaders_x.put('Authorization', authorizationHeader);

        port.inputHttpHeaders_x.put('Content-Type', 'text/xml; charset=utf-8');
       
        return port;
    }
    
    @Future(callout=true) 
    public static void syncCase(String caseId, String status, String resolution, String resolutionMethod, String closedDate)
    {                 
        gguEduIntegration.SFDCHandlerPort port = login();
        port.updateCase(caseId, status, resolution, resolutionMethod, closedDate);
    }
}

Apex Trigger 

Trigger 

trigger CaseSyncTrigger on Case (after update) {
    for (Case caseNew : Trigger.new) {
        GGUWSCall.syncCase(caseNew.CaseID__c, caseNew.Status, caseNew.Resolution__c, caseNew.ResolutionMethod__c, caseNew.ClosedDate != null ? caseNew.ClosedDate.format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\'') : '');
//        System.debug(Logginglevel.INFO, 'caseId: '+caseNew.CaseID__c+' status: '+caseNew.Status + ' resolution: '+caseNew.Resolution__c+ ' resolutionMethod: '+ 
//        caseNew.ResolutionMethod__c + ' closeDate: '+(caseNew.ClosedDate!=null?caseNew.ClosedDate.format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\''):''));
    }
}

Thanks 





 
Best Answer chosen by Kamaldeep Sehrawat
SunidharSunidhar
I'm not sure but u can do some thing like this

trigger CaseSyncTrigger on Case (after update) {
  set<Id> set_id = new set<Id>();
    for (Case caseNew : Trigger.new) {
       set_id.add(caseNew.id);
    }
 GGUWSCall.syncCase(set_id);
}




public class GGUWSCall
{
    private static gguEduIntegration.SFDCHandlerPort login(){
        gguEduIntegration.SFDCHandlerPort port = new gguEduIntegration.SFDCHandlerPort();
         
        port.inputHttpHeaders_x = new Map<String, String>();

        Blob headerValue = Blob.valueOf(GGUSettings.USERNAME + ':' + GGUSettings.PASSWORD );
         
        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);

        port.inputHttpHeaders_x.put('Authorization', authorizationHeader);

        port.inputHttpHeaders_x.put('Content-Type', 'text/xml; charset=utf-8');
       
        return port;
    }
    
    @Future(callout=true) 
    public static void syncCase(set<Id> ids)
    {                 
        list<wrapClas> wrap_list = new list<wrapClas>();
        for(case cs : [SELECT id,CaseID__c, status,Resolution__c,ResolutionMethod__c,ClosedDate FROM case WHERE id IN:ids]) {
            wrap_list.add(new wrapCls(cs));
        }
        gguEduIntegration.SFDCHandlerPort port = login();
        for(wrapClas wr : wrap_list) {
            port.updateCase(wr);
        }
    }
    
    public wrapClas {
        public string caseId;
        public string status;
        public string resolution;
        public string resolutionMethod;
        public date closedDate;
        
        public wrapClas(case cs) {
            caseId = cs.CaseID__c;
            status = cs.status;
            resolution = cs.Resolution__c;
            closedDate = cs.ClosedDate;
            resolutionMethod = cs.ResolutionMethod__c;
        }
    }
}

All Answers

SunidharSunidhar
To bulkify the code u need to send the list of reccords to syncCase method in trigger like GGUWSCall.syncCase(trigger.new);

as future call outs dosen't support primitive data type u can better send the list of case id's to the method and u need to get the records from query in the future callout class, and then by using wrapper class in the method parameter u can send the data to another system.


trigger CaseSyncTrigger on Case (after update) {
  set<Id> set_id = new set<Id>();
    for (Case caseNew : Trigger.new) {
       set_id.add(caseNew.id);
    }
 GGUWSCall.syncCase(set_id);
}




public class GGUWSCall
{
    private static gguEduIntegration.SFDCHandlerPort login(){
        gguEduIntegration.SFDCHandlerPort port = new gguEduIntegration.SFDCHandlerPort();
         
        port.inputHttpHeaders_x = new Map<String, String>();

        Blob headerValue = Blob.valueOf(GGUSettings.USERNAME + ':' + GGUSettings.PASSWORD );
         
        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);

        port.inputHttpHeaders_x.put('Authorization', authorizationHeader);

        port.inputHttpHeaders_x.put('Content-Type', 'text/xml; charset=utf-8');
       
        return port;
    }
    
    @Future(callout=true) 
    public static void syncCase(set<Id> ids)
    {                 
        list<wrapClas> wrap_list = new list<wrapClas>();
        for(case cs : [SELECT id,CaseID__c, status,Resolution__c,ResolutionMethod__c,ClosedDate FROM case WHERE id IN:ids]) {
            wrap_list.add(new wrapCls(cs));
        }
        gguEduIntegration.SFDCHandlerPort port = login();
        port.updateCase(wrap_list);
    }
    
    public wrapClas {
        public string caseId;
        public string status;
        public string resolution;
        public string resolutionMethod;
        public date closedDate;
        
        public wrapClas(case cs) {
            caseId = cs.CaseID__c;
            status = cs.status;
            resolution = cs.Resolution__c;
            closedDate = cs.ClosedDate;
            resolutionMethod = cs.ResolutionMethod__c;
        }
    }
}

if this sloves ur requirement please make it as solved
Kamaldeep SehrawatKamaldeep Sehrawat
Hi, 

Thanks for the prompt reply. I am still getting the following Error 

Method does not exist or incorrect signature: [gguEduIntegration.SFDCHandlerPort].updateCase(List<GGUWSCall.wrapClas>;)

Thanks 

 
SunidharSunidhar
I'm not sure but u can do some thing like this

trigger CaseSyncTrigger on Case (after update) {
  set<Id> set_id = new set<Id>();
    for (Case caseNew : Trigger.new) {
       set_id.add(caseNew.id);
    }
 GGUWSCall.syncCase(set_id);
}




public class GGUWSCall
{
    private static gguEduIntegration.SFDCHandlerPort login(){
        gguEduIntegration.SFDCHandlerPort port = new gguEduIntegration.SFDCHandlerPort();
         
        port.inputHttpHeaders_x = new Map<String, String>();

        Blob headerValue = Blob.valueOf(GGUSettings.USERNAME + ':' + GGUSettings.PASSWORD );
         
        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);

        port.inputHttpHeaders_x.put('Authorization', authorizationHeader);

        port.inputHttpHeaders_x.put('Content-Type', 'text/xml; charset=utf-8');
       
        return port;
    }
    
    @Future(callout=true) 
    public static void syncCase(set<Id> ids)
    {                 
        list<wrapClas> wrap_list = new list<wrapClas>();
        for(case cs : [SELECT id,CaseID__c, status,Resolution__c,ResolutionMethod__c,ClosedDate FROM case WHERE id IN:ids]) {
            wrap_list.add(new wrapCls(cs));
        }
        gguEduIntegration.SFDCHandlerPort port = login();
        for(wrapClas wr : wrap_list) {
            port.updateCase(wr);
        }
    }
    
    public wrapClas {
        public string caseId;
        public string status;
        public string resolution;
        public string resolutionMethod;
        public date closedDate;
        
        public wrapClas(case cs) {
            caseId = cs.CaseID__c;
            status = cs.status;
            resolution = cs.Resolution__c;
            closedDate = cs.ClosedDate;
            resolutionMethod = cs.ResolutionMethod__c;
        }
    }
}
This was selected as the best answer