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
StaciStaci 

trigger to validate empty fields

I'm trying to write a trigger to make sure certain fields are not empty based on the status that is chosen.  I have several different types of fields I'm trying to validate.  My code is below but it doesn't seem to be validating anything

Resolution_Detail__c = RTF
Workaround_ICA__c = RTF
Defect_Type_Multi__c = multipicklist
Declined_Reason__c= picklist
NPI_Numbertext__c = plain text
NPI_Tracking_Link__c = URL
CPI_Numbertext__c = plain text
CPI_Tracking_Link__c = URL
Change_Numbertext__c = plain text
CPI_Tracking_Link__c = URL





 
trigger CWStatusValidation on Case (before update, before insert)
{

              
        for (Case objCase : Trigger.new)
        {
           //If Status is any of the following and Resolution Detail is empty, prompt to fill in.
           if((objCase.Status == 'Closed - Cancelled' || objCase.Status == 'Closed - Unable to Reproduce' || objCase.Status == 'Closed - Duplicate'
           || objCase.Status == 'Recurring Incident') && objCase.Resolution_Detail__c == '')            
           {
                trigger.new[0].addError('Please fill in the Resolution Detail for this case.');
           }
              //If Status is any of the following and ICA is empty, prompt to fill in.
              if((objCase.Status == 'Pending - RCA' || objCase.Status == 'Pending - Customer Validation' 
              || objCase.Status == 'Pending - Dealer Validation' || objCase.Status == 'Pending - Evaluation') && objCase.Workaround_ICA__c == '')             
              {
                trigger.new[0].addError('Please fill in the Workaround / ICA for this case.');
              }
                    //If Status = Pending - CPI, fill in Resolution Details, CPI Number and CPI Tracking Link
                    if(objCase.Status == 'Pending - CPI' && objCase.CPI_Numbertext__c == ' ' && objCase.CPI_Tracking_Link__c == ' ' && objCase.Resolution_Detail__c == '' )            
                    {
                        trigger.new[0].addError('Please fill in the Resolution Details, CPI Number and CPI Tracking Link for this case.');
                    }
                         //If the Status = Closed - Declined, fill in Resolution Detail, Defect Type and Declined Reason.
                         if(objCase.Status == 'Closed - Declined' && objCase.Resolution_Detail__c == '' && objCase.Defect_Type_Multi__c == '' && objCase.Declined_Reason__c == '')
                         {
                           trigger.new[0].addError('Please fill in the Resolution Details, Defect Type and Declined Reason for this case.');
                         }
                            //If the Status = Pending - NPI, fill in Resolution Detail, NPI Number and NPI Tracking Link.
                            if(objCase.Status == 'Pending - NPI' && objCase.Resolution_Detail__c == '' && objCase.NPI_Numbertext__c == null && objCase.NPI_Tracking_Link__c == null)
                            {
                               trigger.new[0].addError('Please fill in the Resolution Details, NPI Number and NPI Tracking Link for this case.');
                            }
                                //If the Status = Pending - Change, fill in Resolution Detail, Change Number and Change Tracking Link.
                                if(objCase.Status == 'Pending - Change' && objCase.Resolution_Detail__c == '' && objCase.Change_Number__c == '' && objCase.Change_Tracking_Link__c == '')
                                {
                                   trigger.new[0].addError('Please fill in the Resolution Details, Change Number and Change Tracking Link for this case.');
                                }
                                    //If the Status is any of the following, fill in Resolution Detail and Defect Type.
                                    if((objCase.Status == 'Closed - Resolved' || objCase.Status == 'Closed - No Response') && objCase.Resolution_Detail__c == '' && objCase.Defect_Type_Multi__c == '' )
                                    {
                                       trigger.new[0].addError('Please fill in the Resolution Details and Defect Type for this case.');
                                    }
           
         }       
}
Best Answer chosen by Staci
Matt BeshanyMatt Beshany
Staci,

My apologies if I'm way off on this, as I am not yet a certified Developer, but I think your issue might be because you are looking for a null-value by using the ' ' versus using something like the ISBLANK function. In my various calls with SFDC I've found that often times, the system won't properly identify the null-value when you try to target it using ' ', hense the need for the ISBLANK function. I hope this helps...

~Matt

All Answers

Amritesh SahuAmritesh Sahu
Hi Staci,

Always write bulkified trigger.
Here inside loop, you are only adding error to the first record in the Trigger.New.
Please change all "trigger.new[0].addError" to "objCase.addError".

Thanks,
Amritesh
Matt BeshanyMatt Beshany
Staci,

My apologies if I'm way off on this, as I am not yet a certified Developer, but I think your issue might be because you are looking for a null-value by using the ' ' versus using something like the ISBLANK function. In my various calls with SFDC I've found that often times, the system won't properly identify the null-value when you try to target it using ' ', hense the need for the ISBLANK function. I hope this helps...

~Matt
This was selected as the best answer
StaciStaci
Thank you both!  @Matt Beshany that worked, thank you!
mjohnson-TICmjohnson-TIC
Just to keep in mind, long text fields must be evaluated <> '' while other fields are evaluated <> null.
Gokul  BharatiGokul Bharati
Hi Staci,

Before going to triggers,if there is no complex validation as in case of this we can use validation rules(OOB)

Thanks,
Gokul
Matt BeshanyMatt Beshany
Awesome! I'm glad I could help.