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
XJCXJC 

Trigger to create records on status change

Hi,

 

I am a newbie when it comes to apex and could do with some guidance. Basically, I am trying to create two different triggers, each firing under different scenarios.

 

On the first instance, I'd like a trigger which would fire when a checkbox is populated in an account and create a child record (populating the lookup and some fields on the child object) and on another instance, I'd like it to trigger when a certain value is selected from a picklist, creating a child record of the account and populating fields in both the account & child object.

 

Any ideas would be amazing!

 

Thanks.

m.elmoussaouim.elmoussaoui

Both tirggers it must be after insert otherwise you can't link the created account to the child record.

Here's an example for your case, for more information you can find more here (specialy bulkifying your trigger) 

 

trigger AccountAfterInsert on Account (after insert)
{
    for(Account a : Trigger.new)
    {
	if(a.checkboxField__c)
	{
            //do stuff
	}

	if(a.picklistField__c == 'PicklistValue')
	{
	    //do stuff
	}
    }
}

 

 

 

 

 

 

Cato1984Cato1984

Since you are creating new child records for the Account, you will have the Account Id during update triggers. You can use a before update trigger. If however these values can be set on creation of an account, meaning the end user creates a new account and before they hit save for the first time they set the checkbox and picklist to the values that would normally trigger your code, you would have to do an after insert trigger to ensure you have the account Id.

XJCXJC

Hi, 

 

Thanks for your help. 

 

Just to clarify (I do not have that much programming experience), after insert means when a record has been updated? Also, will this create a child record, but not associated with the parent account record? Is there any way to populate the lookup in the child with the record ID? (If it were only a picklist populated to say 'Option 1'?

 

Thanks again!