+ Start a Discussion
Sudhir_MeruSudhir_Meru 

Update field for first time

Hi,

 

  I am writting a triigger to update values based on the picklist value. 

 

 There is a fields Renewal which has picklist of ( Yes and No ) based on the pick list value another field will get updated which is Discount Program

 

 My Requirement now is Discount Program must set value only for first time next time it must not set 

 

  i.e if user select Renewal (Yes) for first time it must set Discount Program to DSP next time when your select No or Yes from Renewal it must set to NSP

 

 These two fields are on Oppertunities,   Renewal_c and Discount_c

 

 

trigger NewDiscountProgramUpdate on Opportunity (before update) 
{
// Loop through the incoming records
for (Opportunity o : Trigger.new) {

 if ( o.Renewal_Opportunity__c == 'Yes' && 
      o.discount_program__c ==  'NSP' ) 
  {
    o.discount_program__c = 'DSP';
  }
 else
      {
    o.discount_program__c = 'NSP';
  }

}

Please Suggest me

 

 

  

Best Answer chosen by Admin (Salesforce Developers) 
Manos SpanoudakisManos Spanoudakis

How about doing this with a workflow and field updates action ?

I would use another field as a flag (i.e. renewal_set_to_nsp). 

 

So the first time the workflow will also set the flag field and in your workflow rule you will also check the value of renewal_set_to_nsp.

All Answers

Manos SpanoudakisManos Spanoudakis

How about doing this with a workflow and field updates action ?

I would use another field as a flag (i.e. renewal_set_to_nsp). 

 

So the first time the workflow will also set the flag field and in your workflow rule you will also check the value of renewal_set_to_nsp.

This was selected as the best answer
Jerun JoseJerun Jose

Try the code below.

 

trigger NewDiscountProgramUpdate on Opportunity (before update){
	// Loop through the incoming records
	for (Opportunity o : Trigger.new){
		if(o.Renewal_Opportunity__c == 'Yes' && o.discount_program__c == null){
			o.discount_program__c = 'DSP';
		}else if(o.Renewal_Opportunity__c == 'Yes' && o.discount_program__c ==  'NSP' && trigger.oldmap.get(o.id).Renewal_Opportunity__c != o.Renewal_Opportunity__c){
			o.discount_program__c = 'NSP';
		}
	}
}

 

Sudhir_MeruSudhir_Meru

 

Wonder full your suggestion worked for me. :) Apperciate your help