0

I have a validation rule which is running twice (one when creating an Opp, and then in a @future method ) i need it only run the first time, and not the second. Any tip? the problem is with the isnew(), because the first time its a new opp, but the second its not.


validation:

AND (
    NOT(ISNEW()), 
    OM_R1_Number_of_attr_Industry_SubSegment__c = 0, 
    (
        TEXT(CROperatingGroup__c) = 'Health & Public Service' || 
        TEXT(CROperatingGroup__c) = 'Products'
    ), 
    OpportunityValidationBypassBoolean__c 
)

Apex Class:

public static void populatevalueInUserRelatedfield(set<ID> setOpportunity){

        list<Opportunity> lstToUPdate = new list<Opportunity>();        
        if(setOpportunity == null || setOpportunity.size() ==0 ){return;}

        list<Opportunity> lstOppLocal = [Select id, CreatedBy.EmployeeNumber, CreatedBy.Enterprise_ID__c from opportunity where id in : setOpportunity];


        if(lstOppLocal != null && lstOppLocal.size()>0){            
            for(Opportunity eachOpportunity : lstOppLocal){                 
                    eachOpportunity.CreatedByEmployeeNumber__c  =   eachOpportunity.CreatedBy.EmployeeNumber;
                    eachOpportunity.CreatedByEnterpriseID__c = eachOpportunity.CreatedBy.Enterprise_ID__c ;
                    lstToUPdate.add(eachOpportunity);

            }           
            if(lstToUPdate!= null && lstToUPdate.size() >0){                
                update lstToUPdate ;
            }
        }  

    }    


}
  • 2
    I am confused, you say it is running when inserted yet you have the NOT ( ISNEW()) so do you want it to run when inserted or do you not want it to run when inserted? – Eric Nov 24 '15 at 19:08
  • The first time it run asking for the isNew() its related to the opp, so, it follow the code order with no problems, but when running the @future proppery ( populatevalueInUserRelatedfield() ) it't not a new opportunity anymore because it's allready created, do i explain myself? Thaks in advance – CrispuSF Nov 24 '15 at 19:44
  • The VR will be TRUE if Oppo is being updated AND the other 3 conditions are true; the VR will be FALSE if Oppo is being inserted. It is still unclear what you are trying to do. – cropredy Nov 24 '15 at 20:11
1

you can bypass the validation rule using a custom field which is set during the second @future method call. Using a workflow rule you then reset the variable after the transaction so that further updates will cause the validation to fire as expected.

Example:

http://bobbuzzard.blogspot.com/2011/11/bypass-validation-rules-from-apex.html

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.