You need to sign in to do that
Don't have an account?
symantecAP
Trigger to update a look up field with Manager iD
Hi All
I have a look up filed called Manager on CONTACT which is a look up to USER Object. I need to update the Manager field with the creatdby users Manager.
Here is my code so far kindly help
trigger updateManager on Contact (before insert) { for ( Contact c : trigger.new) { if(c.CreatedBy != null) { c.Manager__c = UserInfo.getmanagerId(); } } }
Thanks in ADVANCE
AP
Hi,
Below is the sample code for trigger to fulfill your requirement.
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
I tried it But it doesnot update the Manage look up field :(
Hi,
Sorry, i missed the update command in the sample code:
Its not updating still. My Manager filed is a look up field and.. I have a question as to Y cant we have ManagerID for Manager__c; Y are we providing ManagerName.
Id MAnagerID= [ select id,ManagerId from user where id=: Userinfo.getUserId()].Managerid;
system.debug('****' +MAnagerId);
string MAnagerNAme= [ select id,Name from user where id=: MAnagerID].Name;
system.debug('=================' +MAnagerNAme);
c.Manager__c = MAnagerID;
update c;
Hi,
This is the updated sample code.change the Manager Name to manager id and then it works fine.
trigger updateManager on Contact (before insert) {
for ( Contact c : trigger.new) {
if(c.CreatedBy != null)
{
Id MAnagerID= [ select id,ManagerId from user where id=: Userinfo.getUserId()][0].Managerid;
string MAnagerNAme= [ select id,Name from user where id=: MAnagerID][0].Name;
system.debug('=================' +MAnagerNAme);
c.Manager__c = MAnagerID;
update c;
}
}
}
Hi Here is my final code after modifications
What I dont get is . It doesnot wok when I give c.createdby!= NULL. I do not understand why doesnt it takes null.
This works fine.
In a before insert trigger, any more than first degree references (lookup fields) will evaluate to null (perhaps http://cloudnow.wordpress.com/2011/04/04/apex-bloopers-why-art-thou-null/ explains it a bit better)
so c.CreatedById will not be null, but c.CreateBy.Name will be Null, as that value is not available in the contex just yet.
This value will be available in say a before update trigger.
Also this trigger isnt buklified, if you intend for it to be used en masse, then here goes