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
Giancarlo AmatiGiancarlo Amati 

field from previous opportunities

Dear Team,
when creating a new Opportunity record for a specific account, I would need to access the value of a custom field from the most recent closed-won opportunity of a certain deal type. 

What would be the best approach here? Building an Opportunity Trigger and use SOQL to access the most recent opportunity? 

Thank you. 


Piyush Gautam 6Piyush Gautam 6
Hi Giancarlo Amati,

Here you need to update a field on Opportunity while creating a record so the best approach is to create a trigger.

Below code will help you:
trigger TriggerName on Opportunity (before insert) {

    Map<Id,Account> accOppMap= new Map<Id,Account>([SELECT Id, (SELECT customField__c FROM Opportunities where stageName= 'CLosedWon' Order By lastmodifiedDate DESC LIMIT 1) FROM Account]);

    For(Opportunity opp:{
	    opp.customField__c= accOppMap.get(opp.AccountId).Opportunities[0].customField__c;
Mark as solved if found helpful.