You need to sign in to do that
Don't have an account?
prasanth kumar
custoum field not updating with ownername trigger. pelase help
i wrote a trigger that, ownername should be come at representative__C field. ownerid is displaying but firstname or lastname or alias is not coming. please help.
trigger usernametorepresentative1 on employee__c (before insert,before update) { for(employee__C e1:trigger.new) { e1.representative__c=e1.owner.alias; // e1.representative__c=e1.ownerid; // this is coming. // e1.representative__c=e1.owner.lastname; // e1.representative__c=e1.owner.firstname; } }
As Per Shrikant comment, you need query user details. Please find the below code, it will solve your problem.
All Answers
As you are trying to access the Owner Name, you need to query on user object to get the data of Owner.
Cause: In trigger context we only get the obejct related data not their relation object data.
If its helps, please mark as best answer so it will help to other who will serve same problem.
Thanks!
Shrikant is right ,In trigger.new or trigger,old you can have only Id of the reference record like Owner(User ) or any other reference field . So you can directy refer the Id of the record without query .Incase you need any other field value you need to query with the help of that Id .
Please let me know if it helps .
Thank
Manoj
As Per Shrikant comment, you need query user details. Please find the below code, it will solve your problem.
Please try below code. I hope that will help you.
NOTE:- If you want to fatch the related object data in Trigger then you need to run the query again. Then you can fatch the data like below query
ist<employee__c> lstEmp = [select id ,owner.alias,ownerid,owner.lastname,owner.firstname,representative__c from employee__C where id in :setId ];
Please let us know if this will help you.
Thanks
Amit Chaudhary
Yes Arun is absolutly right all reference field value we can get before insert context,only the record id and audit fields will not available in before insert context .