+ Start a Discussion
Force.platformForce.platform 

read only error in after insert trigger

Hello All,
  I have trigger that create no of contacts realted to account as per value entered in No_Of_Contact in account object.
Trigger:
trigger AllAccountTrigger on Account (after insert,after update, before insert, before update) {
    if(Trigger.isAfter)
    {   if(Trigger.isInsert || Trigger.isUpdate)
        { 
          AccountExample.createMultipleContact(Trigger.new);
        }
    }
    }

class method:
 public static void createMultipleContact(List<Account> acc){
         List<Contact> c= new List<Contact>();
         for( Account a:acc){
          if(a.Create_Contact__c= true && a.No_Of_Contacts_to_create__c!= 0){
          for(Integer i=1; i<=a.No_Of_Contacts_to_create__c;i++){
          Contact con=new Contact();
          con.FirstName=a.name;
          con.LastName =a.Account_Number__c+'-'+con.Contact_Count__c;
          con.Phone=a.phone;
          con.AccountId=a.Id;
          c.add(con); 
            }
            if(c.size()>0){
            insert c;
               }
              }
             }
            }
 
 but i getting error: System.FinalException: Record is read-only:
Shalini RShalini R
Hi 

Please refer the link it will help to you.

https://developer.salesforce.com/forums/?id=906F000000092NSIAY
GauravGargGauravGarg

Hi,

Please update the code to run in future call and add "@Future" annotion for creating contact. 

Thanks,

Gaurav
Skype: gaurav62990