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
himanshu huske 7himanshu huske 7 

Trigger_Task_4

Account object has Acc_Satus__c  Field and Contact Object has Con_Status__c Field.
both Acc_Satus__c, Con_Status__c are picklist field with values: Open, Closed, InProgress.
Account Record Has Three Child Contact Records
1. Whenever Account record field is updated with Acc_Satus__c Piclist field as 'Closed', all Contact Con_Status__c field should be 'Closed'.
2. if Con_Status__c field in all three Contact record is selected as 'Closed' Account picklist Acc_Satus__c Field Should be 'Closed'.
give me a code with Helper class
ASIF ALIASIF ALI
Hiii Himanshu ,
 This is for your first scenario
trigger trigger_task4 on Account (before update) {
    set<Id> accId = new set<Id>();
    list<account> accList = new list<account>();
    list<contact> conList = new list<contact>();
    list<contact> conToUpdate = new list<contact>();
    map<Id,list<contact>> accMap = new map<Id,list<contact>>();
    for(account acc : trigger.new)
    {
       accId.add(acc.Id); 
        system.debug('Is this working Anything ??');
    }
    conList = [select Id, accountId, lastname, con_status__c from contact where accountId IN:accId];
     system.debug('Are we getting contacts ??');
     system.debug(conList);
    for(contact con : conList)
    {
       if(!accMap.containsKey(con.accountId))
       		accMap.put(con.accountId, new list<contact>());
       accMap.get(con.AccountId).add(con);
    }
    accList = [Select Id, Name, Acc_Status__c from account where Id IN:accMap.keySet()];
     system.debug(accmap);
    for(account acc :accList)
    {
        if(acc.Acc_Status__c == 'closed')
        {
            system.debug('Just checking for contact block ??');
            for(contact con : accMap.get(acc.Id))
        {
            con.con_status__c = 'closed';
            conToUpdate.add(con);
            system.debug('Inside the update list block');
        }
            system.debug(conToUpdate);
        } 

    }

    if(conToUpdate.size()>0)
        update conToUpdate;
}

 
ASIF ALIASIF ALI
This is for both scenario,
modify little bit if required,
trigger trigger_task4 on Account (before update) {
    set<Id> accId = new set<Id>();
    list<account> accList = new list<account>();
    list<contact> conList = new list<contact>();
    list<contact> conToUpdate = new list<contact>();
      list<contact> conToUpdate2 = new list<contact>();
    map<Id,list<contact>> accMap = new map<Id,list<contact>>();
    for(account acc : trigger.new)
    {
       accId.add(acc.Id); 
        system.debug('Is this working Anything ??');
    }
    conList = [select Id, accountId, lastname, con_status__c from contact where accountId IN:accId];
     system.debug('Are we getting contacts ??');
     system.debug(conList);
    for(contact con : conList)
    {
       if(!accMap.containsKey(con.accountId))
       		accMap.put(con.accountId, new list<contact>());
       accMap.get(con.AccountId).add(con);
    }
    accList = [Select Id, Name, Acc_Status__c from account where Id IN:accMap.keySet()];
     system.debug(accmap);
    for(account acc :accList)
    {
        if(acc.Acc_Status__c == 'closed')
        {
             system.debug('Just checking for contact block ??');
            for(contact con : accMap.get(acc.Id))
        {
            con.con_status__c = 'closed';
            conToUpdate.add(con);
            system.debug('Inside the update list block');
        }
            system.debug(conToUpdate);
        } 
        
        if(acc.acc_status__c != 'closed')
        {
            for(contact con:accMap.get(acc.Id))
            {
                if(con.con_status__c != 'closed')
                    contoUpdate2.add(con);
            }
            if(conToUpdate.size()>0)
                acc.acc_status__c = 'Closed';
            update acc;
        }
    }

    if(conToUpdate.size()>0)
        update conToUpdate;
}

 
Ajay K DubediAjay K Dubedi
Hi himanshu,

You can use the below code:

<<<<----Contact trigger --->>>>>
 
trigger contactTrigger on Contact ( before update) {
    
    if(Trigger.isBefore && Trigger.isupdate){
        ContactStatus.statusUpdateContact(Trigger.new);
    }
}

<<<<---Contact Helper class--->>>>
public class ContactStatus {
    public static void statusUpdateContact(List<Contact> contactList){
        try{
        if(contactList.size() > 0){
            Integer count = 0;
            Set<Id> accIds = new Set<Id>();
            for(Contact con: contactList){
                if(con.AccountId != NULL){
                    accIds.add(con.AccountId);
                }
            }
            System.debug('subhasis'+accIds);
            List<Account> updateList = new List<Account>();
            List<Account> accountList = new List<Account>();
            accountList = [SELECT Id,Name,Acc_Satus__c,(SELECT Id,Con_Status__c FROM Contacts) FROM Account WHERE Id IN : accIds LIMIT 50000];
            System.debug('Name'+accountList);
            
            if(accountList.size() > 0){
                for(Account accObject : accountList){
                    System.debug('size'+accObject.Contacts.size());
                    if(accObject.Contacts.size() > 0){
                        for(Contact con: accObject.Contacts){
                            System.debug('satus'+con.Con_Status__c);
                            if(con.Con_Status__c == 'Closed'){
                                count += 1;
                                System.debug('count'+count);
                            }
                        }
                    }
                    if(accObject.Contacts.size() == count){
                        System.debug('subhasis');
                        accObject.Acc_Satus__c = 'Closed';
                        updateList.add(accObject);
                    }
                    count = 0;
                }
                if(updateList.size()>0)
                    update updateList; 
            }
        }
        }catch(Exception exp){
            System.debug('Exception Cause'+exp.getCause()+'Exception lineNumber'+exp.getLineNumber());
        }
    }
}

<<<<<<----Account Trigger--->>>>>
trigger TriggerAccount on Account ( before update) {

    
    if(trigger.isBefore && trigger.isUpdate){
        AccountSatus.statusUpdate(Trigger.new);
    }
}

<<<<----Account Helper class----->>>>>
public class AccountSatus {

    public static void statusUpdate(List<Account> accList){
        try{
        if(accList.size() > 0){
            Set<Id> accIds = new Set<Id>();
            for(Account accObject : accList){
                accIds.add(accObject.Id);
            }
            List<Contact> contactList = new List<Contact>();
            contactList = [SELECT Id,Name,Con_Status__c,AccountId FROM Contact WHERE Account.Acc_Satus__c = 'Closed' AND AccountId IN : accIds LIMIT 50000];
            if(contactList.size() > 0){
                for(Account acc : accList){
                    for(Contact con : contactList){
                        if(acc.id == con.AccountId){
                            con.Con_Status__c = acc.Acc_Satus__c;
                        }
                    }
                }
                update contactList;
            }
        }
        }catch(Exception exp){
            System.debug('Exception cause'+exp.getCause()+'LineNumber'+exp.getLineNumber());
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi