0
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;
            i++;
        }
        update a2;
    }
}

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

7
  • What are you trying to do? Can you briefly explain the use case? This update on which object?
    – Prady
    Commented Jul 23, 2013 at 6:51
  • Im having AccountOne as custom object ,when i create a new account the same will be created in tht custom object ,similarly if i update tht account same must be updated in Accountone.....my trigger handles single record but not bulk Commented Jul 23, 2013 at 6:56
  • Is there any relationship between accountone and account?
    – Prady
    Commented Jul 23, 2013 at 7:09
  • It would also be helpful for those answering if your code was neatly formatted. Commented Jul 23, 2013 at 7:12
  • when we update r create records in account trigger will be fired such that same record will be created or updated in Accountone Commented Jul 23, 2013 at 7:21

3 Answers 3

0

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

if(trigger.isUpdate)
{ 
    Map<String,Account> accNameMap = new Map<String,Account>();
    for(Account acc : trigger.old){
        accNameMap.put(acc.name,acc);
    }
    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;
}
11
  • this throws error in list<Account_one> Commented Jul 23, 2013 at 7:53
  • Initial field of expression mut be concrete sobject Commented Jul 23, 2013 at 7:54
  • I have updated my code. Can you please confirm which line is throwing exception and what is the error message ?
    – S.Sharma
    Commented Jul 23, 2013 at 8:02
  • Account relatedAccount = trigger.newmap.get(accNameMap.get(accOne.name)); this line throws error: Incompatible key type SOBJECT:Account for MAP<Id,Account> Commented Jul 23, 2013 at 8:40
  • Sorry, I just missed the map key type. I have updated it to use account's id. Can you please give it a try now ?
    – S.Sharma
    Commented Jul 23, 2013 at 9:16
1

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

UPDATED: 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
             accountOneList.add(ac);
         }
         insert accountOneList;
     }

     if (trigger.isUpdate) {
         list<AccountOne__c> a2 = new list<AccountOne__c>([SELECT Id,
                                                                  Name,
                                                                  Phone_c,
                                                                  Fax_v,
                                                                  ParrentId__c
                                                           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;
     }
 }     
4
  • It runs for single record Commented Jul 23, 2013 at 7:26
  • @ManojChandran please post code of whole trigger Commented Jul 23, 2013 at 7:36
  • i have updated check now Commented Jul 23, 2013 at 7:44
  • @ManojChandran please see update Commented Jul 23, 2013 at 8:23
0

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.

1
  • just check my code now and see my previous comment hope so u will understand....thank you Commented Jul 23, 2013 at 6:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .