+ Start a Discussion
Pritam ShekhawatPritam Shekhawat 

trigger for change owner of record

Hello ,
         I have one object Project__c . In this there are one  picklist Assigned_To__c  in this picklist i add manually all user name. So whenever i choose for example Pritam Shekhawat then project owner should be Pritam Shekhawat. Give me a idea what is easy way to do this.
Thanks
Best Answer chosen by Pritam Shekhawat
Pritam ShekhawatPritam Shekhawat
@David ,Puru thanks guys for quick response...I don't want to use custom setting . Take a look this is my code it works fine now. suggests me that is right?
trigger UpdateOwnerID on Project__c(before insert, before update) {
     Set<string> aliasscope = new Set<String>();
      for(Project__c opp : trigger.new)
      {
              aliasscope.add(opp.Assigned_To__c);
       }
    map<string,id> userIdbyAlias = new map<string,id>();  //Keep in mind this will only store one user id per alias
    for(User u : [Select id,Name from user where Name IN :aliasscope])
    {
            userIdbyAlias.put(u.Name,u.Id);
     }
    for (Project__c objOpportunity : Trigger.new)
    {
       

             if (objOpportunity.OwnerId <> userIdByAlias.get(objOpportunity.Assigned_To__c) )           
             {
                   objOpportunity.OwnerId = userIdByAlias.get(objOpportunity.Assigned_To__c);
              }
         
    }

}

 

All Answers

Puru AnnamalaiPuru Annamalai
Hi,

1. Maybe you can create a combinations of these projects and the owners in a LIST custom settings(easy to maintain, and you can add any additional values in future).
2. On a Trigger, use this custom setting values and based on this, populate the AssignedTo value.
Pritam ShekhawatPritam Shekhawat
can you please explain more how to use custom setting in trigger?
or there are another possible way for this?
isualum12isualum12
I think your on the right track with the custome setting in the trigger.  Here is a link to examples and more definition around them for you.  Hope this helps.  Please mark if it does. 
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_custom_settings.htm
Puru AnnamalaiPuru Annamalai
Hi,

Hope below sample help you...

//Create Custom Setting Object and records
Setup -> Develop -> Custom Settings -> New
Fields -> Name,AssignedTo

//Trigger
Trigger TriggerName on Project__c(before insert, before update) {
Map<String, Project_Custom_Setting__c>  csMap = Project_Custom_Setting__c.getAll();
For(Project__c currProj : Trigger.New){
currProj.AssignedTo__c = csMap.get(currProj.Name).AssignedTo__c;
}
}

 
David ZhuDavid Zhu
As Puru mentioned, you can use custom settings to save the dropdown list and use trigger to change the project owner.
the code below might help:
 
Trigger TriggerName on Project__c(before insert, before update) {
      User usr;
      Map<id,User> mapusrs = new Map<id,User>([select id from user]);
         

      For(Project__c currProj : Trigger.New){
            string username = currProj.AssignedTo__c ;
            usr = null;

           for (User u: mapusrs.values()) {
                 usr  = u;
             
                if (usr.name.tolowercase() == username.tolowercase)
                     break;
            }

          if (usr != null)
               currProj.ownerid = usr.id;
      }
}

 
Pritam ShekhawatPritam Shekhawat
@David ,Puru thanks guys for quick response...I don't want to use custom setting . Take a look this is my code it works fine now. suggests me that is right?
trigger UpdateOwnerID on Project__c(before insert, before update) {
     Set<string> aliasscope = new Set<String>();
      for(Project__c opp : trigger.new)
      {
              aliasscope.add(opp.Assigned_To__c);
       }
    map<string,id> userIdbyAlias = new map<string,id>();  //Keep in mind this will only store one user id per alias
    for(User u : [Select id,Name from user where Name IN :aliasscope])
    {
            userIdbyAlias.put(u.Name,u.Id);
     }
    for (Project__c objOpportunity : Trigger.new)
    {
       

             if (objOpportunity.OwnerId <> userIdByAlias.get(objOpportunity.Assigned_To__c) )           
             {
                   objOpportunity.OwnerId = userIdByAlias.get(objOpportunity.Assigned_To__c);
              }
         
    }

}

 
This was selected as the best answer