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
JaredPHJaredPH 

Help? Trigger okay except when null value in field

I've gotten some help and worked out a trigger. It does what I want with updating a field based on a comparison of a date ("accepted__c") and datetime field ("payment_ref__c"). I just realized that it gives an error if one of the comparison date or date/time fields is blank. Is there an easy way to insert a line so it skips everything if either of those fields are blank? Also, am I going to run into trouble if I use an opportunity record type that doesn't include one of the fields referenced in the trigger?

 

Thanks for any help you can give!!

 

Here is the trigger:

 

Trigger LateApp on opportunity(before update)
{
datetime accdt;
date payref;
list<opportunity> opp=new list<opportunity>();
for(opportunity o : trigger.new)
{
accdt=trigger.new[0].Accepted__c.date();
payref=trigger.new[0].Payment_ref__c.adddays(45);
if(payref.daysBetween(accdt.date()) >= 0 && payref.daysBetween(accdt.date()) < 30)
{
o.Late_App__c ='Late1';
}
if(payref.daysBetween(accdt.date()) >= 30 && payref.daysBetween(accdt.date()) < 60)
{
o.Late_App__c ='Late2';
}
if(payref.daysBetween(accdt.date()) >=60)
{
o.Late_App__c ='Late3';
}
if(payref.daysbetween(accdt.date()) <-15)
{
o.Late_App__c='';
}
opp.add(o);
}
}
Best Answer chosen by Admin (Salesforce Developers) 
Jerun JoseJerun Jose

Hi,

 

You will just need to have an outer if condition that checks for nulls in the two date fields.

Also, I noticed that your trigger was incompletely built to handle bulk requests.

 

Below is the code with the change requested and my fix to handle bulk data.

 

Trigger LateApp on opportunity(before update)
{
	datetime accdt;
	date payref;
	list<opportunity> opp=new list<opportunity>();
	for(opportunity o : trigger.new)
	{
		if( o.Accepted__c !=null && o.Payment_ref__c != null )
		{
			accdt = o.Accepted__c.date(); payref = o.Payment_ref__c.adddays(45);
			if(payref.daysBetween(accdt.date()) >= 0 && payref.daysBetween(accdt.date()) < 30)
			{
				o.Late_App__c ='Late1';
			}
			if(payref.daysBetween(accdt.date()) >= 30 && payref.daysBetween(accdt.date()) < 60)
			{
				o.Late_App__c ='Late2';
			}
			if(payref.daysBetween(accdt.date()) >=60)
			{
				o.Late_App__c ='Late3';
			}
			if(payref.daysbetween(accdt.date()) <-15)
			{
				o.Late_App__c='';
			}
			opp.add(o);
		}
	}
}

 

All Answers

Jerun JoseJerun Jose

Hi,

 

You will just need to have an outer if condition that checks for nulls in the two date fields.

Also, I noticed that your trigger was incompletely built to handle bulk requests.

 

Below is the code with the change requested and my fix to handle bulk data.

 

Trigger LateApp on opportunity(before update)
{
	datetime accdt;
	date payref;
	list<opportunity> opp=new list<opportunity>();
	for(opportunity o : trigger.new)
	{
		if( o.Accepted__c !=null && o.Payment_ref__c != null )
		{
			accdt = o.Accepted__c.date(); payref = o.Payment_ref__c.adddays(45);
			if(payref.daysBetween(accdt.date()) >= 0 && payref.daysBetween(accdt.date()) < 30)
			{
				o.Late_App__c ='Late1';
			}
			if(payref.daysBetween(accdt.date()) >= 30 && payref.daysBetween(accdt.date()) < 60)
			{
				o.Late_App__c ='Late2';
			}
			if(payref.daysBetween(accdt.date()) >=60)
			{
				o.Late_App__c ='Late3';
			}
			if(payref.daysbetween(accdt.date()) <-15)
			{
				o.Late_App__c='';
			}
			opp.add(o);
		}
	}
}

 

This was selected as the best answer
JaredPHJaredPH

Perfect! Thanks for your help.