You need to sign in to do that
Don't have an account?

Populating a lookup field using a trigger
hi ,
i have a custom lookup field - field1 which looks up to User object. and another text field - field2 which contains the username. i want to fetch the id of the username from field2 and pass it to field1 . so that lookup field also will contain the same username. field2 is being populated by informatica. i have written this trigger but somehow does not seem to work.
trigger updateEmployeeId on Peoplesoft_User_View__c (after insert, after update) {
if (Trigger.isBefore) {
if (Trigger.isInsert) {
for(Peoplesoft_user_view__c p : trigger.New){
// ppl = [ select employee_name__c from Peoplesoft_User_View__c];
User u = new User();
u = [select ID from user where Username =:p.employee_name_text__c];
p.Employee_Name__c = u.ID;
}
}
}
}
Please look at the order of execution:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm
Prabhan
try something like this...This code snippet is not tested..
trigger updateEmployeeId on Peoplesoft_User_View__c (after insert, after update) {
List<User> userList=new List<User>();
Map<String,Id> userMap=new Map<ID,String>();
List<Peoplesoft_User_View__c> updateList=new List<Peoplesoft_User_View__c>();
if (Trigger.isBefore) {
if (Trigger.isInsert) {
for(Peoplesoft_user_view__c p : trigger.New){
// ppl = [ select employee_name__c from Peoplesoft_User_View__c];
userList.add(p.employee_name_text__c);
}
userList=[select ID,UserName from user where Username IN : userList];
for(User u:userList)
{
userMap.put(u.UserName,u.ID);
}
for(Peoplesoft_user_view__c p : trigger.New){
if(p.employee_name_text__c<>'')
{
p.Employee_Name__c=userMap.get(p.employee_name_text__c);
updateList.add(p.Id);
}
}
Database.update(updateList);
}
}
}
And, if what to see the log of this code,
Administration Setup>>Monitoring>>Debug Logs:
register urself there and create/ update the file and see the log... you will know what is happening...
let me know what it says.
Prabhan
Hi Nisha,
You are writing after trigger and checking for if trigger is isBefore. I think if you give system.debug inside your trigger code and check debug log, that will show that it is not executed.