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
madhu_ramadhu_ra 

Field Value is Giving null in Before Update Trigger

Hi All,

I'm writing a 'before update' trigger on Opportunity. There I'm reffering for 'FiscalYear' and 'FiscalQuarter' which are basically will change when the 'Close Date' is changed. I could able to track the Close Date change correctly, but those two fields are giving 'null' values in the trigger context. Below is my code snippet for this.

if(Trigger.isBefore)
{
  List<Opportunity> updatedOppList = [SELECT id,          name,CurrencyIsoCode,Probability,ScheduledRevenue__c,FiscalQuarter,FiscalYear,CloseDate,StageName FROM Opportunity WHERE Id IN :   Trigger.newMap.keySet()];
Integer currentFiscalYear,currentQuarter;
for(Opportunity o : updatedOppList)
{
   Opportunity newOpp = Trigger.newMap.get(o.Id);
   Opportunity oldOpp = Trigger.oldMap.get(o.Id);
   if(newOpp.CloseDate != oldOpp.CloseDate)
   { 
      currentFiscalYear = newOpp.FiscalYear; 
      currentQuarter = newOpp.FiscalQuarter;
      //More Stuffs
   }
}
}

 


I tried with o.FiscalYear and o.FiscalQuarter, but then it's giving older values of those fields even I have retrieved them with Trigger.newMap.keyset(). Please advice me where am doing the mistake.

Thanks
Madhura

hitesh90hitesh90

Hi madhura,

 

Try to use After update trigger with following code.

 

if(Trigger.isAfter){
	List<Opportunity> updatedOppList = [SELECT id,name,CurrencyIsoCode,Probability,CurrencyIsoCode,FiscalQuarter,FiscalYear,CloseDate,StageName FROM Opportunity WHERE Id IN :   Trigger.newMap.keySet()];
	Integer currentFiscalYear,currentQuarter;
	for(Opportunity o : updatedOppList){
		Opportunity newOpp = Trigger.newMap.get(o.Id);
		Opportunity oldOpp = Trigger.oldMap.get(o.Id);            
		if(newOpp.CloseDate != oldOpp.CloseDate){        
			currentFiscalYear = o.FiscalYear; 
			currentQuarter = o.FiscalQuarter;
			//More Stuffs
		}
	}
}

 

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

Devender MDevender M

Hi do like this

 

if(Trigger.isBefore)
{
for(Opportunity o : trigger.newMap.values())
{
   Opportunity newOpp = Trigger.newMap.get(o.Id);
   Opportunity oldOpp = Trigger.oldMap.get(o.Id);
   if(newOpp.CloseDate != oldOpp.CloseDate)
   { 
      currentFiscalYear = newOpp.FiscalYear; 
      currentQuarter = newOpp.FiscalQuarter;
      //More Stuffs
   }
}
}
madhu_ramadhu_ra

Thanks for the reply hitesh90,

 

I don't like to use after update trigger here since inside the trigger I have to update a field of the opportunity. So if I use after update trigger it may land up with unwanted trigger firing as well as causing govner limits. Any trick appreciate. 

 

Thanks, 

Madhura

madhu_ramadhu_ra
Thanks for the response Devender M,
Actually I'm retrieving two child objects as well in the first query eventhough I haven't posted it. So I can't iterate through trigger.newMap.values() since I need that parent child relation for the logics.
Devender MDevender M

Query inside a map, and iterate over the map.

hitesh90hitesh90

Hi madhura,

 


For that you have to use static variable to handle the recursive trigger call.
see below Link:
Controlling Recursive Triggers

 

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

madhu_ramadhu_ra
hi hitesh90,

Actually what I had is an 'after update' trigger and with the static field for control the recursive trigger firing. Now since we are having number of triggers on same object we thought to refactor the triggers based on govner limits, efficiency, etc. That's why I looked for a help from the community to check whether I'm missing something here.

I tried all options with 'before update' trigger but seems like some fields are not get populating in the 'before' context. I reverted back to 'after update' trigger with the comtroller static flag.

Thanks all for the kind responses.
Madhura