+ Start a Discussion
alexjalexj 

System.FinalException: Record is read-only

I have a trigger which enable to verify for each insert if there are yet a Lead or Account with the same mail.

If there is Lead, there are no problem, my trigger run correctly. But when there is already an account, I have an error : 

SearchDoubleAndFusionAccount: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.SearchDoubleAndFusionAccount: line 124, column 1

 

Can I modify an Account just after the insert ? 

 

Here is my trigger :

trigger SearchDoubleAndFusionAccount on Account (after insert) {
    if(checkRecursive.runOnce())
    {
        Set<ID> ids = Trigger.newMap.keySet();
        Set<String> accountMail = new Set<String>();// liste des nouveaux id
        List<Account> updatedAccount = new List<Account>();
        List<Lead> deletedLead = new List<Lead>();
        List<Account> deletedAccount = new List<Account>();
        String demande ='';
        
        for (Account a : trigger.new)// ajout des nouveaux id
        {
            accountMail.add(a.Adresse_mail__c);
            System.debug(a.Adresse_mail__c);
        }
        
        List<Account>accounts = [SELECT Id, Salutation, FirstName , Adresse_mail__c, LastName, Numero_client__c, PersonLeadSource, D_sinscription_la_newsletter__c, Inscription_la_newsletter__c, Raison_de_la_d_sinscription__c, Date_de_d_sinscription_la_newsletter__c, Date_d_inscription_la_newsletter__c   FROM Account];
        List<Lead> leads= [SELECT  Id, Salutation, FirstName , Adresse_mail__c, LastName, Num_ro_piste__c, LeadSource,Phone, Telephone_portable__c, Rating,Type_d_utilisation__c , Origine_connaissance_Kiwatch__c, Client__c, D_sinscription_la_newsletter__c, Inscription_la_newsletter__c, Raison_de_la_d_sinscription__c, Date_de_d_sinscription_la_newsletter__c, Date_d_inscription_la_newsletter__c, Street, City, State, Country, PostalCode  FROM Lead ];
        
        for (Account anew : trigger.new)
        {
            for(Account acc : accounts){
                if(acc.Id == anew.Id)
                {
                    System.debug('Id egal'+ ' '+acc.Id+ ' '+anew.Id);
                }
                else if(acc.Adresse_mail__c ==anew.Adresse_mail__c){
                    System.debug('Email egal'+ ' '+anew.Adresse_mail__c+' '+acc.Id+ ' '+anew.Id);
                    //champs identification du compte (non modifiable)
                    if(acc.Salutation == null){acc.Salutation = anew.Salutation;}
                    if(acc.FirstName == null){acc.FirstName = anew.FirstName;}
                    if(acc.LastName == null){acc.LastName = anew.LastName;}
                    if(acc.Numero_client__c == 0 || acc.Numero_client__c ==null ){acc.Numero_client__c = anew.Numero_client__c;}
                    if(acc.Adresse_mail__c == null){acc.Adresse_mail__c = anew.Adresse_mail__c;}
                    if(acc.PersonLeadSource == null){acc.PersonLeadSource = anew.PersonLeadSource;}
                    
                    
                    //information sur l'adresse. Si un est modifié, tous le sont
                    if(anew.PersonMailingStreet !=null || anew.PersonMailingCity !=null || anew.PersonMailingPostalCode !=null || anew.PersonMailingCountry !=null || anew.PersonMailingState !=null)
                    {
                        acc.PersonMailingStreet = anew.PersonMailingStreet;
                        acc.PersonMailingCity = anew.PersonMailingCity ;
                        acc.PersonMailingPostalCode = anew.PersonMailingPostalCode;
                        acc.PersonMailingCountry = anew.PersonMailingCountry;
                        acc.PersonMailingState = anew.PersonMailingState ;
                    }
                    
                    if(anew.PersonHomePhone !=null){acc .PersonHomePhone = anew.PersonHomePhone ;}
                    if(anew.PersonMobilePhone !=null){acc .PersonMobilePhone =  anew.PersonMobilePhone ;}          
                    if(anew.Rating !=null){acc .Rating = anew.Rating ;}
                    
                    if(anew.Type_d_utilisation__c !=null){acc .Type_d_utilisation__c = anew.Type_d_utilisation__c ;}
                    if(anew.demande__c !=null || anew.Demande_contact__c !=null || anew.Objet_demande__c !=null){
                        acc.demande__c = anew.demande__c;
                        acc.Demande_contact__c = anew.Demande_contact__c;
                        acc.Objet_demande__c = anew.Objet_demande__c;
                        demande = acc.LastName +'a fait une nouvelle demande de contact' + acc.Demande_contact__c+' :'+acc.Objet_demande__c+' '+acc.demande__c;
                        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                        String[] toAddresses = new String[] {'h.broussin@kiwatch.com'}; 
                        mail.setToAddresses(toAddresses); 
                        mail.setSubject('Mise à jour d\'une compte');
                        mail.setPlainTextBody(demande);
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
                    }
                    

                    //champ relatif à la newsletter
                    if(anew.Inscription_la_newsletter__c == true){
                        if(acc.Inscription_la_newsletter__c == false){
                            acc.Inscription_la_newsletter__c = true;
                            System.debug( acc.Inscription_la_newsletter__c);
                            acc.Date_d_inscription_la_newsletter__c = anew.Date_d_inscription_la_newsletter__c;
                            acc.D_sinscription_la_newsletter__c = false;
                            System.debug('cas1');
                        }
                    }
                    if(anew.D_sinscription_la_newsletter__c == true){
                        if(acc.D_sinscription_la_newsletter__c == false){
                            acc.D_sinscription_la_newsletter__c = true;
                            acc.Date_de_d_sinscription_la_newsletter__c = anew.Date_de_d_sinscription_la_newsletter__c;
                            acc.Raison_de_la_d_sinscription__c = anew.Raison_de_la_d_sinscription__c;
                            acc.Inscription_la_newsletter__c = false;
                            System.debug('cas2');
                         }
                    }
                    
                    //champ concernant l'abonnement
                    if(anew.Date_d_abonnement__c !=null){acc.Date_d_abonnement__c = anew.Date_d_abonnement__c;}
                    if(anew.Date_fin_de_contrat__c !=null){acc.Date_fin_de_contrat__c = anew.Date_fin_de_contrat__c;}
                    if(anew.Raison_fin_de_contrat__c !=null){acc.Raison_fin_de_contrat__c = anew.Raison_fin_de_contrat__c;}
                    if(anew.Abonnement__c !=null){acc.Abonnement__c = anew.Abonnement__c;}
                    if(anew.Date_de_l_abonnement_actuel__c !=null){acc.Date_de_l_abonnement_actuel__c = anew.Date_de_l_abonnement_actuel__c;}
                    
                
                    updatedAccount.add(acc);
                    deletedAccount.add(anew);
                }
            }
              
            for (Lead ld : leads) {
                if(ld.Adresse_mail__c ==anew.Adresse_mail__c){
                    System.debug('Piste existante'+ ' '+anew.Adresse_mail__c+' '+ld.Id+ ' '+anew.Id);

                     //champs identification du compte (non modifiable)
                    if(ld.Salutation != null){anew.Salutation = ld.Salutation;}                  
                    if(ld.FirstName != null){anew.FirstName = ld.FirstName;}
                    if(ld.LastName != null){anew.LastName = ld.LastName;}
                    
                    anew.PersonLeadSource = ld.LeadSource;
                    
                    
                    //information sur l'adresse. Si un est modifié, tous le sont
                    if(anew.PersonMailingStreet ==null && anew.PersonMailingCity ==null && anew.PersonMailingPostalCode ==null && anew.PersonMailingCountry ==null && anew.PersonMailingState ==null)
                    {
                        anew.PersonMailingStreet = ld.Street;
                        anew.PersonMailingCity = ld.City ;
                        anew.PersonMailingPostalCode = ld.PostalCode;
                        anew.PersonMailingCountry = ld.Country;
                        anew.PersonMailingState = ld.State ;
                    }
                    
                    if(anew.PersonHomePhone ==null){anew.PersonHomePhone = ld.Phone ;}
                    if(anew.PersonMobilePhone ==null){anew.PersonMobilePhone =  ld.Telephone_portable__c ;}          
                    if(anew.Rating ==null){anew.Rating = ld.Rating ;}
                    
                    if(anew.Type_d_utilisation__c ==null){anew.Type_d_utilisation__c = ld.Type_d_utilisation__c ;}    
                    
                    //champ relatif à la newsletter
                     if(anew.Inscription_la_newsletter__c == false && anew.D_sinscription_la_newsletter__c == false){
                        if(ld.Inscription_la_newsletter__c == true){
                            anew.Inscription_la_newsletter__c = true;
                            anew.Date_d_inscription_la_newsletter__c = ld.Date_d_inscription_la_newsletter__c;
                            anew.D_sinscription_la_newsletter__c = ld.D_sinscription_la_newsletter__c;
                            anew.Date_de_d_sinscription_la_newsletter__c = ld.Date_de_d_sinscription_la_newsletter__c;
                        }
                    }

                    updatedAccount.add(anew);
                    deletedLead.add(ld);
                }
            }
        }       
        if(updatedAccount.size()>0){
            System.debug('mise à jour compte');
            update updatedAccount;
        }

        if(deletedLead.size()>0){
            for (Lead l : deletedLead){
                System.debug('suppression piste');
                MyFutureClass.DeleteLead(l.Id); 
            }
        }
        if(deletedAccount.size()>0){
            for (Account a : deletedAccount){
                System.debug('suppression compte');
                MyFutureClass.DeleteAccount(a.Id); 
            }
        }
    }
}

 

Andy BoettcherAndy Boettcher

You can't touch the record itself on the "after insert" - if you want to modify any fields, either you have to instantiate a new object via SOQL or flip your trigger to a "before insert".

 

-Andy

alexjalexj

When I try to flip my trigger to before insert, I have an error  :

DML statment cannot operate on trigger.new or trigger.old:

 

I try to instantiate a new object, I think that is easier

craigmhcraigmh

When you work with a before insert/update trigger, you do not need to specify a DML statement.

 

So this:

 

List<Lead> newLeads = trigger.new;
for(Lead l: newLeads) {
	l.ProcessedByTrigger__c = true;
}
update newLeads;

 

 

Becomes this:

 

for(Lead l: trigger.new) {
	l.ProcessedByTrigger__c = true;
}

 

RichardGRichardG

Instantiating a new object via SOQL is the solution !

(it seems obvious once the trigger fires and make the update after insert :)
Thx TechMan97