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
Anto HotelbedsAnto Hotelbeds 

Bulk Trigger

Hi,

 

I need to create a trigger but I have problems to create it in a "bulk" way. This is the requirement:

 

I have accounts which are related to another one (called the main). I can have up to 15 accounts related to this main one.

 

When some field of the main account, as the account name or address, are modified, I need to replicate this change to the accounts that are related to this main one. This change doesnt have to be replicated always, just when the related accounts fulfills a condition based on its accountrecordtype

 

I can do

 

For [a: in trigger.new]{

Account RelatedAccount=[SELECT id, recordtypeId FROM Account Where RelatedAccountId:=a.Id];

 

}

 

But this wouldnt be the bulk way and I would probably get errors.

 


Any help is appreciated.

Thanks,

Antonio

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You are correct in this assumption. Your code would run okay outside of a bulk environment (e.g. a user editing a record), but would fall flat when using an import wizard or the data loader. Instead, you would want to do something like this:

 

for(account a:[select id,recordtypeid,relatedaccountid from account where relatedaccountid in :trigger.new]) {
  // Trigger.newMap.get(a.relatedaccountid) is the account in the trigger.
  // a is the related account for this loop iteration
}

Edit: Small modification to the filter criteria.

All Answers

sfdcfoxsfdcfox

You are correct in this assumption. Your code would run okay outside of a bulk environment (e.g. a user editing a record), but would fall flat when using an import wizard or the data loader. Instead, you would want to do something like this:

 

for(account a:[select id,recordtypeid,relatedaccountid from account where relatedaccountid in :trigger.new]) {
  // Trigger.newMap.get(a.relatedaccountid) is the account in the trigger.
  // a is the related account for this loop iteration
}

Edit: Small modification to the filter criteria.

This was selected as the best answer
Anto HotelbedsAnto Hotelbeds

Thanks a lot sfdcfox. I paste my trigger code in case it might help someone:

 

trigger UpdateDuplicities on Account (after update) {
	List<Account> duplicities = new Account[]{};
    duplicities=[SELECT id,Unique_Atlas_Code__c,Reasons_for_Duplicate__c,Market_Country__c,Commission_Group__c,Distribution_Type__c,
                 Street_Commercial__c,City_Commercial__c,Post_Code_Commercial__c,Country_Commercial__c,Province_Commercial_2__c,
                 Customer_Connectivity__c,Commercial_Brand__c,Customer_Subtype__c,Customer_Type__c,Direct_Hotel_Payment__c,Liberate_Rates__c,
                 OwnerId,Pricing_Model__c,License__c,License_Number__c,Name FROM account WHERE Unique_Atlas_Code__c in :trigger.new];
    Account a;
    for (Integer i=0;i<duplicities.size();i++){
        a=duplicities.get(i);
        //Analizo que campo ha cambiado en la cuenta principal
        //Si ha modificado un campo que la cuetna relacionada hereda de la padre, lo inserto en el map si no existe
        //y si existe le modifico el campo que tiene que heredar
        system.debug('valor nuevo: '+Trigger.newMap.get(a.Unique_Atlas_Code__c).Street_Commercial__c);
        system.debug('valor antiguo: '+Trigger.oldMap.get(a.Unique_Atlas_Code__c).Street_Commercial__c);
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).Market_Country__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).Market_Country__c){
            //Cambia el market country
            if (a.Reasons_for_Duplicate__c!='Source Market'){
                a.Market_Country__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).Market_Country__c;
            }
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).Commission_Group__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).Commission_Group__c){
            //Cambia el commission group
            if (a.Reasons_for_Duplicate__c!='Source Market' && a.Reasons_for_Duplicate__c!='Business Model Net' && a.Reasons_for_Duplicate__c!='Business Model Commissionable'){
                a.Commission_Group__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).Commission_Group__c;
            }
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).Distribution_Type__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).Distribution_Type__c){
            //Cambia el Distribution Type
            if (a.Reasons_for_Duplicate__c!='B2B Product Availability' && a.Reasons_for_Duplicate__c!='B2C Product Availability'){
                a.Distribution_Type__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).Distribution_Type__c;
            }
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).Street_Commercial__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).Street_Commercial__c){
            //Cambia la calle comercial
            system.debug('entro aqui');
            if (a.Reasons_for_Duplicate__c!='Different Departments (SAP)' && a.Reasons_for_Duplicate__c!='Different Departments (No SAP)'){
                a.Street_Commercial__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).Street_Commercial__c;
                system.debug('actualizo la calle a: '+ Trigger.newMap.get(a.Unique_Atlas_Code__c).Street_Commercial__c);
            }
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).City_Commercial__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).city_Commercial__c){
            //Cambia la ciudad comercial
            if (a.Reasons_for_Duplicate__c!='Different Departments (SAP)' && a.Reasons_for_Duplicate__c!='Different Departments (No SAP)'){
                a.City_Commercial__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).City_Commercial__c;
            }
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).Post_Code_Commercial__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).Post_Code_Commercial__c){
            //Cambia el codigo postal comercial
            if (a.Reasons_for_Duplicate__c!='Different Departments (SAP)' && a.Reasons_for_Duplicate__c!='Different Departments (No SAP)'){
                a.Post_Code_Commercial__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).Post_Code_Commercial__c;
            }
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).Country_Commercial__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).Country_Commercial__c){
            //Cambia el country comercial
            if (a.Reasons_for_Duplicate__c!='Different Departments (SAP)' && a.Reasons_for_Duplicate__c!='Different Departments (No SAP)'){
                a.Country_Commercial__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).Country_Commercial__c;
            }
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).Province_Commercial_2__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).Province_Commercial_2__c){
            //Cambia la provincia comercial
            if (a.Reasons_for_Duplicate__c!='Different Departments (SAP)' && a.Reasons_for_Duplicate__c!='Different Departments (No SAP)'){
                a.Province_Commercial_2__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).Province_Commercial_2__c;
            }
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).Customer_Connectivity__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).Customer_Connectivity__c){
            //Cambia el customer connectivity
            if (a.Reasons_for_Duplicate__c!='XML Integration' && a.Reasons_for_Duplicate__c!='Whitelabel'){
                a.Customer_Connectivity__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).Customer_Connectivity__c;
            }
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).Commercial_Brand__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).Commercial_Brand__c){
            //Cambia el commercial brand
            a.Commercial_Brand__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).Commercial_Brand__c;
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).Customer_Subtype__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).Customer_Subtype__c){
            //Cambia el customer subtype
            a.Customer_Subtype__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).Customer_Subtype__c;
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).Customer_Type__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).Customer_Type__c){
            //Cambia el customer type
            a.Customer_Type__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).Customer_Type__c;
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).Direct_Hotel_Payment__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).Direct_Hotel_Payment__c){
            //Cambia el direct hotel payment
            a.Direct_Hotel_Payment__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).Direct_Hotel_Payment__c;
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).Liberate_Rates__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).Liberate_Rates__c){
            //Cambia el liberate rates
            a.Liberate_Rates__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).Liberate_Rates__c;
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).OwnerId!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).OwnerId){
            //Cambia el owner de la cuenta
            a.OwnerId=Trigger.newMap.get(a.Unique_Atlas_Code__c).OwnerId;
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).Pricing_Model__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).Pricing_Model__c){
            //Cambia el pricing model
            a.Pricing_Model__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).Pricing_Model__c;
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).License__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).License__c){
            //Cambia el tipo de licencia
            a.License__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).License__c;
        }
        if (Trigger.newMap.get(a.Unique_Atlas_Code__c).License_Number__c!=Trigger.oldMap.get(a.Unique_Atlas_Code__c).License_Number__c){
            //Cambia el numero de licencia
            a.License_Number__c=Trigger.newMap.get(a.Unique_Atlas_Code__c).License_Number__c;
        }
        duplicities.set(i,a);
        system.debug(a.Street_Commercial__c + ' Account '+a.Name);
	}
    //make the update of the list of accounts
    update duplicities;
}

 Cheers!

 

Antonio