+ Start a Discussion
prasanth kumarprasanth 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;


 }
}

 
Best Answer chosen by prasanth kumar
Arunkumar RArunkumar R
Hi Prashant,

As Per Shrikant comment, you need query user details. Please find the below code, it will solve your problem.
 
trigger usernametorepresentative1 on employee__c (before insert,before update) 
{
	Set<Id> ownerIds = new Set<Id>();
	for(employee__C e1:trigger.new)
	{
		ownerIds.add(e1.ownerid);
	}
	Map<Id, User> userMap = new Map<Id, User>([ SELECT Id, Alias, FirstName, LastName FROM User where Id in :ownerIds]);
	for(Employee__c currEmp : Trigger.New)
	{
		if(userMap.containsKey(currEmp.OwnerId))
		{
			User usr = userMap.get(currEmp.OwnerId);
			currEmp.representative__c=usr.Alias;
			currEmp.representative__c=currEmp.ownerid;  
			currEmp.representative__c=usr.lastname;
			currEmp.representative__c=usr.firstname;
		}
	}
}

All Answers

Shrikant BagalShrikant Bagal
Hello Prasanth,

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.
T​hanks! 

 
ManojjenaManojjena
Hi Prashant,

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
Arunkumar RArunkumar R
Hi Prashant,

As Per Shrikant comment, you need query user details. Please find the below code, it will solve your problem.
 
trigger usernametorepresentative1 on employee__c (before insert,before update) 
{
	Set<Id> ownerIds = new Set<Id>();
	for(employee__C e1:trigger.new)
	{
		ownerIds.add(e1.ownerid);
	}
	Map<Id, User> userMap = new Map<Id, User>([ SELECT Id, Alias, FirstName, LastName FROM User where Id in :ownerIds]);
	for(Employee__c currEmp : Trigger.New)
	{
		if(userMap.containsKey(currEmp.OwnerId))
		{
			User usr = userMap.get(currEmp.OwnerId);
			currEmp.representative__c=usr.Alias;
			currEmp.representative__c=currEmp.ownerid;  
			currEmp.representative__c=usr.lastname;
			currEmp.representative__c=usr.firstname;
		}
	}
}
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
Hi Prashant,

Please try below code. I hope that will help you.
trigger usernametorepresentative1 on employee__c (before insert,before update) {
    
  Set<String> setId = new Set<String>();	
  for(employee__C e1:trigger.new)
  {
	setId.add(e1.id);
  }

	List<employee__c> lstEmp = [select id ,owner.alias,ownerid,owner.lastname,owner.firstname,representative__c from employee__C where id in :setId ];	
	for(employee__C e1: lstEmp)
	{
		System.debug('---->'+e1.owner.lastname);
		System.debug('---->'+e1.ownerid);
		System.debug('---->'+e1.owner.firstname);
		
		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;


	}
}

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
ManojjenaManojjena
Amit you are right ,However we can not get recordId before insert context  .
 
ManojjenaManojjena
Hi All ,

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 .