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
ajay ambatiajay ambati 

trigger not firing on chekbox feild i

 i am entering new values in account if phone is anything in account that must be same related contact phone and create related contact for that account.so its firing. but, after this i want to check the checkbox field in account thats not firing pls hlp me

trigger creatingcasebasedonaccounttrigger on Account (after insert) 
{
    list<schema.contact> lst = new list<schema.contact>();
    list<account> gh = new list<account>();
     list<Id> listIds = new List<Id>();
    
   //schema.accc=new schema.contact();
   if(trigger.isafter) 
   {
       if(trigger.isinsert)
       {
          for(account acc:trigger.new) 
          {
             System.debug('@@@@id is: ' + acc.Id);
            System.debug('####name is : ' + acc.Name);
            if(acc.phone !=null)
            {
            schema.contact cs = new schema.contact();
                  //cs.Account=acc.Name;
                  cs.lastname=acc.name;
                 //cs.closed Date='';
                  cs.accountid =acc.id;
                  cs.phone=acc.phone;
                  cs.contactrelaioncheckbox__c=true;
                  lst.add(cs);
          
                
              }
          }
       }
    //acc.CHECKEDACCOUNT__c=true;
     insert lst; 
      
       
 list<account> lk =[select id,name,CHECKEDACCOUNT__c from account where id in:listIds];
         for(account an:lk)
         {
             an.CHECKEDACCOUNT__c=true;
             gh.add(an);
         }
       
   }
           update gh;
         
   }

 
Nayana KNayana K
trigger creatingcasebasedonaccounttrigger on Account (after insert) 
{
    List<Contact> lstContact = new List<Contact>();
	List<Account> lstAccount = new List<Account>();
	Set<Id> setAccId = new Set<Id>();
    if(Trigger.isAfter && Trigger.isInsert) 
    {
		for(Account objAccount : Trigger.New) 
		{
			if(String.isNotBlank(objAccount.Phone))
			{
				lstContact.add(new Contact(AccountId = objAccount.Id, LastName = objAccount.LastName, 
											Phone = objAccount.Phone, contactrelaioncheckbox__c = true));
				setAccId.add(objAccount.Id);
			}
		}
		
		if(!lstContact.isEmpty())
			insert lstContact;
		
		for(Id idAcc : setAccId)
		{
			lstAccount.add(new Account(Id = idAcc, CHECKEDACCOUNT__c = true));
		}
		
		if(!lstAccount.isEmpty())
			update lstAccount;
    }      
}

try this once.
Dilip_VDilip_V
Ajay,

Try this code.
trigger creatingcasebasedonaccounttrigger on Account (after insert) 
{
    list<schema.contact> lst = new list<schema.contact>();
    list<account> gh = new list<account>();
     list<Id> listIds = new List<Id>();
    
   //schema.accc=new schema.contact();
   if(trigger.isafter) 
   {
       if(trigger.isinsert)
       {
          for(account acc:trigger.new) 
          {
             System.debug('@@@@id is: ' + acc.Id);
            System.debug('####name is : ' + acc.Name);
            if(acc.phone !=null)
            {
            schema.contact cs = new schema.contact();
                  //cs.Account=acc.Name;
                  cs.lastname=acc.name;
                 //cs.closed Date='';
                  cs.accountid =acc.id;
                  cs.phone=acc.phone;
                  cs.contactrelaioncheckbox__c=true;
                  lst.add(cs);
                  listIds.add(acc.id);
                
              }
          }
       }
    //acc.CHECKEDACCOUNT__c=true;
     insert lst; 
      
       
 list<account> lk =[select id,name,CHECKEDACCOUNT__c from account where id in:listIds];
         for(account an:lk)
         {
             an.CHECKEDACCOUNT__c=true;
             gh.add(an);
         }
       
   }
           update gh;
         
   }

Let me know if it helps.

Make it as best answer if it helps

Thanks.
Dilip_VDilip_V
Ajay,

You forgot to populate listIds 
listIds.add(acc.id);
thats the main reason.

Thanks.
shravanshravan
Here is code snippet of your requirement. 

trigger AccountTrigger on Account (after insert) {
    List<Contact> contactList = new List<Contact>();
    for(Account acc : Trigger.new) {
        Contact con = new Contact();
        //All fields mapping here.
        //con.firstname = account.name
        contactList.add(con);
        acc.CHECKEDACCOUNT__c = true;
    }        
    insert contactList;
}
ajay ambatiajay ambati
hi,follwing error iam geeting while saving.
 
Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger creatingcasebasedonaccounttrigger caused an unexpected exception, contact your administrator: creatingcasebasedonaccounttrigger: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 0012800000sJlLXAA0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, record already exists: []: Trigger.creatingcasebasedonaccounttrigger: line 43, column 1
shravanshravan
can you pls take out checkbox field update to workflwo field update
 
ajay ambatiajay ambati
No I want to update field through trigger itself
Dilip_VDilip_V
Ajay,

You have active validation rule on account.Deactivate it and tryonce.

Let me know if it helps.

If it helps make it as best answer.

Thanks.
shravanshravan
ok. then try this.
trigger AccountTrigger on Account (after insert) {   
    List<Contact> contactList = new List<Contact>();
    for(Account acc : Trigger.new) {
        Contact con = new Contact();
        //All fields mapping here.
        //con.firstname = account.name
        contactList.add(con);        
    }        
   List<Account> accList = [select id,CHECKEDACCOUNT__c  from Account where id in :Trigger.newMap.keySet()];
   for(Account acc : accList) {
        acc.CHECKEDACCOUNT__c = true;
   }
    insert accList;
}


Here you may face recusive try to user static variable to avoid recursive