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
Ker Vin.ax1237Ker Vin.ax1237 

Bypassing validation rules

My Leads have a validation rule such that once someone views a Lead, they can't leave the status as "Open" (as in if the Status is "Open" and the Creation DateTime is not the current date time, then trigger an error message).

 

Now, we are using the Web-to-Lead to pump questions from the website into SFDC directly. The Web-to-Lead basically writes the contact information and their query into Leads (queries are written to a non visible field called " QA"). I wrote  a trigger executing on "After insert" such that a Task will be created, the QA from Lead will be copied into the Comments in the Task and then the QA field will be cleared.

 

My problem starts here. The validation rule doesn't allow me to save the "QA" as blank because the status is Open. Is there a way to bypass this validation rule just for this scenario? Or is there a better way?

 

Any comments will be appreciated. 

Best Answer chosen by Admin (Salesforce Developers) 
kibitzerkibitzer

In terms of the validation rule, you may have the logic in the validation rule wront. Since you don't want an error if the Description is being cleared, you may wants something like:

 

(ISPICKVAL(Status,'Open') && CreatedDate < NOW()) && NOT(ISCHANGED(Description) && ISBLANK(Description))

 

In terms of the trigger - It looks lik you are always updating the leads, but aren't always changing the description.

 

My suggestion is to put in some debug statements to note when you are and are not changing the description, then see whether your logic is correct.

 

All Answers

Starz26Starz26

maybe use ISNEW() = false in the validation rule

Ker Vin.ax1237Ker Vin.ax1237

Sorry, didn't work. Still hit the same error

ISPICKVAL(Status,'Open') && ISNEW() == false

 

apparently changing after the insert makes isnew() to false anyway....

kibitzerkibitzer

How about rewriting the validation rule to bypass validation if the QA field is being changed to null? (Using ISCHANGED on the field)?

 

Dan

 

Ker Vin.ax1237Ker Vin.ax1237

I assume you meant something like this?

(ISPICKVAL(Status,'Open') && CreatedDate < NOW()) || ISCHANGED(Description)

 

The LeadSource for the trigger is of type 'Test'. In other words, the trigger should only run if the Lead Source = 'Test'. I tried manually creating a Lead. If the source is Test, I get the same error. Do you mind looking at this trigger and telling me what I'm missing?

 

TestTrigger: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 00QO0000000uAHkMAM; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please change Lead Status from 'Open': []

 

 

trigger TestTrigger on Lead (after insert) {

//get current date/time and add one day for reminder
Date now = Date.today();
Datetime timeNow = Datetime.now();
Date whenDue = now.addDays(1);
Datetime whenToRemind = timeNow.addDays(1);

//get keyset mapping for the Leads to update
Set<Id> idsToUpdate = trigger.newmap.keySet();
List<Lead> leadsSelected = [select Id, OwnerId, Description from Lead where Id in :idsToUpdate and LeadSource = 'Test'];

if (!leadsSelected.isEmpty()) {
List<Task> tasksToCreate = new List<Task>();
List<Lead> leadsToUpdate = new List<Lead>();
String commentsField;

for(Lead leadCheck : leadsSelected) {
if (leadCheck.Description == null) {
commentsField = '';
} else{
commentsField = leadCheck.Description;
}
tasksToCreate.add(new Task( WhoId = leadCheck.Id,
Description = 'My Question\n' + commentsField,
Subject = Test',
Status = 'Not Started',
isReminderSet = true,
ActivityDate = whenDue,
ReminderDateTime = whenToRemind));

if (leadCheck.Description != null) {
leadCheck.Description = '';
leadsToUpdate.add(leadCheck);
}
}
update leadsToUpdate;
if (!tasksToCreate.isEmpty()) {insert tasksToCreate;}
}

kibitzerkibitzer

In terms of the validation rule, you may have the logic in the validation rule wront. Since you don't want an error if the Description is being cleared, you may wants something like:

 

(ISPICKVAL(Status,'Open') && CreatedDate < NOW()) && NOT(ISCHANGED(Description) && ISBLANK(Description))

 

In terms of the trigger - It looks lik you are always updating the leads, but aren't always changing the description.

 

My suggestion is to put in some debug statements to note when you are and are not changing the description, then see whether your logic is correct.

 

This was selected as the best answer
Ker Vin.ax1237Ker Vin.ax1237

Ah thanks a lot! The stuff works now. The trigger is as designed, as I only update the leads back when the description is set to null. Thanks again!