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
Babar Hussain 13Babar Hussain 13 

apex class execution

Hi Everyone,

i was struck on the one condition, my requirement is update the contact status Cancelled when Account status is Cancelled but not update if Account status is "Pending"/"In progress". 

I have added my code below , please check let me know how to add that condification or any hints please

 

public class AccountUpdate {
    public void getIds() {
    List<Account> accIds = new List<Account>();
        Set<Id> getIds = new Set<Id>();
        for(Account a :[select Id, Name from Account]) {
            getIds.add(a.Id);   
        }
        System.debug('Print all the Account Ids =>' +getIds);
         //get the related contacts associated to Account
        List<Contact> con = [Select Id, Name, AccountId From Contact Where AccountId IN :getIds];
        System.debug('Print all related Contacts'+con);
        List<Contact> getupdateContacts = new List<Contact>();
        if(Account.Account_Status__c == 'Cancelled'){
            getupdateContacts.add(con);
            }
            update getupdateContacts;
        }
        
    }
    
   
    
    
}
HarshHarsh (Salesforce Developers) 
Hi Babar,

Please try the below code and follow the instructions given in the comment.
 
public class AccountUpdate {
    public void getIds() {
        List<Account> accIds = new List<Account>();
        Set<Id> getIds = new Set<Id>();
        for(Account a :[select Id, Name from Account]) {
            getIds.add(a.Id);   
        }
        System.debug('Print all the Account Ids =>' +getIds);
        //get the related contacts associated to Account
        List<Contact> con = [Select Id, Name, AccountId From Contact Where AccountId IN :getIds];
        System.debug('Print all related Contacts'+con);
        List<Contact> getupdateContacts = new List<Contact>();
        if(Account.Account_Status__c == 'Cancelled'){
            for(Contact condata :con){
            condata.status__c = 'Cancelled';    // instead of  status__c  please put api name of status filed in contact object
                getupdateContacts.add(condata);
        }
        }
        update getupdateContacts;
    }
    
}

Please mark it as Best Answer if the above information was helpful.

Thanks.
 
Abdul KhatriAbdul Khatri
Hi Babar,

I am not sure in which context you are planning to execute this method. The code is not optimized. I have simplified and optimized the code.
 
public class AccountUpdate 
{
    public void getIds() 
    {     
        List<Contact> getupdateContacts = new List<Contact>();
        
        for(Account a : [SELECT Id, Name, (SELECT Id FROM Contacts WHERE Status__c != 'Cancelled') 
                         	FROM Account 
                         	WHERE Account_Status__c = 'Cancelled'])
        {
         	if(a.Contacts != null && a.Contacts.size() > 0)            
            {
                for(Contact cont : a.Contacts)
                {
                	cont.status__c = 'Cancelled';
                    getupdateContacts.add(cont);
                }
            }            
        }
        
        update getupdateContacts;
    }
}

I hope it will help in the long run.

Regards,
Abdul Aziz Khatri