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
Ankit Garg 117Ankit Garg 117 

Trigger to update a field on a child accounts from a filed on a parent account.

Hey Gurus,

I am newbiew to Triggers. 

Scenerio  - There is a checkbox field on a Account called "Account Visited"? Now if the parent account gets visited, its same as visiting all of its child accounts. 

So I need a trigger if someone checked the box on the Parent account, all of its child accounts gets checked as well and sales rep dont have to go to every child account individually to update.

Cheers
Mohamed IthrisMohamed Ithris
Hello Ankit,

Please find the below Code.I hope it will help you.I did some of Senorio.

trigger AccountVisited on Account (before Insert) {

List<Id> Aid =New List<Id>();
for(Account acc :Trigger.New)
{
if(acc.ParentId != null)
{
Aid.add(acc.ParentId);
}
}
System.debug('Aid '+Aid );
if(Aid != null)
{
List<account> str = [Select id,Account_Visited__c from Account Where  id IN :Aid];
if(str.size() != 0)
{
System.debug('str[0].Account_Visited__c'+str[0].Account_Visited__c);
if(str[0].Account_Visited__c == true)
{
for(Account acc1 :Trigger.New)
{
acc1.Account_Visited__c = true; 

}
}}}}

If you think its ok please make as resolved.

Thanks
Mohamed Ithris