bulk update trigger

trigger AccountReplicate on Account (after delete, after insert, after update) {
    if ( trigger.isInsert ) {
        for ( Account acc : trigger.new ) {
            AccountOne__c ac = new AccountOne__c();
            ac.Fax__c = acc.Fax;
            ac.Phone__c = acc.Phone;
            ac.Name =acc.Name;
            insert ac;

    if ( trigger.isUpdate ) {
        integer  i = 0;
        list<AccountOne__c> a2 = new list<AccountOne__c>([SELECT Id 
                                                       FROM AccountOne__c 
                                                       WHERE Name =: trigger.old[i].Name ]);
        for ( AccountOne__c c1 : a2 ) {
            c1.Name = trigger.new[i].name;
            c1.Fax__C = trigger.new[i].Fax;
            c1.Phone__C = trigger.new[i].Phone;
        update a2;

I tried bulk trigger for the update operation, but it’s not working can anyone help me?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The approach you are following is wrong. Can you please try this piece of code for bulk update of trigger.

    Map<String,Account> accNameMap = new Map<String,Account>();
    for(Account acc : trigger.old){
    List<AccountOne__c> accOneList =  [Select name,fax,phone from AccountOne__c where name in: accNameMap.keyset()];
    for(AccountOne__c accOne : accOneList){
        Account relatedAccount = trigger.newmap.get(accNameMap.get(accOne.name).Id);
        accOne.name = relatedAccount.Name;
        accOne.fax = relatedAccount.fax;
        accOne.phone = relatedAccount.phone;
    update accOneList;

Method 2

from my side there are 2 key problems:

  • the first is that you haven’t queried all fields, as it was said @Sandy, and you can use it in for loop. And I’m not sure that this trigger will run for single record as well as for bulk
  • the second is that you suppose that query return exactly same amount of record as amount of processed into the trigger. You use i variable, but the query might return more that one record in single DML operation (it seems from your query, because this condition WHERE Name =: trigger.old[i].Name might return more than 1 records. If it’s impossible in your data model, please write about it). Also I think that using LIMIT in the query might be helpful

try to use

 trigger AccountReplicate on Account (after delete, after insert, after update)         {

     if (trigger.isInsert) {
         List<AccountOne__c> accountOneList = new List<AccountOne__c>();
         for (Account acc : trigger.new) {
             AccountOne__c ac = new AccountOne__c();
             ac.Fax__c = acc.Fax;
             ac.Phone__c = acc.Phone;
             ac.Name = acc.Name;
             ac.ParrentId__c = acc.Id; // add key field for one-to-one matching accountOne__c with account
         insert accountOneList;

     if (trigger.isUpdate) {
         list<AccountOne__c> a2 = new list<AccountOne__c>([SELECT Id,
                                                           FROM AccountOne__c
                                                           WHERE ParrentId__c 
                                                               IN trigger.newMap.keySet()]);
         Account tmpAcc;
         for (AccountOne__c c1 : a2) {
            tmpAcc = trigger.newMap.get(c1.ParrentId__c);
            if (tmpAcc != null) {
                c1.Name = tmpAcc.name;
                c1.Fax__C = tmpAcc.Fax;
                c1.Phone__C = tmpAcc.Phone;
         update a2;

Method 3

You haven’t queried all fields

I think your query would be

list a2 = new list([Select Id,Name,Phone_C,Fax_C from AccountOne__c where Name =: trigger.old[i].Name ]);

But I can’t understand what you are trying to do.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x