function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Pranav ChitransPranav Chitrans 

trigger error while inserting new record

plz help with this error... It perfectly works with the update scenario.. but when i insert any new account it throws error
ERROR : Apex trigger Pranav.CheckboxUpdate caused an unexpected exception, contact your administrator: Pranav.CheckboxUpdate: execution of BeforeInsert caused by: System.StringException: Invalid id: populate: Trigger.Pranav.CheckboxUpdate: line 8, column 1
trigger CheckboxUpdate on Account (before insert,after insert ,after update) 
{
 List<Contact> cnlist = new List<Contact>();
 if(trigger.isbefore && trigger.isInsert)
 {
  for(Account ac : trigger.new) 
  {
   ac.Contact_Custom_Lookup__c = ac.name;
  }
 }

 if(trigger.isInsert && trigger.isAfter)
 {
  for(account ac:trigger.new)
  {
   Contact c= new Contact();
   c.Accountid=ac.id;
   c.Lastname=ac.Name;
   c.Checkbox__c=ac.Checkbox__c;
   cnlist.add(c);
  }
  Insert cnlist;
 }
 if(trigger.isupdate)
 {
  for(account ac:trigger.new)
  {
   List<Contact> cn=[select id from contact Where accountid IN :trigger.new ];
   for(integer i=0;i<cn.size();i++)
   {
    cn[i].Checkbox__c=ac.Checkbox__c;
   } 
   cnlist.addall(cn);
  }      
  update cnlist;
 }
}

 
Amit Chaudhary 8Amit Chaudhary 8
It look like you are trying to insert String in ID field

ac.Contact_Custom_Lookup__c = ac.name;

Please let us know if this will help you.