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
Nikhil SomvanshiNikhil Somvanshi 

The below Trigger is not working

Hello Masters,

My requirement here is to create a Trigger on Contact which, in case of change of record ownership, populates a field Previously Owned By with name of the previous owner of the record. Please find my code below and reflect some suggestions to make it work correctly.

trigger PreviousOwnerPopulate on Contact (before update) 
{
Set <ID> PreviousId = new Set<ID>();

for (contact co: trigger.new)
{
for (Contact con: trigger.old)
{

if(co.OwnerID != con.OwnerID)
{
PreviousId.add(con.OwnerID);
}
}

}

map<id,string> mapforPreviousIdname = new map<id,string> ();

for (User u : [select id, name from User where id in:PreviousId])
{
mapforPreviousIdname.put(u.id,u.name);
}

for (contact c : trigger.new)

if (mapforPreviousIdname.size() > 0 )
{
c.Previously_Owned_By__c = mapforPreviousIdname.get(c.OwnerID);
}

Best Answer chosen by Nikhil Somvanshi
Pradeep SinghPradeep Singh
Hi, 
You can use below code
trigger PreviousOwnerPopulate on Contact (before update) 
{
	
	set<id> userIdSets = new set<id>();
	for(Contact c : trigger.new){
		if(trigger.oldmap.get(c.id).ownerId != c.OwnerId){
			userIdSets.add(trigger.oldmap.get(c.id).ownerId);
		}
	}
	Map<id,user> userIdNameMap = new Map<id,user>([select id,name from user where id in:userIdSets]);
	
	for(Contact c : trigger.new){
		if(trigger.oldmap.get(c.id).ownerId != c.OwnerId){
			c.Previously_Owned_By__c = userIdNameMap.get(trigger.oldmap.get(c.id).ownerId);
		}
	}
	
}
Please let me know if you face any issue.
If this solves your problem , please mark it as best answer/solved
 

All Answers

Pradeep SinghPradeep Singh
Hi, 
You can use below code
trigger PreviousOwnerPopulate on Contact (before update) 
{
	
	set<id> userIdSets = new set<id>();
	for(Contact c : trigger.new){
		if(trigger.oldmap.get(c.id).ownerId != c.OwnerId){
			userIdSets.add(trigger.oldmap.get(c.id).ownerId);
		}
	}
	Map<id,user> userIdNameMap = new Map<id,user>([select id,name from user where id in:userIdSets]);
	
	for(Contact c : trigger.new){
		if(trigger.oldmap.get(c.id).ownerId != c.OwnerId){
			c.Previously_Owned_By__c = userIdNameMap.get(trigger.oldmap.get(c.id).ownerId);
		}
	}
	
}
Please let me know if you face any issue.
If this solves your problem , please mark it as best answer/solved
 
This was selected as the best answer
Pradeep SinghPradeep Singh
replace 14th line by c.Previously_Owned_By__c = userIdNameMap.get(trigger.oldmap.get(c.id).ownerId).name;
Nayana KNayana K
trigger PreviousOwnerPopulate on Contact (before update) 
{
Set <ID> PreviousId = new Set<ID>();

for (contact co: trigger.new)
{
if(co.OwnerID != trigger.oldMap.get(co.Id).OwnerID)
{
PreviousId.add(con.OwnerID);
}

}

map<id,String> mapforPreviousIdname = new map<id,string> ();

for (User u : [select id, name from User where id in:PreviousId])
{
mapforPreviousIdname.put(u.id,u.name);
}
Contact objOldCon;
for (contact c : trigger.new)
{
objOldCon= trigger.oldMap.get(c.Id);
if(c.OwnerID != objOldCon.OwnerID && mapforpreviousidname.containsKey(objOldCon.OwnerId))
{
c.Previously_Owned_By__c = mapforPreviousIdname.get(objOldCon.OwnerID);
}

} 
}