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
MrHammyMrHammy 

bulkifiing issue

My class is now working and doing everything I want with the exception of large result sets.  My public PageReference Save is a for loop and calls another class that is making way to many  DML calls. Its the end of the day and I thought I would ask if anyone can point me at a way to bulkify this a little bit, I know what's wrong I just can't find the fix yet.

 

Thanks for all the help

 

public class MultiRow {
        public List<multiRowContact> Contacts { get; set; }
        String chainname, prog, filter;
        String idx = ApexPages.currentPage().getParameters().get('idx') ;
    
        public PageReference 
        cancel() {
        return new PageReference('/'+idx);
        }
 
    public MultiRow() {
        chainname = ApexPages.currentPage().getParameters().get('name') ;
        prog = ApexPages.currentPage().getParameters().get('pg') ;
        if(prog == 'WM Synergy'){
        filter = '0128000000020mOAAQ'; //WM Synergy
        } else if (prog == 'PS Cornerstone') {
        filter = '0128000000020mTAAQ'; //PS Cornerstone
          } else if (prog == 'CB Ascend') {
        filter = '0128000000020mVAAQ'; //CB Ascend
        } else {
        filter = ''; 
        }
        LoadData();        
    }
     


    private void LoadData() { 
        Contacts = new List<multiRowContact>();
        for (List<erp_data__c> cs : [SELECT name, Account__c ,Acct_RT_Name__c, Chain__c,Id, Program__c, RecordTypeId , updatetowhat__c 
               FROM ERP_Data__c  
               WHERE Chain__c = :chainname and RecordTypeId = :filter limit 25]) {
               
                for (erp_data__c c : cs) {
                    multiRowContact MRC = new multiRowContact();
                    MRC.ID = c.ID;
                    MRC.Checked = true;
                    MRC.Name = c.Name;
                    MRC.Program = c.Program__c;
                    MRC.account = c.Account__c ;
                    MRC.updatetowhat = prog;
                    Contacts.add(MRC);
            }
        }
    }
    public PageReference Save() {
        for (multiRowContact MRC : Contacts) {
        MRC.Save();
        }
        //LoadData();
        return new PageReference('/'+idx);
    } 

    private class multiRowContact {
    String prog = ApexPages.currentPage().getParameters().get('pg') ;
    String idx = ApexPages.currentPage().getParameters().get('idx') ;
        public String ID { get; set; }
        public String Name { get; set; }
        public String Account { get; set; }
        public String updatetowhat{ get; set; }
        public String Program { get; set; }
        public Boolean Checked { get; set; }
        List<Participating_Branch_Locations__c > updatedList = new List<Participating_Branch_Locations__c >();
        List<erp_data__c > updatedList_erp = new List<erp_data__c >();
       
        public void Save() {
           

            if (Checked) {
            
            //select account__c  from Participating_Branch_Locations__c where Distributor_Program__c = idx
            
            list <Participating_Branch_Locations__c > ids = [select account__c  from Participating_Branch_Locations__c where Distributor_Program__c = :idx];
            
                if( 1 == 2){
            
                } else {
            
                    System.debug('Saving...ID: ' + ID);
                    erp_data__c c = [SELECT c.updatetowhat__c FROM erp_data__c c WHERE c.ID = :ID LIMIT 1];
                    c.updatetowhat__c = prog;
                    updatedList_erp.add(c);
                    
                    Participating_Branch_Locations__c newpbl = new Participating_Branch_Locations__c (Account__c = Account ,Distributor_Program__c = idx);
                   
                    updatedList.add(newpbl);
            
            
            
                }
            
            
            
            }
            insert updatedList;
            update updatedList_erp;

        }

    }

}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You're going to have to refactor the save inside the MultiRowContact class so that it sets up the records to insert/update but doesn't carry out the insert.  

 

The way I'd handle this is to create the lists in the MultiRow class and pass these as parameters to each call of the MultiRowContact.save() method. 

 

The MultiRow save method becomes:

 

public PageReference Save() {
        List<Participating_Branch_Locations__c > updatedList = new List<Participating_Branch_Locations__c >();
        List<erp_data__c > updatedList_erp = new List<erp_data__c >();
        for (multiRowContact MRC : Contacts) 
        {
          MRC.Save(updatedList, updatedList_erp);
        }

insert updatedList;
update updatedList_erp;
//LoadData(); return new PageReference('/'+idx); }

 

and the MultiRowContact save method becomes:

 

public void Save(List<Participating_Branch_Locations__c > updatedList,
        List<erp_data__c > updatedList_erp) 
   {
            if (Checked) {
            
            //select account__c  from Participating_Branch_Locations__c where Distributor_Program__c = idx
            
            list <Participating_Branch_Locations__c > ids = [select account__c  from Participating_Branch_Locations__c where Distributor_Program__c = :idx];
            
                if( 1 == 2){
            
                } else {
            
                    System.debug('Saving...ID: ' + ID);
                    erp_data__c c = [SELECT c.updatetowhat__c FROM erp_data__c c WHERE c.ID = :ID LIMIT 1];
                    c.updatetowhat__c = prog;
                    updatedList_erp.add(c);
                    
                    Participating_Branch_Locations__c newpbl = new Participating_Branch_Locations__c (Account__c = Account ,Distributor_Program__c = idx);
                   
                    updatedList.add(newpbl);
                }
            }
       }

 

All Answers

bob_buzzardbob_buzzard

You're going to have to refactor the save inside the MultiRowContact class so that it sets up the records to insert/update but doesn't carry out the insert.  

 

The way I'd handle this is to create the lists in the MultiRow class and pass these as parameters to each call of the MultiRowContact.save() method. 

 

The MultiRow save method becomes:

 

public PageReference Save() {
        List<Participating_Branch_Locations__c > updatedList = new List<Participating_Branch_Locations__c >();
        List<erp_data__c > updatedList_erp = new List<erp_data__c >();
        for (multiRowContact MRC : Contacts) 
        {
          MRC.Save(updatedList, updatedList_erp);
        }

insert updatedList;
update updatedList_erp;
//LoadData(); return new PageReference('/'+idx); }

 

and the MultiRowContact save method becomes:

 

public void Save(List<Participating_Branch_Locations__c > updatedList,
        List<erp_data__c > updatedList_erp) 
   {
            if (Checked) {
            
            //select account__c  from Participating_Branch_Locations__c where Distributor_Program__c = idx
            
            list <Participating_Branch_Locations__c > ids = [select account__c  from Participating_Branch_Locations__c where Distributor_Program__c = :idx];
            
                if( 1 == 2){
            
                } else {
            
                    System.debug('Saving...ID: ' + ID);
                    erp_data__c c = [SELECT c.updatetowhat__c FROM erp_data__c c WHERE c.ID = :ID LIMIT 1];
                    c.updatetowhat__c = prog;
                    updatedList_erp.add(c);
                    
                    Participating_Branch_Locations__c newpbl = new Participating_Branch_Locations__c (Account__c = Account ,Distributor_Program__c = idx);
                   
                    updatedList.add(newpbl);
                }
            }
       }

 

This was selected as the best answer
MrHammyMrHammy
That did the trick, i could see what i needed to do but was not sure how to make it happen, thanks ever so much!