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
QuanchiQuanchi 

Apex Trigger Help

Hello,

 

I have and Apex trigger that works fine but I need to make a change to it.......and I can't figure out how to do it.

The code works great when creating a new record.....I also need it to execute when an update it made.

 

The code gets the user's managers name and populates a field in the opportunity object.  I need it to update this field when the opportunity owner changes.  Any help would be most appreiciated.

 

Thanks.

 

******************************************************************************************

 

 

 

trigger Opp_Owner on Opportunity (before insert, before update ) {
/*****************************************************************************
Name     : Update Owner Manager on the opportunity from the User object

Purpose  : defaults the Owner Manager field of the opportunity from the User

******************************************************************************/
    
  //holds User Ids
  Set<Id> setUserIds = new Set<Id>();
 
  //holds a list of Users
  List<User> lstUser = new List<User>();
 
  //holds key value pairs of User Id and User Object
  Map<Id, User> mapUserObj = new Map<Id, User>();

  //holds User object
  User UserObj;
  
  //For each opportunity being inserted add User Id value in in Set of User Ids.
  for(Opportunity oppObj : Trigger.new){
    if(oppObj.OwnerId != null){
      setUserIds.add(oppObj.OwnerId);
    }
  }
 
  
  //Fetch all Users whose Id is in the Set.
  lstUser = [select Id, Name, Manager_Full_Name__c, ManagerId from User where Id in :setUserIds Limit 1000];
  if(lstUser.size() == 0){
    return; 
  }
 
  //Add these fetched Users in a Map <User Id, User object>
  for(User usrObj : lstUser){
    mapUserObj.put(usrObj.Id, usrObj); 
  }
 
  //for each opportunity being inserted get User from the map and update the field values
  for(Opportunity oppObj : Trigger.new){
    //get User object
    if(oppObj.OwnerId != null){
      if(mapUserObj.containsKey(oppObj.OwnerId)){
        UserObj = mapUserObj.get(oppObj.OwnerId);
        //map opportunity fields to User fields
        oppObj.Opportunity_Owner_s_Manager__c = UserObj.Manager_Full_Name__c;
      } 
    }
  }

 }

 

 

Best Answer chosen by Admin (Salesforce Developers) 
nandurinanduri

Try this 

 

 

 

trigger Opp_Owner on Opportunity (before insert, before update )
{
/*************************************************​****************************
Name : Update Owner Manager on the opportunity from the User object
Purpose : defaults the Owner Manager field of the opportunity from the User
**************************************************​****************************/

//holds User Ids
Set<Id> setUserIds = new Set<Id>();

//holds a list of Users
List<User> lstUser = new List<User>();

//holds key value pairs of User Id and User Object
Map<Id, User> mapUserObj = new Map<Id, User>();
//holds User object
User UserObj;

//For each opportunity being inserted add User Id value in in Set of User Ids.
for(Opportunity oppObj : Trigger.new){
if(oppObj.OwnerId != null){
setUserIds.add(oppObj.OwnerId);
}
}


//Fetch all Users whose Id is in the Set.
lstUser = [select Id, Name, Manager_Full_Name__c, ManagerId from User where Id in :setUserIds Limit 1000];
if(lstUser.size() == 0){
return;
}

//Add these fetched Users in a Map <User Id, User object>
for(User usrObj : lstUser){
mapUserObj.put(usrObj.Id, usrObj);
}

if(trigger.isinsert)
{

//for each opportunity being inserted get User from the map and update the field values
for(Opportunity oppObj : Trigger.new)
{
//get User object
if(oppObj.OwnerId != null){
if(mapUserObj.containsKey(oppObj.OwnerId)){
UserObj = mapUserObj.get(oppObj.OwnerId);
//map opportunity fields to User fields
oppObj.Opportunity_Owner_s_Manager__c = UserObj.Manager_Full_Name__c;
}
}
}
}
if(trigger.isupdate)// && trigger.new[0].ownerid !=trigger.old[0].ownerid)
{
//map opportunity fields to User fields

for(Integer i = 0; i<trigger.new.size(); i++)
{
//get User object
if(trigger.new[i].OwnerId != trigger.old[i].ownerid){
if(mapUserObj.containsKey(trigger.new[i].OwnerId)){
UserObj = mapUserObj.get(trigger.new[i].OwnerId);
//map opportunity fields to User fields
trigger.new[i].Opportunity_Owner_s_Manager__c = UserObj.Manager_Full_Name__c;
}
}
}

}
}

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

You have to make the separate condition for insert and update.

Try the below code as reference:

trigger Opp_Owner on Opportunity (before insert, before update )

{

/*****************************************************************************

Name     : Update Owner Manager on the opportunity from the User object

Purpose  : defaults the Owner Manager field of the opportunity from the User

******************************************************************************/

if(trigger.isinsert)

  //holds User Ids

  Set<Id> setUserIds = new Set<Id>();

 

  //holds a list of Users

  List<User> lstUser = new List<User>();

 

  //holds key value pairs of User Id and User Object

  Map<Id, User> mapUserObj = new Map<Id, User>();

  //holds User object

  User UserObj;

 

  //For each opportunity being inserted add User Id value in in Set of User Ids.

  for(Opportunity oppObj : Trigger.new){

    if(oppObj.OwnerId != null){

      setUserIds.add(oppObj.OwnerId);

    }

  }

 

 

  //Fetch all Users whose Id is in the Set.

  lstUser = [select Id, Name, Manager_Full_Name__c, ManagerId from User where Id in :setUserIds Limit 1000];

  if(lstUser.size() == 0){

    return;

  }

 

  //Add these fetched Users in a Map <User Id, User object>

  for(User usrObj : lstUser){

    mapUserObj.put(usrObj.Id, usrObj);

  }

 

  //for each opportunity being inserted get User from the map and update the field values

  for(Opportunity oppObj : Trigger.new)

  {

    //get User object

    if(oppObj.OwnerId != null){

      if(mapUserObj.containsKey(oppObj.OwnerId)){

        UserObj = mapUserObj.get(oppObj.OwnerId);

        //map opportunity fields to User fields

        oppObj.Opportunity_Owner_s_Manager__c = UserObj.Manager_Full_Name__c;

      }

    }

  }

 }

 if(trigger.isupdate && trigger.new[0].ownerid !=trigger.old[0].ownerid)

 {

 // Put your desire code for updation here.

 }

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

QuanchiQuanchi

Thanks for your quick response.  I added what you suggested and get an error:  Error: Compile Error: unexpected token: if at line 63 column 1

 

Below is my updated code, any ideas?

 

************************************************

 

trigger Opp_Owner on Opportunity (before insert, before update )

{

/*************************************************​****************************

Name     : Update Owner Manager on the opportunity from the User object

Purpose  : defaults the Owner Manager field of the opportunity from the User

**************************************************​****************************/

if(trigger.isinsert)

  //holds User Ids

  Set<Id> setUserIds = new Set<Id>();

 

  //holds a list of Users

  List<User> lstUser = new List<User>();

 

  //holds key value pairs of User Id and User Object

  Map<Id, User> mapUserObj = new Map<Id, User>();

  //holds User object

  User UserObj;

 

  //For each opportunity being inserted add User Id value in in Set of User Ids.

  for(Opportunity oppObj : Trigger.new){

    if(oppObj.OwnerId != null){

      setUserIds.add(oppObj.OwnerId);

    }

  }

 

 

  //Fetch all Users whose Id is in the Set.

  lstUser = [select Id, Name, Manager_Full_Name__c, ManagerId from User where Id in :setUserIds Limit 1000];

  if(lstUser.size() == 0){

    return;

  }

 

  //Add these fetched Users in a Map <User Id, User object>

  for(User usrObj : lstUser){

    mapUserObj.put(usrObj.Id, usrObj);

  }

 

  //for each opportunity being inserted get User from the map and update the field values

  for(Opportunity oppObj : Trigger.new)

  {

    //get User object

    if(oppObj.OwnerId != null){

      if(mapUserObj.containsKey(oppObj.OwnerId)){

        UserObj = mapUserObj.get(oppObj.OwnerId);

        //map opportunity fields to User fields

        oppObj.Opportunity_Owner_s_Manager__c = UserObj.Manager_Full_Name__c;

      }

    }

  }

 }

 if(trigger.isupdate && trigger.new[0].ownerid !=trigger.old[0].ownerid)

 {

 //map opportunity fields to User fields
        oppObj.Opportunity_Owner_s_Manager__c = UserObj.Manager_Full_Name__c;
 }

}

nandurinanduri

Try this 

 

 

 

trigger Opp_Owner on Opportunity (before insert, before update )
{
/*************************************************​****************************
Name : Update Owner Manager on the opportunity from the User object
Purpose : defaults the Owner Manager field of the opportunity from the User
**************************************************​****************************/

//holds User Ids
Set<Id> setUserIds = new Set<Id>();

//holds a list of Users
List<User> lstUser = new List<User>();

//holds key value pairs of User Id and User Object
Map<Id, User> mapUserObj = new Map<Id, User>();
//holds User object
User UserObj;

//For each opportunity being inserted add User Id value in in Set of User Ids.
for(Opportunity oppObj : Trigger.new){
if(oppObj.OwnerId != null){
setUserIds.add(oppObj.OwnerId);
}
}


//Fetch all Users whose Id is in the Set.
lstUser = [select Id, Name, Manager_Full_Name__c, ManagerId from User where Id in :setUserIds Limit 1000];
if(lstUser.size() == 0){
return;
}

//Add these fetched Users in a Map <User Id, User object>
for(User usrObj : lstUser){
mapUserObj.put(usrObj.Id, usrObj);
}

if(trigger.isinsert)
{

//for each opportunity being inserted get User from the map and update the field values
for(Opportunity oppObj : Trigger.new)
{
//get User object
if(oppObj.OwnerId != null){
if(mapUserObj.containsKey(oppObj.OwnerId)){
UserObj = mapUserObj.get(oppObj.OwnerId);
//map opportunity fields to User fields
oppObj.Opportunity_Owner_s_Manager__c = UserObj.Manager_Full_Name__c;
}
}
}
}
if(trigger.isupdate)// && trigger.new[0].ownerid !=trigger.old[0].ownerid)
{
//map opportunity fields to User fields

for(Integer i = 0; i<trigger.new.size(); i++)
{
//get User object
if(trigger.new[i].OwnerId != trigger.old[i].ownerid){
if(mapUserObj.containsKey(trigger.new[i].OwnerId)){
UserObj = mapUserObj.get(trigger.new[i].OwnerId);
//map opportunity fields to User fields
trigger.new[i].Opportunity_Owner_s_Manager__c = UserObj.Manager_Full_Name__c;
}
}
}

}
}

This was selected as the best answer
QuanchiQuanchi

Perfect!!!!  Thank you so much!!!