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
Prakhyat sapraPrakhyat sapra 

i have one chackbox type field in account. & if chackbox is false the account owner = contact owner & if true account owner != contact owner through trigger

i have one chackbox type field in account. & if chackbox is false the account owner != contact owner & if true account owner != contact owner through trigger?????
Best Answer chosen by Prakhyat sapra
CharuDuttCharuDutt
Hii Prakhyat
Try Below Trigger
trigger AccountCheckBox on Account (before insert,before update) {
    set<Id>SetAccountId = new Set<Id>();
    if(trigger.IsBefore){
        if(trigger.IsUpdate){
            for(Account Acc : trigger.new){
                if(Acc.OwnerId != trigger.oldMap.get(Acc.Id).OwnerId){
                    SetAccountId.add(Acc.Id);
                }
            }
        }   
    }
    map<String,Boolean>mapAccount = new map<String,Boolean>();
    list<Account>lstAccount = [Select Id,OwnerId,(Select Id,OwnerId,AccountId from Contacts) from Account where Id in :SetAccountId];
    for(Account Acc : lstAccount){
        for(Contact Con : Acc.Contacts){
            if(Acc.OwnerId == Con.OwnerId){
                mapAccount.put(Acc.Id,true);
            }else{
                mapAccount.put(Acc.Id,false);
            }
        }
    }
    if(trigger.IsBefore){
        if(trigger.IsUpdate){
            for(Account Acc : trigger.new){
                if(mapAccount.containsKey(Acc.Id)){
                    Acc.Checkbox__c = mapAccount.get(Acc.Id);
                }
            }
        }   
    }
}
Please Mark It As best Asnwer If It Helps
Thank You!