+ Start a Discussion
symantecAPsymantecAP 

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

Navatar_DbSupNavatar_DbSup

Hi,

      Below is the sample code for trigger to fulfill your requirement.

 

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 = MAnagerNAme;
	}
}
}

  Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

symantecAPsymantecAP

I tried it But it doesnot update the Manage look up field :(

Navatar_DbSupNavatar_DbSup

Hi,

    Sorry, i missed the update command in the sample code:

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 = MAnagerNAme;
update c;
}
}
}

 

symantecAPsymantecAP

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;

Navatar_DbSupNavatar_DbSup

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;
}
}
}

symantecAPsymantecAP

Hi Here is my final code  after modifications 

trigger updateManager on Contact (before insert) {
for ( Contact c : trigger.new) {
if(c.createdby.Name != 'xx' )
{
Id MAnagerID= [ select id,ManagerId from user where id=: Userinfo.getUserId()][0].Managerid;

c.manager__c= MAnagerID;
//update c;
}
}
}

 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.

Ritesh AswaneyRitesh Aswaney

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

 

trigger updateManager on Contact (before insert) {
Id[] createdByIds = new Id[]{};
for ( Contact c : trigger.new)
createdByIds.add(c.CreatedById);

Map<Id, User> createdMap = new Map<Id,User>([ select id,ManagerId from user where id IN :createdByIds]);

for(Contact c: trigger.new)
{ if(c.createdbyId != null) c.manager__c= createdMap.get(c.CreatedById).ManagerId; } }

}