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
subodh chaturvedi 17subodh chaturvedi 17 

how to write a trigger to check & Uncheck the parent Record based on the Child Record Values.

subodh chaturvedi 17 
how to update a parent field (checkbox) Based on child records field Values.
 i have checkbox field Visa__c In Account & i have a custom Object (Card program ) where i have two fields Cardtype __c & card_processor_status__c, so the requiremnt is when the  cardtype__c=Visa  & Card_processor_Status__c =Active then the Checkbox should get check in Visa__c in account & when the two fields value is other than visa & active the  checkbox should get unchecked.

let suppose we have 10 records in Cardprogram out of 10 if atleast 1 record have fullfill the criteria then also the checkbox should get checked .

Here is the criteria for when the “Visa” field should be TRUE:
There is a Card Program record related to the Account that has at least one single record that meets both the criteria below:
The “Card Type” field is “Visa”
The “Card Processor Status” field is “Active”
 
Best Answer chosen by subodh chaturvedi 17
Parag Bhatt 10Parag Bhatt 10
Hi Subodh,

Actually i was not checking that condition previously in my code. If you see there i declared map but didn't use .
now as per your requirement I fixed the code and attested below please have a look :)
 
trigger conatctaccouontupdate on card_program__c (after insert,After Update) {
        set<id> setid = new set<id>();
        map<id,card_program__c> mapcheck = new map<id,card_program__c>();
        list<Account> acclist = new list<Account>();
        for(card_program__c con:Trigger.new){
            mapcheck.put(con.AccountId,con);
            setid.add(con.AccountId);
            
        }
        
        if(setid.size()>0){
            for(Account acc:[select id,Visa__c from account where id IN :setid ]){
                if(mapcheck.containsKey(acc.id)){
                    if(mapcheck.get(acc.id).Card_Processor_Status__c == TRUE && mapcheck.get(acc.id).Card_Type__c == 'Visa'){
                        acc.visa__c = TRUE;
                        acclist.add(acc); 
                    }
                    else{
                        acc.visa__c = FALSE;
                        acclist.add(acc); 
                    }
                    
                }
            }
        }
        
        if(acclist.size()>0){
            update acclist;
        }
    }
It is now  working as expected  :)

Please let us know if this will help you. and mark it  as best answer

Thanks,
Parag Bhatt
 

All Answers

Saravana Muthu 8Saravana Muthu 8
Hi,

You can create a process builder to achieve this.

1) Create the process builder on custom Object (Card program ).

2) Check the criteria The “Card Type” field is “Visa” and the “Card Processor Status” field is “Active".

3) Then update the Account(Parent) field Visa__c to TRUE.

Thanks,
Sarav
Sweet Potato Tec
subodh chaturvedi 17subodh chaturvedi 17
Hi,

I have done Exactly the same thing but the problem is it is Working only for 1 record. 
Suppose i have 2 records in card program whose both field values are meeting the criteria ,so for one record when i am changing the value other thanCard Type= visa & Card Processor Status” field =active  then it is changing the Visa  in account  with  unchecked , that should not happen because we have one record in card program whose value Is Active & visa so the checkbox should get remained checked.

If both record values are changed than only the Visa checkbox get unchecked.
Parag Bhatt 10Parag Bhatt 10
Hi subodh chaturvedi,

I will definately help you out finding solution to above problem.
I wrote a Trigger on "card Program" which will run on "after Insert and after Update" scenario. please find below :)
trigger conatctaccouontupdate on card_program__c (after insert,After Update) {
    set<id> setid = new set<id>();
    map<id,Account> mapaccount = new map<id,Account>();
    list<Account> acclist = new list<Account>();
    for(card_program__c con:Trigger.new){
        if(con.Card_Processor_Status__c == TRUE && con.Card_Type__c == 'Visa' && con.AccountId !=null){
            setid.add(con.AccountId);
        }
    }
    
    if(setid.size()>0){
        for(Account acc:[select id,Visa__c from account where id IN :setid ]){
            acc.visa__c = TRUE;
            acclist.add(acc);
            
        }
    }
    
    if(acclist.size()>0){
        update acclist;
    }
}

It is working as expected :)

Please let us know if this will help you.

Thanks,
Parag Bhatt
 
subodh chaturvedi 17subodh chaturvedi 17
Hi Prag,
Your Code is working as  per requirement but there is one small problem that is when i changed the value of both record then in Account the Visa__c checkbox is not geting Unchecked. 
Parag Bhatt 10Parag Bhatt 10
Hi Subodh,

Actually i was not checking that condition previously in my code. If you see there i declared map but didn't use .
now as per your requirement I fixed the code and attested below please have a look :)
 
trigger conatctaccouontupdate on card_program__c (after insert,After Update) {
        set<id> setid = new set<id>();
        map<id,card_program__c> mapcheck = new map<id,card_program__c>();
        list<Account> acclist = new list<Account>();
        for(card_program__c con:Trigger.new){
            mapcheck.put(con.AccountId,con);
            setid.add(con.AccountId);
            
        }
        
        if(setid.size()>0){
            for(Account acc:[select id,Visa__c from account where id IN :setid ]){
                if(mapcheck.containsKey(acc.id)){
                    if(mapcheck.get(acc.id).Card_Processor_Status__c == TRUE && mapcheck.get(acc.id).Card_Type__c == 'Visa'){
                        acc.visa__c = TRUE;
                        acclist.add(acc); 
                    }
                    else{
                        acc.visa__c = FALSE;
                        acclist.add(acc); 
                    }
                    
                }
            }
        }
        
        if(acclist.size()>0){
            update acclist;
        }
    }
It is now  working as expected  :)

Please let us know if this will help you. and mark it  as best answer

Thanks,
Parag Bhatt
 
This was selected as the best answer