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
Naveen KvpNaveen Kvp 

how to write a account trigger toupdate status picklistfield (cold call.prospecting,existing) on account change picklist field to existing,whenever order is having atleast one order here order is lookup with account.

how to write a account trigger toupdate status picklistfield (cold call.prospecting,existing) on account change picklist field
to existing,whenever order is having atleast one order here order is lookup with account.
Vatsal KothariVatsal Kothari
Hi Naveen,

You can refer below code:
trigger updateAccountStage on CustomObj__c (after insert) {

    List<Account> accList = new List<Account>();
    Set<Id> accountIds = new Set<Id>();
    
    for(CustomObj__c order : trigger.new){
        if(order.Account__c != null){
            accountIds.add(order.Account__c);//API Name of account lookup field
        }
    }

    for (AggregateResult ar : [Select Count(Id) numRecs, Account__c acctId From CustomObj__c Where Account__c In :accountIds Group By Account__c]) {
        Id actId = (Id) ar.get('acctId');
        Account acct = new Account(Id = actId);
        Integer Count = (Integer) ar.get('numRecs');
        if(count > 0){
            acct.StatusField__c = 'Existing';
            accList.add(acct);
        }
        else if(count <= 0){ // change the status picklist value to something else if yoou want
        }
    }
    
    if(accList.Size() > 0){
        update accList;
    }

}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal