Saturday, 17 June 2017

Trigger to check the checkbox on contact object based on checkbox check on account object


Suppose there is a validate custom field on both account & contact object, we need to check the checkbox on account object based on which the checkbox on contact object needs to be checked for the related account, then we can use the below trigger for the same.


trigger AccConValidate on Account (after update,after delete)
 {

Set<Id> ObjID = new Set<Id>();


if(trigger.isinsert || trigger.isdelete)
{
    for(account a: trigger.new)
    {
        ObjID.add(a.id);
    }
}


if(trigger.isupdate )
{
    for(account a: trigger.old)
    {
        ObjID.add(a.id);
    }
}




list<contact> ObjLocCon = [select id,validate__c,account.validate__c from contact  where account.id in: ObjID];

for(contact c : ObjLocCon )
{
    if (c.account.validate__c)
    {
        c.validate__c=true;
     
    }
    else
    {
    c.validate__c=false;
    }

}
update ObjLocCon ;

}

No comments:

Post a Comment