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_on Contact

Both Account and Contact has a picklist field Status_c with values Open,Close,InProgress 
account record has 3 child record in contact
if all 3 contact has picklist value = 'close', then account picklist value will get updated to 'close'.
SATHISH REDDY.SATHISH REDDY.
trigger AccountStatucUpdate on Contact (After insert, After update {
    Set<Id> accountIdList = new Set<Id>();
    List<Account> accUpdateList = new List<Account>();
    if (Trigger.isAfter) {   
        if (Trigger.isInsert) {
            for(Contact c : Trigger.new){
                if(c.statuc__c == 'Close' && c.accountId != null)
                    accountIdList.add(c.accountId);
            }
            if(!accountIdList.siEmpty())
            for(Account acc : [Select Id, Status__c, (select id, statuc__c from contacts != Close) FROM Account Where Id IN: accountIdList AND Status__c != 'Close']){
                if(acc.Contacts.isEmpty()){
                    acc.Status__c = 'Close';
                    accUpdateList.add(acc);
                }
            }
            if(!accUpdateList.isEmpty())
                update accUpdateList;
        }       
        else if (Trigger.isUpdate){
            for(Contact c : Trigger.new){
                if(c.statuc__c == 'Close' && c.accountId != null && trigger.OldMap.get(c.Id).Status__c != trigger.newMap.get(c.Id).Status__c)
                    accountIdList.add(c.accountId);
            }
            if(!accountIdList.siEmpty())
            for(Account acc : [Select Id, Status__c, (select id, statuc__c from contacts != Close) FROM Account Where Id IN: accountIdList AND Status__c != 'Close']){
                if(acc.Contacts.isEmpty()){
                    acc.Status__c = 'Close';
                    accUpdateList.add(acc);
                }
            }
            if(!accUpdateList.isEmpty())
                update accUpdateList;
        }
    }
}

The code should be something in the lines of this. However, the best way is to have this logic in a separate handler class and then invoke that class within this trigger. You can find many examples online something like this https://developer.salesforce.com/forums/?id=906F00000009EfuIAE

Please mark it as the best answer if this helps.

Cheers!
Sathish
Maharajan CMaharajan C
Hi,

Please try the below trigger

trigger AccountStatucUpdate on Contact (After insert, After update) {
    Set<Id> accIds = new Set<Id>();
    List<Account> accListtoUpdate = new List<Account>();
    if (Trigger.isInsert)
    {
        for(Contact con : Trigger.new)
        {
            if(con.AccountId != null && con.status__c == 'Close')
            {
                accIds.add(con.AccountId);
            }
        }
    }
    else if(Trigger.isUpdate)
    {
        for(Contact c : Trigger.new)
        {
            if(c.status__c == 'Close' && c.accountId != null && trigger.OldMap.get(c.Id).Status__c != trigger.newMap.get(c.Id).Status__c)
                { 
                    accIds.add(c.AccountId);
                }
        }
    }
    
    if(!accIds.IsEmpty())
    {
        List<Account> accList = [Select id,(select id, status__c from Contacts) from Account where id IN: accIds];
        
        for(Account acc : accList)
        {
            Integer unclosedCons = 0;
            for(Contact cont : acc.Contacts)
            {
                if(cont.status__c != 'Close')
                {
                    unclosedCons++;
                }
            }
            
            if(unclosedCons == 0)
            {
                acc.Status__c = 'Close';
                accListtoUpdate.add(acc);
            }
        }

    }
    
    if(!accListtoUpdate.isEmpty())
    update accListtoUpdate;

}


Thanks,
Maharajan.C
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi
You can use the below code:
List<Id> AccIds=new List<Id>();
            for(COntact c: Trigger.new)
            {
                if(c.Status__c=='Close' && c.AccountId!=null)
                    AccIds.add(c.accountId);
            }
            List<Account> accList=new List<Account>([select id,name,status__c,(Select id from contacts where status__c!='Close') from account where status__c!='Close']);
            for(Account acc: accList)
            {
                if(acc.contacts.isEmpty())
                acc.status__c='Close';
            }
            update accList;

Thanks
Ajay K DubediAjay K Dubedi
Hi Himanshu huske,

You can use the below code:

<------ Apex 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());
        }
    }
}



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

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

Thanks,
Ajay Dubedi
Cristina AlvaradoCristina Alvarado
Tree trimming is a professional occupation that has a wide range of applications. It is the procuring and the running of https://www.fortwaynetreepros.com/. When trees need to be trimmed, the owner of the property can choose to hire a commercial tree care company to undertake the work or they can do it themselves using a variety of hand tools or machinery that they have purchased like chainsaws, loppers, trimmers and so on.
Cristina AlvaradoCristina Alvarado
This might sound arcane, but it's really pretty cool. Open your PDFs side by side and they'll seamlessly merge into one website (https://2pdf.com/merge/). Unmerging is just as easy, so it's a great way to make two separate documents look like they're meant to be together.
Ien AdamsIen Adams
Odoo is a unique ERP system provider because it is open-source, meaning its source code is accessible to and may be worked upon by, the internet developer community (more about you can find here https://mobilunity.com/blog/hire-odoo-developers/). Although starting from version 9, it changed to an open-core business model, wherein some capabilities are only available in a commercial enterprise edition, it nonetheless preserves most of its basic functionality in its open-source community edition.