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
Karen HansonKaren Hanson 

Error In Apex Trigger to create contacts record only if check box is checked in account

HI ,
I have a custom field called Create_Contact__c that is a data type "checkbox". in Account object and
I need to evaluate it in an apex trigger. and create new contact record when it is checked in account object .

Error: Compile Error: Variable does not exist: Create_Contact__c at line 3 column 6​
 
trigger CreateAccountContact on Account (after insert, after update){

  if(Create_Contact__c == True)  {

    List<Contact> ct = new List <Contact>();

    for(Account acc : trigger.new){
    
        Contact c = new Contact(LastName = acc.name,
                    AccountId=acc.id,
                    Fax=acc.Fax,
                    MailingStreet=acc.BillingStreet,
                    MailingCity=acc.BillingCity,
                    MailingState=acc.BillingState,
                    MailingPostalCode=acc.BillingPostalCode,
                    MailingCountry=acc.BillingCountry,
                    Phone=acc.Phone);

        ct.add(c);
    }
    insert ct; 
}
}

 
RatanRatan
remove 3rd line code and add inside for loop
 
trigger CreateAccountContact on Account (after insert, after update){

    List<Contact> ct = new List <Contact>();

    for(Account acc : trigger.new){
		
		if(acc.Create_Contact__c == True)  {
        Contact c = new Contact(LastName = acc.name,
                    AccountId=acc.id,
                    Fax=acc.Fax,
                    MailingStreet=acc.BillingStreet,
                    MailingCity=acc.BillingCity,
                    MailingState=acc.BillingState,
                    MailingPostalCode=acc.BillingPostalCode,
                    MailingCountry=acc.BillingCountry,
                    Phone=acc.Phone);

        ct.add(c);
		}
    }
    insert ct; 
}

 
Amit Chaudhary 8Amit Chaudhary 8
I found below two issue in your .
1) Check you was using outside the loop
2) Before DML please check size of list. in case of Create_Contact__c == false it will fail

Please below code. I hope that will help you
trigger CreateAccountContact on Account (after insert, after update){

    List<Contact> ct = new List <Contact>();

    for(Account acc : trigger.new)
	{
		if(acc.Create_Contact__c == True)  
		{
			Contact c = new Contact(LastName = acc.name,
						AccountId=acc.id,
						Fax=acc.Fax,
						MailingStreet=acc.BillingStreet,
						MailingCity=acc.BillingCity,
						MailingState=acc.BillingState,
						MailingPostalCode=acc.BillingPostalCode,
						MailingCountry=acc.BillingCountry,
						Phone=acc.Phone);

			ct.add(c);
		}
    }
	if( ct.size() >0 )
	{
		insert ct; 
	}	
}
Let us know if this will help you

Thanks
Amit Chaudhary


 
RatanRatan
Amit.. Just for FYI:

If you perform DML on empty list that doesn't count DML operation

Please see this discussion http://salesforce.stackexchange.com/q/19399/18731
Onesh ReddyOnesh Reddy
Hi Karen,
When the Checkbox is True and  you try to update other fields, a New Contact is created for every Update.(Eg : If you Update the account phone number again a new contact will be created as the checkbox is True).
Please find the Code below that exactly fits your purpose.
trigger CreateAccountContact on Account (after insert, after update){
    
    List<Contact> ct = new List <Contact>();
    List<Account> Oldaccount=trigger.old;
    List<Account> Newaccount=trigger.new;
    for(integer i=0;i<newaccount.size();i++){
        if(Newaccount[i].Create_Contact__c==true || (Newaccount[i].Create_Contact__c==true && oldaccount[i].create_Contact__c==false))
        {
            Contact c = new Contact(LastName = Newaccount[i].name,
                                    AccountId=Newaccount[i].id,
                                    Fax=Newaccount[i].Fax,
                                    MailingStreet=Newaccount[i].BillingStreet,
                                    MailingCity=Newaccount[i].BillingCity,
                                    MailingState=Newaccount[i].BillingState,
                                    MailingPostalCode=Newaccount[i].BillingPostalCode,
                                    MailingCountry=Newaccount[i].BillingCountry,
                                    Phone=Newaccount[i].Phone);
            ct.add(c);
        }
    }
    if( ct.size() >0 )
	{
		insert ct; 
	}	
}
Let us know if this will help you

Thanks,
Onesh.K