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
NaishadhNaishadh 

Batch Update Trigger problem

I have created trigger on my role custom object. If I create, update or delete single record on role it works fine and execute trigger and apex code. But if I try to create/update multiple record using excel connector it simply ignore the trigger.

 

Please guide.

 
 


Message Edited by Naishadh on 11-12-2008 02:31 AM
SuperfellSuperfell
I think you missed a semi-colon ;) what's your trigger code look like?
NaishadhNaishadh
Hi,

Sorry i didn't get it . Trigger code are as under.
trigger RoleContactTrigger on Roles_Contacts__c (after insert,after update,after delete) {   

Roles_Contacts__c[] contactRole;

if(Trigger.isDelete) {
contactRole= Trigger.old;
} else {
contactRole= Trigger.new;
}

RoleContactClass.updateContacts(contactRole);
}
and apex class code
public class RoleContactClass { 
public static void updateContacts(Roles_Contacts__c[] contactRole) {
        System.debug(contactRole);
        Set<ID> userIds = new Set<ID> ();
        //Retrieve userIds
        for(Integer i = 0;i < contactRole.size();i++) {
          userIds.add(contactRole[i].Contact__c);           
        }

        //fetch contact record based on userIds.
        Contact[] con = [ Select c.Primary_Brand_roll_up__c, c.Contacts_roll_up__c, c.Business_Unit_roll_up__c, c.Id From Contact c where c.Id in :userIds];

        contactRole = [ Select s.Contact__r.Name, s.Contact__c, s.Primary_Brand__c, s.Business_Unit__c From Roles_Contacts__c s where s.Contact__c in :userIds];

        System.debug('No of Record' + con.size());
       

        //clear all content of contact custom field
        //not support single quotes only.
        con[0].Contacts_roll_up__c = 't';
        con[0].Primary_Brand_roll_up__c = 't';
        con[0].Business_Unit_roll_up__c = 't';       
       

        for(Integer i = 0;i < contactRole.size();i++) {
          if(contactRole[i].Contact__r != null && contactRole[i].Contact__r.Name != null) {
              if(con[0].Contacts_roll_up__c.length() > 1) {
                  if(!con[0].Contacts_roll_up__c.contains(contactRole[i].Contact__r.Name)) {
                      con[0].Contacts_roll_up__c += ',' + contactRole[i].Contact__r.Name;
                  }                     
              } else {
                  con[0].Contacts_roll_up__c = contactRole[i].Contact__r.Name;
              }
          }
           

          if(contactRole[i].Business_Unit__c != null) {
              if(con[0].Business_Unit_roll_up__c.length() > 1) {
                  if(!con[0].Business_Unit_roll_up__c.contains(contactRole[i].Business_Unit__c)) {
                      con[0].Business_Unit_roll_up__c += ',' + contactRole[i].Business_Unit__c;
                  }
              } else {
                  con[0].Business_Unit_roll_up__c = contactRole[i].Business_Unit__c;
              }
          }

           
          if(contactRole[i].Primary_Brand__c != null) {
              if(con[0].Primary_Brand_roll_up__c.length() > 1) {
                  if(!con[0].Primary_Brand_roll_up__c.contains(contactRole[i].Primary_Brand__c)) {
                      con[0].Primary_Brand_roll_up__c += ',' + contactRole[i].Primary_Brand__c;              
                  }
              } else {
                  con[0].Primary_Brand_roll_up__c = contactRole[i].Primary_Brand__c;
              }              
          }
           
        }

        //check for no data.
        if(con[0].Contacts_roll_up__c.length() == 1) {
          con[0].Contacts_roll_up__c = '';
        }

        if(con[0].Business_Unit_roll_up__c.length() == 1) {
          con[0].Business_Unit_roll_up__c = '';
        }

        if(con[0].Primary_Brand_roll_up__c.length() == 1) {
          con[0].Primary_Brand_roll_up__c = '';
        }
       


        try {
          update con;
        }catch(System.DmlException e){
          System.debug('We caught a DML exception: ' + e.getDmlMessage(0));  
        }    
    }
}





Message Edited by Naishadh on 11-13-2008 03:29 AM
SuperfellSuperfell
You trigger only updates the first item in the array, so any bulk operation will only update 1 out of every 100 rows. trigger.new / trigger.old is an array so that you can process more than one row at once, see the apex docs.
NaishadhNaishadh
Thanks Simon. It is working now. Thanks again for the help.