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

Update Opportunity Owner

I have an approval process which is triggered as soon as an opportunity is saved,for which i have written a trigger.
I want to update my opportunity owner name to the approver name after the approval is done.
I tried doing it using the field update in approval actions, but since owner is a lookup field i am not able to write a formula to update it.

Please let me know how can i get it. Below is my trigger:

trigger OpportunityApprovalSubmit on Opportunity(after insert){ 
  if(Trigger.isInsert)    {    
     List<Approval.ProcessSubmitRequest> apRegList = new List<Approval.ProcessSubmitRequest>();       
     Map<Id,RecordType> oppRecTypes = new Map<Id,RecordType>([select Id from RecordType where SobjectType='Opportunity' 
                and IsActive=true                                        
                and Name in ('Advanced')]);       
     for(Opportunity tempOpportunity: Trigger.New)        {        
        if(oppRecTypes.get(tempOpportunity.RecordTypeId) != null)            {         
           Approval.ProcessSubmitRequest approvalRequest = new Approval.ProcessSubmitRequest();               
           approvalRequest.setComments('Submitted for Approval');               
   if(apRegList != null && apRegList.size()>0)       
   Approval.ProcessResult[] approvalProcess = Approval.process(apRegList); 

Thanks in advance
Best Answer chosen by imishra
I have resolved it.
Created a checkbox in the backend which is checked during the approval action and then fire a trigger as below:
trigger UpdateOwner on Opportunity (before update) {

     Opportunity c = new Opportunity();  
     Map<String, Opportunity> contactMap = new Map<String, Opportunity>();  
     for (Opportunity contact : { 
        Opportunity acnt = [select Name,LastModifiedBy.Name,Vertical_Opportunity_Owner__c from Opportunity where Id=:contact.Id];     
        if(acnt.Vertical_Opportunity_Owner__c != null)         
        contact.OwnerId = acnt.Vertical_Opportunity_Owner__c;