You need to sign in to do that
Don't have an account?
Auto Populate a Custom Lookup Field with the Current User
Hi
We have a custom lookup field that has been created on the Opportunity Line Item object called Product Owner. We would like this to be auto populated with the current user when left blank upon saving.
We've tried to create a trigger to do this but run into errors upon saving the product line
trigger updatefield on OpportunityLineItem (after insert) {
user defaultuser = [select id from user where name = 'default user'];
for (OpportunityLineItem record:trigger.new) {
if(record.Product_Owner__c ==null) {
record.Product_Owner__c = defaultuser.id;
}
}
}
Any help or advice would be much appreciated.
Thanks,
Snita
try this. Userinfo.getUserId() will give you current user id.
All Answers
Please find the below example,
Thanks,
Vinoth
try this. Userinfo.getUserId() will give you current user id.
I tried Ketankumar's trigger and it worked perfectly! Many thanks for your help and quick response.
Triggers working perfectly, we're attempting to write the test class this morning and hitting a null value error.
Initially we didn't create the user and set the class to seealldata=true but realised that this isn't the best way to go about it. However, when we create the user inside the test class the value still returns the null value or doesn't know that the created user inserted the line.
Are we referncing this incorrectly? Any suggestions/advice would be very appreciated.
Thank you,
Snita
Any help is much appreciated!
I wrote the same code and it populates the current user info after I save the record.
I need to pre-populate the current user info when I create the new record.
Below is my trigger code.
trigger populate_current_user on Applications_Time_Tracking__c (before insert) {
// populate the current user name on record creation
for(Applications_Time_Tracking__c tt : Trigger.new){
if(tt.User__c == null){
tt.User__c = UserInfo.getUserId();
}
}
Can you please help.