I am new to coding in Force.com and coding in general.
Object structure:
- Opportunity has a detail object called Pricing__c
- The OwnerId is only on the Opportunity object
- There are two lookup fields on the User object to other users to indicate managers (i.e. the standard ManagerId and custom Senior_Manager__c fields)
- The lookup fields are needed on the Pricing__c object to support an approval process
Lookup fields on the Pricing__c record for Sales_Manager__c and Senior_Manager__c need to populate based on the Opportunity Owner's user record upon insert of a new Pricing__c record. What's the correct way to make this happen?
The logic to execute this needs to exist in a class and that class is called from a trigger. I have the trigger calling the class properly. I need help with writing the class logic for this scenario.
This is what I've written so far:
public with sharing class pricingManagerAndLeader {
public static void updateLookups(List<Pricing__c> pml){
// Get the Opportunity Ids by looping over the Pricing Approval Request records, then..
// Get a list of Opportunity Owner user information from the Opportunity records related to Pricing Approval Request records, then...
// Take the Opportunity Owner user information to populate Sales_Manager__c and Senior_Manager__c lookup fields on Pricing__c records
// Get a list of all Opportunity Ids
Set<Id> optyIds = new Set<Id>();
for (Pricing__c p : pml){
optyIds.add(p.Opportunity__c);
}
// List of Opportunity fields that can be accessed from the Pricing Approval Request object (commented out because I don't know if I need this)
// List<Pricing__c> pToUpdate = new List<Pricing__c>();
// pToUpdate = [SELECT p.Opportunity__r.OwnerId, p.Opportunity__r.Name, p.Opportunity__r.Id, p.Opportunity__c FROM Pricing__c p];
// Since Pricing__c is a detail of master Opportunity, get Pricing__c and User info from the Opportunity
List<Opportunity> optyList = new List<Opportunity>();
optyList = [SELECT o.Owner_Opportunity__c, o.Owner.Senior_Manager__c, o.Owner.ManagerId, o.Owner.Name, o.Owner.Id, o.OwnerId, o.Id, o.Record_Type_Name__c,
(SELECT Id, Name, Opportunity__c, RecordTypeId, Product_Family__c, Sales_Manager__c, VSL__c, Sales_Person__c FROM Pricing__r WHERE (RecordType.Name = 'NA Pricing Request') AND (Opportunity__c IN :optyIds))
FROM Opportunity o
WHERE o.Record_Type_Name__c = 'NA'];
// For each Pricing Approval Request inserted, get o.Owner.ManagerId and o.Owner.Senior_Manager__c and update the Pricing__c fields
for (Opportunity o : optyList){
Pricing__c p = new Pricing__c();
p.Sales_Manager__c = o.Owner.ManagerId;
p.Senior_Manager__c = o.Owner.Senior_Manager__c;
insert p;
}
}//end updateLookups}//end class