1
Public static void opptyownermanagerupdate(Opportunity[] Opptylist)
{
   List<Opportunity> Opptylists = new List<Opportunity>();
   List<Opportunity> opptyToUpdate =new List<Opportunity>();
   Set<id> ownerid = new Set<id>();
   For(Opportunity op :Opptylists)
    {
    if(op.StageName=='ClosedWon'&& op.Order_Complete__c) 
     {
       If(ownerid != null && !ownerid.isEmpty())
       {
       ownerid.add(op.owner.id);
       opptytoUpdate.add(op);
       }
       }
       }
     Map<Id, string>  userMap= new Map<Id, string>();
     For(User oppOwner : [Select Id, Manager.Name from User where Id IN :ownerid ])
     {
      userMap.put(oppOwner.Id, oppOwner.Manager.Name);
     }

    For(Opportunity op: Opptylists)
     {
      op.Timestamped_Manager__c = usermap.get(op.owner.id);
     }
}  

I have written the code above to update the Timestamped Manager field with the Opportunity Owner's manager name, but it is getting updated with Opportunity Owner's Id instead. What correction needs to be done here?

4
  • 1
    Is Timestamped Manager a lookup to User or a Text field for the name?
    – BradByte
    Nov 19, 2014 at 12:33
  • 1
    The code towards the end looks OK. If this is being called from a trigger op.ownerId would be better than op.owner.id as related objects are not populated in triggers. But as posted, this class will never do anything because it is iterating over the empty Opptylists rather than the Opptylist that is passed in. Can you check that in the code you are running and update your question?
    – Keith C
    Nov 19, 2014 at 12:37
  • 1
    I don't think it's ever going in that second if statement. What exactly are you trying to achieve with !ownerid.isEmpty()? Also ownerid != null it's always the case since you're instantiating it above - Set<id> ownerid = new Set<id>(); so you don't have to check for that. Nov 19, 2014 at 12:39
  • Timestamped manager is text field on Opportunity Object Nov 19, 2014 at 12:51

2 Answers 2

1

op.Timestamped_Manager__c = usermap.get(op.owner.id); appears to be the culprit here.

You've stated that the Timestamped_Manager__c field is a text field, in which case you'd likely want to change that to a user lookup. In the standard salesforce UI, user lookups are always displayed by the name of the user.

If you want to keep the field as a textfield, you'll have to use op.owner.name (id that's available) or query for the name field first.

0

Public static void Timestampedmanager(Opportunity[] Opptylist) { List Opptylists = new List(); Set ownerid = new Set(); For(Opportunity op :Opptylists) { if(op.StageName=='ClosedWon'&& op.Order_Complete__c) { If(ownerid != null && !ownerid.isEmpty()) { ownerid.add(op.ownerid);

   }
   }
   }
 Map<Id, string>  userMap= new Map<Id, string>();
 For(User oppOwner : [Select Id, Managerid , Manager.Name from User where Id IN :ownerid ])
 {
  userMap.put(oppOwner.Id, oppOwner.Manager.Name);
 }

For(Opportunity op: Opptylists)
 {
  op.Timestamped_Manager__c = usermap.get(op.ownerid);
  system.debug('ssssssssssss'+op.Timestamped_Manager__c);
 }

}

I have modified the code , still its not working.The Time stamped manager field is empty. I suspect op.Timestamped_Manager__c = usermap.get(op.ownerid); should be updated like op.Timestamped_Manager__c = usermap.get(op.ownerid).Manager.name

but when i do that its throwing error.

can you help.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .