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
Prakhyat sapraPrakhyat sapra 

Create the opportunity whose state is closewon & account is !null then update closedate__c on account as todat date by trigger handler

Create the opportunity whose state is closewon & account is !null then update closedate__c on account as todat date by trigger handler ??
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Prakhyat,

Can you specify your requirement more clearly. Does user created opportunity manully or do we need some automation for this. if any opportunity is created with closed won then update the account closeddate to todays date. Is this your requirement.

Is it only for create scenerio or for update as well?

Thanks,
 
Harshit Kumar 27Harshit Kumar 27
Hi Prakhyat,

Kindly refer to the below code -> 

// Trigger => 

trigger OpportunityTrigger on Opportunity(after Insert) {
    if(Trigger.isAfter){
        if(Trigger.isInsert){
            OpportunityTriggerHandler.afterInsert(Trigger.new);
        }
    }
}

// Handler Class =>

public class OpportunityTriggerHandler {
    public static void afterInsert(List<Opportunity> oppList){
        Set<Id> oppIdSet = new Set<Id>();
        for(Opportunity o : oppList){
            oppIdSet.add(o.AccountId);
        }
        List<Account> accList = [SELECT Id, Name, Close_Date__c FROM Account WHERE Id IN : oppIdSet];
        List<Account> aList = new List<Account>();
        for(Opportunity o : oppList){
            for(Account acc : accList){
                if(o.StageName == 'Closed Won'){
                    acc.Close_Date__c = System.today();
                    aList.add(acc);
                    // System.debug(aList);
                }
            }
        }
        update aList;
    }
}

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

Regards,
Harshit Kumar