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
nisha c onisha c o 

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;
}
}
}
}

PrabhaPrabha
oracleguruoracleguru

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);
}
}
}

PrabhaPrabha

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

 

 
deepabalisfdcdeepabalisfdc

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.

trigger updateEmployeeId on Peoplesoft_User_View__c (before insert, before update) {

for(Peoplesoft_user_view__c p : trigger.New){
User u = new User();
u = [select ID from user where Username =:p.employee_name_text__c];
p.Employee_Name__c = u.ID;
}

}