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
Ricky 007Ricky 007 

Trigger to update account field if the contract has an account.

Hi, I am new to salesforce and i am facing an issue in writing trigger.

The Requirement is like this:
If the Contract has an account then the Status(custom field,picklist type) field of the corresponding  account must be set to Active.

Please help in resolving the issue.
Any help would be appreciated.


Trigger to update account field if the contract has an account.
Best Answer chosen by Ricky 007
Sujeet PatelSujeet Patel
Hii Rajat 
Finally I full fill your requirement may be this will work.
If account has not contract then account field will be update to Inactive if account has contract this will update to active
trigger AcctiveAccount on Contract (after insert,after delete)
{
        
    if(trigger.isInsert)
    {
    
         List<Account> ac=new List<Account>();
        
            for(Contract con:trigger.new)
            {
                if(con.accountid<>null)
                {
                    Account act=new Account();
                    act.Id=con.Accountid;
                    ac.add(act);
                }
            }
            
            List<Account> acout=[Select Id,Active__c,(Select id from Contracts) from Account where id in: ac];
            
    
    
        List<Account> ac_Update=new List<Account>();
        for(Account act:acout)
        {
        
        act.Active__c ='Active';
        ac_Update.add(act);
    
        }
    
        update ac_Update;    
    }
    if(trigger.isDelete)
    {
        List<Account> ac_list=new List<Account>();
        
            for(Contract con:trigger.old)
            {
                if(con.accountid<>null)
                {
                    Account act=new Account();
                    act.Id=con.Accountid;
                    ac_list.add(act);
                }
            }
    
        List<Account> acout=[Select Id,Active__c,(Select id from Contracts) from Account where id in: ac_list];
    
        List<Account> ac_Up=new List<Account>();
        for(Account ac:acout)
        {
            if(ac.contracts.size()==0)
            {
                ac.Active__c='InActive';
                ac_Up.add(ac);
            }
        }
        update ac_Up;
    }
}

All Answers

Sujeet PatelSujeet Patel
Hii Rajat 
Finally I full fill your requirement may be this will work.
If account has not contract then account field will be update to Inactive if account has contract this will update to active
trigger AcctiveAccount on Contract (after insert,after delete)
{
        
    if(trigger.isInsert)
    {
    
         List<Account> ac=new List<Account>();
        
            for(Contract con:trigger.new)
            {
                if(con.accountid<>null)
                {
                    Account act=new Account();
                    act.Id=con.Accountid;
                    ac.add(act);
                }
            }
            
            List<Account> acout=[Select Id,Active__c,(Select id from Contracts) from Account where id in: ac];
            
    
    
        List<Account> ac_Update=new List<Account>();
        for(Account act:acout)
        {
        
        act.Active__c ='Active';
        ac_Update.add(act);
    
        }
    
        update ac_Update;    
    }
    if(trigger.isDelete)
    {
        List<Account> ac_list=new List<Account>();
        
            for(Contract con:trigger.old)
            {
                if(con.accountid<>null)
                {
                    Account act=new Account();
                    act.Id=con.Accountid;
                    ac_list.add(act);
                }
            }
    
        List<Account> acout=[Select Id,Active__c,(Select id from Contracts) from Account where id in: ac_list];
    
        List<Account> ac_Up=new List<Account>();
        for(Account ac:acout)
        {
            if(ac.contracts.size()==0)
            {
                ac.Active__c='InActive';
                ac_Up.add(ac);
            }
        }
        update ac_Up;
    }
}
This was selected as the best answer
Ricky 007Ricky 007
Hi Patel,
Thanks a lot