2

I want to create a trigger on the opportunity object to change the opportunity owner (if their profile is a Service Engineer) to the field called "Sales Rep Owner" which is a lookup user field on the opportunity object that is set in a visual flow. Here is what I have so far, but the Opportunity Owner does not change over to what is put into the Sales Rep Owner field. Any help?

trigger OppyOwnerChange on Opportunity (before update) { 
   Set<Id> oppIds = trigger.newMap.keySet(); 
   Map<Id, User> mapUsers = new Map<Id, User>([Select Id, ProfileId From User Where Id IN (Select OwnerId from Opportunity Where Id IN :oppIds)]);  

   for(Opportunity opp : trigger.new) { 
      User oOwner = mapUsers.get(opp.OwnerId); 

   if(oOwner.ProfileId == '00eU0000000hmKc') { 
      opp.OwnerId = opp.Sales_Rep_Owner__c; 
    } 
  } 
}
2

opp.Createdby.ProfileId is not present on trigger.new. Indeed no fields from parent objects are pulled in.

You'll have to:

  1. Collect the OwnerIds in trigger.new (remember to bulkify!)
  2. Query the ProfileId of each of the OwnerIds
  3. Determine if the queried ProfileId matches your hard-coded value of 00eU0000000hmKc
  • Thank you for commenting so quickly on my post! I will definitely give this a try :) – NewbieDeveloper27 Jul 22 '15 at 22:38
  • I came up with this but it is not working either trigger OppyOwnerChange on Opportunity (before update) { Set<Id> oppIds = trigger.newMap.keySet(); Map<Id, User> mapUsers = new Map<Id, User>([Select Id, ProfileId From User Where Id IN (Select OwnerId from Opportunity Where Id IN :oppIds)]); for(Opportunity opp : trigger.new) { User oOwner = mapUsers.get(opp.OwnerId); if(oOwner.ProfileId == '00eU0000000hmKc') { opp.OwnerId = opp.Sales_Rep_Owner__c; } } } – NewbieDeveloper27 Jul 23 '15 at 20:02
  • Edit original post please and add this code -- it's hard to read in its current state. – Ray Dehler Jul 24 '15 at 6:16
  • Code has been edited above.... – NewbieDeveloper27 Jul 24 '15 at 15:06
1

The CreatedBy is a lookup to User who created the Opportunity. But in triggers you won't get the lookup fields except its id.

So you need to query the fields(ProfileId) from CreatedBy in your trigger and then check it against your id(00eU0000000hmKc).

trigger OpptyOwnerChange on Opportunity (before update) {
    Set<Id> setCreatedById = new Set<Id>();
Map<Id,Id> mapCreatedByIdProfileId = new Map<Id,Id>();
    for(Opportunity opp : Trigger.new){
        setCreatedById.add(opp.CreatedById);
    }
    for(User u : [SELECT Id,ProfileId FROM User WHERE Id IN : setCreatedById]) {
        mapCreatedByIdProfileId.put(u.Id,u.ProfileId);
    }
    for(Opportunity opp : Trigger.new) {
        if(mapCreatedByIdProfileId.get(opp.CreatedbyId) == '00eU0000000hmKc')
            opp.OwnerId = opp.Sales_Rep_Owner__c;
    }
}  

Hope it helps.

  • mapCreatedByIDProfileId is not an established variable.... – NewbieDeveloper27 Jul 23 '15 at 19:09
  • Sorry missed the variable declaration. Updated my answer to include that now. – Vigneshwaran G Jul 24 '15 at 2:39
  • I used the code that you wrote above and it is did not flip the opportunity owner to the user Sales Rep Owner field....therefore the trigger did not do what is was intended to do. Any other ideas? – NewbieDeveloper27 Jul 24 '15 at 15:03
  • Does opportunity have any workflow field updates which change the owner? One more thing, does Sales Rep Owner have Edit access to Opportunity? Because to be owner to an opportunity, Edit access is required. – Vigneshwaran G Jul 24 '15 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.