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
Debendra Ray 8Debendra Ray 8 

APEX Code for Owner of Opportunity - AN Urgent Reponse Needed

Dear All,
I've a typical situation on hand - There is frequent change of ownership of opportunities in our Org between Sales & Solutions team and it's imprative for us to track the last time Sales Team had changed the ownership of the opportunity to the Solutions Team.
Appreciate, if someone could please suggest a solution for this problem through a Trigger - A Skeletal apex code to achieve the same would be much appreciated.

Thanks & Regards,
Debendra

 
GauravGargGauravGarg

Hi Deb,

Could you please explain the requirement in detail .

Thanks,
Gaurav
skype: gaurav62990

Nithesh NNithesh N
Hi Debendra, 

Have you tried setting the "field history tracking" for "Opportunity Owner" field? 

It tracks the both Old and New values for that field that changed overtime. 

If it Still doesn't satisfy you, Let me know I will try to provide code that suits your requirement.
Debendra Ray 8Debendra Ray 8
Hi, I’ve enabled the field history tracking for the Opportunity Owner field – However, please could you provide me the SOQL query which will retrieve the Old and New Values of the Opportunity Owner and also suggest, how these values could be stored for future reference in the record. Thanks & Regards, Debendra Ray Head Enterprise IT projects [cid:_2_0C3AAB780C3AA938001D1E6B65257C7F] Read the Inspiring Story of Entrepreneurial Growth of mjunction : http://www.mjunction.in/mjunctionstory …………………………………………………………… mjunction services limited Godrej Waterside, 3rd Floor, Tower 1, Plot V Block DP, Sector V, Salt Lake, Kolkata - 700091. …………………………………………………………… Landphone: +91 33 66106100 (Board) Landphone: +91 33 66106307 (Direct) Handphone: +91 85840 08270 Fax: +91 33 44091808 …………………………………………………………… Corporate Website: www.mjunction.in ……………………………………………………………
GauravGargGauravGarg

Hi Deb,

You can use OpportunityHistory Object which maintain the record changes for every opportunity records. 

Please go through the Object structure:
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_opportunityhistory.htm

Hope this helps!!!

Thanks,
Gaurav
Skype: gaurav62990

Nithesh NNithesh N
You Can try this Soql Query
 
SELECT Opp.OldValue, Opp.NewValue, Opp.Field FROM OpportunityFieldHistory Opp

As for Storing these values in the record, I believe they are already stored in a Related list, Which you should Add it into your Page Layout ( look for "Opportunity Field History" and place it on the layout.

Let me know if it works

Best, 
Nithesh
Nithesh NNithesh N
If you only want Owner field, then filter it 
Select Opp.OldValue, Opp.NewValue, Opp.Field From OpportunityFieldHistory Opp Where Opp.Field = 'Owner'

 
Debendra Ray 8Debendra Ray 8
Hi, Thanks for your help – Please could you explain, how to retrieve the ID for the OldValue & the NewValue for the Owner field of the concerned Opportunity. An early response will help. Thanks & Regards, Debendra Ray Head Enterprise IT projects [cid:_2_0C3AAB780C3AA938001D1E6B65257C7F] Read the Inspiring Story of Entrepreneurial Growth of mjunction : http://www.mjunction.in/mjunctionstory …………………………………………………………… mjunction services limited Godrej Waterside, 3rd Floor, Tower 1, Plot V Block DP, Sector V, Salt Lake, Kolkata - 700091. …………………………………………………………… Landphone: +91 33 66106100 (Board) Landphone: +91 33 66106307 (Direct) Handphone: +91 85840 08270 Fax: +91 33 44091808 …………………………………………………………… Corporate Website: www.mjunction.in ……………………………………………………………
Nithesh NNithesh N
Try this....
List<OpportunityFieldHistory> history = new List<OpportunityFieldHistory>();
for(OpportunityFieldHistory  t : [Select Opp.OpportunityId, Opp.OldValue, Opp.NewValue, Opp.Field From OpportunityFieldHistory Opp Where Opp.Field = 'Owner']){
    if(t.NewValue instanceof ID){
        history.add(t);
        System.debug('Old: '+ t.OldValue +' New: ' +t.NewValue);
    }    
    
}

 
Debendra Ray 8Debendra Ray 8
Hi, Thanks again – However, when I tried to assign the OldValue to the OwnerId of the Opportunity using the below code , I’m getting an error -Illegal assignment from Object to Id : for(Opportunity oppr: opportunitiesToUpd ) { for(OpportunityFieldHistory opprFldHistory: history){ oppr.OwnerId = history.OldValue; } } Please could you help. Thanks & Regards, Debendra Ray Head Enterprise IT projects [cid:_2_0C3AAB780C3AA938001D1E6B65257C7F] Read the Inspiring Story of Entrepreneurial Growth of mjunction : http://www.mjunction.in/mjunctionstory …………………………………………………………… mjunction services limited Godrej Waterside, 3rd Floor, Tower 1, Plot V Block DP, Sector V, Salt Lake, Kolkata - 700091. …………………………………………………………… Landphone: +91 33 66106100 (Board) Landphone: +91 33 66106307 (Direct) Handphone: +91 85840 08270 Fax: +91 33 44091808 …………………………………………………………… Corporate Website: www.mjunction.in ……………………………………………………………
Nithesh NNithesh N
You are getting that Error, Because you are trying to assign a string to Id. First you need to convert it into ID. Try This...
Map<Id,OpportunityFieldHistory> history = new Map<Id,OpportunityFieldHistory>();
for(OpportunityFieldHistory  t : [Select Opp.OpportunityId, Opp.OldValue, Opp.NewValue, Opp.Field From OpportunityFieldHistory Opp Where Opp.Field = 'Owner']){
    if(t.NewValue instanceof ID){
        history.put(t.OpportunityId , t);
        System.debug('Old: '+ t.OldValue +' New: ' +t.NewValue);
    }    
    
}

for(Opportunity oppr: opportunitiesToUpd ) { 
  if(history.containsKey(oppr.Id)){
      oppr.OwnerId = Id.valueOf(history.get(oppr.Id).OldValue);
    } 
}

This Should do the trick. Notice that I have changed the history(list) to map.
Debendra Ray 8Debendra Ray 8
Hi Nithesh, Thanks very much – However, it seems valueof method is not supported for the ID primitive type as it’s throwing an error that variable Id not defined in the line : oppr.OwnerId = Id.valueOf(history.get(oppr.Id).OldValue); Please suggest. Thanks & Regards, Debendra Ray Head Enterprise IT projects [cid:_2_0C3AAB780C3AA938001D1E6B65257C7F] Read the Inspiring Story of Entrepreneurial Growth of mjunction : http://www.mjunction.in/mjunctionstory …………………………………………………………… mjunction services limited Godrej Waterside, 3rd Floor, Tower 1, Plot V Block DP, Sector V, Salt Lake, Kolkata - 700091. …………………………………………………………… Landphone: +91 33 66106100 (Board) Landphone: +91 33 66106307 (Direct) Handphone: +91 85840 08270 Fax: +91 33 44091808 …………………………………………………………… Corporate Website: www.mjunction.in ……………………………………………………………
Nithesh NNithesh N
Can you post what error you are getting ?