1

I have on the account a lookup field on account named ' '. And two account record types 'RT1' and 'RT2'

While adding an account with RT2, I have implemented a trigger to change the account owner with the lookup field account owner.

I'm missing something.

Here's my code below the account lookup field on the account is: Compte_Gestionnaire__c

 public static void immeubleAccountOwner (List<Account> listacc){
 set<Id> accId = new set<Id>();
 list<User> listuser = new list<User>();
 map<Id,User> mapUser ;

 for(Account ac : listacc) 
 accId.add(ac.Compte_Gestionnaire__c);

 if(userId.size()>0){
 mapUser = new map<Id,User>([select Id from User where Id IN: userId]);
 }

 for(Account acc : listacc){

acc.OwnerId = mapUser.get(acc.Compte_Gestionnaire__c).OwnerId;
}

}

And my trigger is:

 trigger AccountAfterInsert on Account (before insert, before update) {
 list<Account> listacc;
 for(Account acc : trigger.new)   {

   if(acc.OwnerId != null && acc.Compte_Gestionnaire__c != null && acc.RecordTypeId== '01220000000UC4oAAG'){
   listacc.add(acc);
   }
   }

 if(listacc=.size()>0){
 MyClasse.immeubleAccountOwner(listacc);
 }         
 }
2
  • 1
    believe u don't need a trigger for this. did you try doing this with either process builder or workflow rule + field update ? Commented May 26, 2015 at 11:32
  • It's not the right solution because It's a dynamic value not static one
    – LoveLace
    Commented May 27, 2015 at 10:08

1 Answer 1

1

You can simply write your trigger code as:

trigger AccountAfterInsert on Account (before insert, before update) {
 //list<Account> listacc;
 for(Account acc : trigger.new)   {

   if(acc.OwnerId != null && acc.Compte_Gestionnaire__c != null && acc.RecordTypeId== '01220000000UC4oAAG'){
    acc.OwnerId = acc.Compte_Gestionnaire__c;
   }
   }

     /*if(listacc=.size()>0){
     MyClasse.immeubleAccountOwner(listacc);
     }     */

 }

Hope this may help.

2
  • Here some issues while implementing this idea. This Compte_Gestionnaire__c is empty field and the error is OwnerId cannot be null this is caused by a 'before' trigger. Beside, we cant update fields on 'After' trigger because the record is locked means read only :/
    – LoveLace
    Commented May 26, 2015 at 8:16
  • But you are also handling the null condition in if(), right? So, if acc.Compte_Gestionnaire__c is null then it wont enter into if() block and assign null to ownerid. Also, can you tell me the error which you are getting.
    – Nebula
    Commented May 26, 2015 at 10:44

You must log in to answer this question.

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