+ Start a Discussion
sherazsheraz 

how to update multiple field upon save by user in trigger

Hi,

I am new to salesforce and i have to write trigger on custom object"Prod" and they purpose of that trigger is that when user enter data in field then its should update two more fields upon saving the record.

here is the example:

custom object = Prod

field 1 = field (this is text field)

field 2 = pick list field(option that should be populate is App)

field 3 = field(this text field)

if user enter in field 1 then its should poulate the field 2 = app and update the same value in field 3 = field 1.

please help me 

thanks

 

SLockardSLockard

I'm not sure of the exact situations you might need, but your trigger should be like this:

 

trigger prodUpdate on Prod__c(before insert, before update)
{
	if(Trigger.isInsert)
	{
		for (Prod__c prod : Trigger.New)
		{
			if (prod.field1__c != null)
			{
				prod.field2__c = 'App';
				prod.field3__c = prod.field1__c;
			}
		}
	}
	else
	{
		for (Prod__c prod : Trigger.New)
		{
			Prod__c oldProd = Trigger.oldMap.get(prod.Id);
			
			if (prod.field1__c != oldProd.field1__c)
			{
				if (prod.field1__c != null)
				{
					prod.field2__c = 'App';
				}
				else
				{
					prod.field2__c = '';
				}
				prod.field3__c = prod.field1__c;
			}
		}
	}
}