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
Sandesh D GanjareSandesh D Ganjare 

About inserting record using trigger?

hi,
I have master detail look up field "Employee name" and i want to fill this field through the trigger by taking the employye name from the logged in user. so, before savimg the record it takes the name from the logged user name and assign to the "Employee name "  field.

heLIP ME :-)

THANKS
zzunkozzunko
Create a before insert trigger and set the value of the field to the UserInfo.getUserId() for all the records in the trigger.new list. It will populate the value of the field with the currently logged in user.

hope this helps.

zoran
Sandesh D GanjareSandesh D Ganjare
i have this trigger but it doesnt work,
trigger InsertingMD on New_Application__c (before insert) {
  New_Application__c app = new New_Application__c();
  User u = [select FirstName, Lastname from User where Id = :UserInfo.getUserId()];
  contact c = [select id, Name from contact where (FirstName = :u.FirstName AND Lastname = :u.Lastname)];
  app.Employee_Name_c = c.Name;
  insert app;         

}
Ajay K DubediAjay K Dubedi
Hi Sandesh D Ganjare,
since u r trying to assign the firstname and lastname of the curerntly logged in user so u just have to query the currently logged in user name from "UserInfo.getUserId()" which gives the the requested output and then assign the firstnam and lastname to the field. U dont have to query for the contact fields as per ur question requirement.

try the below code:
 
trigger InsertingMD on New_Application__c (before insert) {

 User u = [select FirstName, Lastname from User where Id = :UserInfo.getUserId()];

for(New_Application__c app: trigger.new)
 
  app.Employee_Name_c = u.FirstName+u.Lastname;
 
}
Thanks.
Abhishek BansalAbhishek Bansal
Hi sandesh,

Since Employee name is a lookup field so please reference this field with a Id field.
Use Below code in your trigger :

trigger InsertingMD on New_Application__c (before insert) {

    User u = [select id from User where Id = :UserInfo.getUserId()];

    for(New_Application__c application : trigger.new) {
        application.Employee_Name_c = u.id;
    }
}

Thanks,
Abhishek Bansal.
JeffreyStevensJeffreyStevens
So - in this case - you wouldn't even need the SOQL.  - right?

trigger insertingMD on New_Application__c(before insert) {
    for(New_Applicaiton__c application :trigger.new) {
        application.Employee_Name__c = :userInfo.getUserID();
    }
}
Abhishek BansalAbhishek Bansal
Hi Sandesh,

Yes, Jeffrey is absolutely right.
You do not have to query User but please correct the syntax as we do not use assignment operator while assigning values to another variable.
So please correct Jeffrey code as below :

trigger insertingMD on New_Application__c(before insert) {
    for(New_Applicaiton__c application :trigger.new) {
        application.Employee_Name__c = userInfo.getUserID();
    }
}

@Jeffrey : It is a very very small mistake but still we have to take care of those things :)

Thanks,
Abhishek.
JeffreyStevensJeffreyStevens
Ah Yes. Good catch.