1

The scenario is I only want to validate the field upon record update not creation.It works successfully when I create or update record manually with validation rules.But it fail when I convert the Lead to Opportunity.

Created_from_Lead__c is Opportunity mapping to indicate that the Opportunity is created from Lead.But I notice there is no value map from Lead.

 IF( OR(ISBLANK(  TEXT(Sold_By_Business_Unit__c)),ISBLANK(Text( Sold_by_Business_Area__c )),ISBLANK(Text( Primary_Project_Group__c ))) && 
     ISNEW() && Created_from_Lead__c, False,
     OR(ISBLANK(  TEXT(Sold_By_Business_Unit__c)),ISBLANK(Text( Sold_by_Business_Area__c )),ISBLANK(Text( Primary_Project_Group__c )))

     )

It fire an exception on after trigger below:

Error: Opportunity_Trigger: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Validation rules message: [] Class.Opportunity_Handler.insertOpportunityLineItem: line 274, column 1 Trigger.Opportunity_Trigger: line 43, column 1

The trigger is to insert Opportunity Line item during Opportunity creation. So I tried to remove validation rules and put the validation on the trigger itself but still it firing after trigger.

The requirement for creation or conversion it should skip validation rules.Only update will fire validation rules.

I already apply this solution on my code but still it fail.

1

This should work, but there is a catch (see below):

OR(ISBLANK( TEXT(Sold_By_Business_Unit__c)),
 ISBLANK(Text( Sold_by_Business_Area__c )),
 ISBLANK(Text( Primary_Project_Group__c ))
) && 
NOT(OR( ISNEW(), Created_from_Lead__c)

The problem was that when you ran AfterInsert trigger, the record was not considered new and ISNEW() had value false, but the formula above would have Created_from_Lead__c = true from the solution you are referencing and will bypass the validation. The catch is that Created_from_Lead__c will keep its value (true) and your validation won't work on updates either unless you'd find the way to change it - e.g. with a Workflow field update...

  • I remove Created_from_Lead__c because the value is false.So I remove it completely although I map it from Lead.So isNew() will not return true if it run after insert trigger?is there anyway I can check if the record is new record? – unidha Apr 15 '16 at 6:04
  • IsNew() is true on a new record creation, it just later in the process when you add some opportunity lines and it updates parent opportunity record the validation fails as it's not a new record any more. And you should not remove Created_from_Lead__c - it supposed to be true if you are populating it from the lead during conversion, at least this is how it's done in the 'solution' – o-lexi Apr 15 '16 at 7:08
  • Okay, now I get it.The exception is fail in Opportunity trigger when it try to insert OpportunityLineItem , so this mean it will consider update the Opportunity itself when try to insert OpportunityLineItem? – unidha Apr 15 '16 at 7:38
  • probably yes as OLI inserts will fire RSF on Oppo and re-execute triggers/VRs. You might consider adding the OLI in a future method/queueable method – cropredy Apr 19 '16 at 2:15
0

The validation rules are running because on lead conversion, a new opportunity is being created. In your code, you can stop creating Opportunity (upon lead conversion) by using the below code.

Database.LeadConvert lc = new database.LeadConvert();
lc.setDoNotCreateOpportunity(true);

Hope this helps.

  • I still need to convert Lead to Opportunity,but I need to skip a validation upon lead conversion. – unidha Apr 15 '16 at 3:24
  • Yes you are not stopping lead conversion. Lead conversion happens. But do you want opportunity creation on lead conversion? – sfdcFanBoy Apr 15 '16 at 3:25
  • Yes.The issue is After Insert trigger is failing. – unidha Apr 15 '16 at 3:37
  • Can you put ISNEW() as the first check in your validation rule and then check other conditions if NOT(ISNEW()) – sfdcFanBoy Apr 15 '16 at 4:07
  • I try to put if(isnew(),false,isblank(customfield) but it still fail.It fail in Opportunity trigger when i try to insert opportunity line item.The error point at line insert opportunitylineitem .My opportunity trigger is try to insert opportunitylineitem during opportunity creation. – unidha Apr 18 '16 at 6:17
0

Here is an example of a Validation I created so it would not check on Lead conversion. This will check the Opportunity on Edit then Save. Both the Lead Source and the PCI fields are picklists

AND(
NOT(ISNEW()),
OR(
AND(ISPICKVAL(LeadSource, "Repo E-mail"), ISBLANK(text(Repo_PCI__c))),
AND(ISPICKVAL(LeadSource, "Repo Web"), ISBLANK(text(Repo_PCI__c))),
AND(ISPICKVAL(LeadSource, "Repo Phone"), ISBLANK(text(Repo_PCI__c))),
AND(ISPICKVAL(LeadSource, "Repo Web"), ISBLANK(text(Repo_PCI__c))),
AND(ISPICKVAL(LeadSource, "Repo Agent Created"), ISBLANK(text(Repo_PCI__c)))
)
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.