+ Start a Discussion
Toby TarczyToby Tarczy 

Process Builder - Inserting a field based on a lookup field value

Hi

I am new to process builder, and with some help I managed to create 2 processes. 

I have a process whereby i would like to populate the primary filed of an object to be equal to the lookup value of contacts from an account. Here is the structure:

Object - Work Request Resource
Case if Work_Request_Resource.Contact__c is not blank 

Action - update and edit Work_Request_Resource.Resource_Name_cc to equal Work_Request_Resource.Contact__c

I was also thinking of a trigger but I can't get the trigger to update the primary field. 

The reason why I am taking this round about way is so that I can see the Contact Name on lookup lists, and  not just an ID

My preference would be to use Process Builder, however I am open to triggers

Many thanks in advance 
Best Answer chosen by Toby Tarczy
Neetu_BansalNeetu_Bansal
Hi Toby,

You will not be able to update the resource name with Contact Name, as the look up field will contain id only. You need to write the trigger for this.
The sample code for trigger would be like this:
trigger WorkResourceTrigger on Work_Request_Resource__c( before insert, before update )
{
	Set<Id> contactIds = new Set<Id>();
	for( Work_Request_Resource__c res : trigger.new )
	{
		if( res.Contact__c != null )
		{
			contactIds.add( res.Contact__c );
		}
	}
	
	Map<Id, Contact> contactMap = new Map<Id, Contact>([ Select Id, Name from Contact where Id IN :contactIds ]);
	
	for( Work_Request_Resource__c res : trigger.new )
	{
		if( res.Contact__c != null )
		{
			res.Resource_Name_c = contactMap.get( res.Contact__c ).Name;
		}
	}
}
You need to update field and Object API Name as per your object model. You can also contact me either on my email id: neetu.bansal.5@gmail.com or Skype Id: neetu.bansal.5

Let me know if any other information is required.

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi Toby,

You will not be able to update the resource name with Contact Name, as the look up field will contain id only. You need to write the trigger for this.
The sample code for trigger would be like this:
trigger WorkResourceTrigger on Work_Request_Resource__c( before insert, before update )
{
	Set<Id> contactIds = new Set<Id>();
	for( Work_Request_Resource__c res : trigger.new )
	{
		if( res.Contact__c != null )
		{
			contactIds.add( res.Contact__c );
		}
	}
	
	Map<Id, Contact> contactMap = new Map<Id, Contact>([ Select Id, Name from Contact where Id IN :contactIds ]);
	
	for( Work_Request_Resource__c res : trigger.new )
	{
		if( res.Contact__c != null )
		{
			res.Resource_Name_c = contactMap.get( res.Contact__c ).Name;
		}
	}
}
You need to update field and Object API Name as per your object model. You can also contact me either on my email id: neetu.bansal.5@gmail.com or Skype Id: neetu.bansal.5

Let me know if any other information is required.

Thanks,
Neetu
This was selected as the best answer
Toby TarczyToby Tarczy
Thank you for your help and a great solution