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
HareHare 

upon checking checkbox field on parent need to check child records exist or not

I Have a Parent Account object in this i have check box field "No Asset exist". when ever i am trying to check the checkbox it need to show error message if Assert records exist for this Account Object. if no assert are exist for Account no error message need to show . Assert is child of Account. its shoud be bulkify need to work for multiple Accounts

Need achive by using trigger...plese help on this
Best Answer chosen by Hare
Anirudh SinghAnirudh Singh
Hi,

Please see if the below trigger helps:

trigger AssertTrigger on Assert__c(after insert)
{
    List<Id> uniqueAccountIds=new List<Id>();
    
    for(Assert__c assertRecord: Trigger.New)
    {
        uniqueAccountIds.add(assertRecord.Account__c);
    }
    
    List<Account> accountsToUpdateList=new List<Account>();
    for(Account accRecord: [SELECT No_Asset_exist__c FROM Account WHERE Id IN: uniqueAccountIds])
    {
        if(accRecord.No_Asset_exist__c)
        {
            accRecord.No_Asset_exist__c=false;
            accountsToUpdateList.add(accRecord);
        }
    }
    
    update accountsToUpdateList;
}

Thanks and Regards,
Anirudh Singh

All Answers

Anirudh SinghAnirudh Singh
Hello,

I have written a Trigger, please let me know if it helps you:

trigger AccountTrigger on Account(before update)
{
    Set<Id> uniqueAccountIds=new Set<Id>();
    
    for(Account accountRecord: Trigger.New)
    {
        if(accountRecord.No_Asset_exist__c)
        {
            uniqueAccountIds.add(accountRecord.Id);
        }
    }
    
    List<Account> accountsList=[
        SELECT Id, (SELECT Id FROM Asserts__r) 
        FROM Account
        WHERE Id IN :uniqueAccountIds
    ];
    
    List<Account> accountsToBeUpdated=new List<Account>();
    for(Account accountRecord: Trigger.New)
    {
        for(Account accRecord: accountsList)
        {
            if(accountRecord.Id==accRecord.Id && accRecord.Asserts__r.size()>0)
            {
                accountRecord.addError('Assert records exist for this Account.');
            }
            else if(accountRecord.Id==accRecord.Id && accRecord.Asserts__r.size()==0)
            {
                accountsToBeUpdated.add(accountRecord);
            }
        }
    }
}

Thanks and Regards,
Anirudh Singh
HareHare
Thanks, working fine..
in  Account object "No Asset exist" field if allready checked.. if asset record created that "No Asset exist" on  account should be uncheck for this need to write trigger on assert triger..  please help me on this... thanks
Anirudh SinghAnirudh Singh
Hi,

Please see if the below trigger helps:

trigger AssertTrigger on Assert__c(after insert)
{
    List<Id> uniqueAccountIds=new List<Id>();
    
    for(Assert__c assertRecord: Trigger.New)
    {
        uniqueAccountIds.add(assertRecord.Account__c);
    }
    
    List<Account> accountsToUpdateList=new List<Account>();
    for(Account accRecord: [SELECT No_Asset_exist__c FROM Account WHERE Id IN: uniqueAccountIds])
    {
        if(accRecord.No_Asset_exist__c)
        {
            accRecord.No_Asset_exist__c=false;
            accountsToUpdateList.add(accRecord);
        }
    }
    
    update accountsToUpdateList;
}

Thanks and Regards,
Anirudh Singh
This was selected as the best answer