+ Start a Discussion

Validation rules conflict with Process Builder

I created trigger to populate default value.I also create validation rules to check that the field should not be empty only on update record.It should not fire if user create new record.The issue is, test class fail due to process builder throw validation rule exception.
trigger Opportunity_Trigger on Opportunity (before insert) {
   if (Trigger.isBefore)
            OpportunityTriggerHandler.populateDefaultData (Trigger.new);

//in trigger framework

 public static  void populateDefaultData (List<Opportunity> newOppList)
            Set<Id> setUser = new Set<Id>();
           for (Opportunity opp : newOppList) 
          {  setUser.add(opp.OwnerId);


          Map<Id, User> mapUsers = new Map<Id, User>([SELECT Id, UserDivision__c FROM User WHERE Id IN :setUser ]);

          for (Opportunity opp : newOppList) 

            User owner =mapUsers.get(opp.OwnerId);

            if(owner.UserDivision=='APAC' && String.IsBlank(opp.OwnerDivision__c))
                    opp.OwnerDivision__c = owner.UserDivision;

Validation Rules
ISBLANK( TEXT(OwnerDivision__c) )

Process builder criteria
Process builder action
update StageStatus with Opportunity.StageName

Test class
private class Opportunity_TestClass {

 //contains insertion for Account 
    @testSetup static void setupData()
        //create account  
        Account account1=new Account(Name='Account 1',Country__c='Denmark');

        insert listAccount;


 static testMethod void  testPopulateData()
        //insert user
        Profile p = [SELECT Id,Name FROM Profile WHERE Name='Sales APAC']; 
        UserRole userRole=[SELECT Id FROM UserRole where Name ='Manager'];
        List<User> lstUser = new List<User>();

        String orgId = UserInfo.getOrganizationId();
        String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
        Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));

        String uniqueName =  orgId + dateString + randomInt;

        User userOne = new User(Alias = 'userOne', Email='userOne@abc.com', 
                                   EmailEncodingKey='UTF-8', LastName='userInt', LanguageLocaleKey='en_US', 
                                   LocaleSidKey='en_US', ProfileId = p.Id, 
                                   TimeZoneSidKey='America/Los_Angeles', UserName=uniqueName + '@test' + orgId + '.org',Employee_ID__c ='123',
                                   UserRoleId=userRole.id,UserDivision__c ='APAC', 

        insert userOne ;

        List<User> lstInsertedUser = [select Id from User where LastName in ('userOne')];



        List<Account> lstAccount =[Select Name,Id from Account where Name ='Account 1'];

            System.debug('Current User: ' + UserInfo.getUserName());
            Opportunity testOpp = new Opportunity(Name='Oppo 1', StageName='Create',
                                                  CloseDate=Date.TODAY()+20, amount=10000,AccountId=lstAccount[0].Id);

            insert testOpp;    

            system.assertEquals(1,[Select count() from Opportunity where Name='Oppo 1']);




I am stuck because logically it should not fire the exception.The data is inserted through trigger.
NagendraNagendra (Salesforce Developers) 
Hi Unidha,

Please find the information below.

When updating a new record with Process Builder, the record is no longer considered new.

I have a validation rule on opportunity that only allows certain conditions on record creation, i.e. IsNew()=TRUE.

There are some workflow field updates that update an Opp whenever created or edited.

So I create a new Opportunity, the field update fires and because IsNew() is TRUE it all works fine.

I then decided to move the Workflow into a Process Builder.

Now I get an 'Error occurred during flow' when creating a new Opp - when the Process Builder tries to update the new record, it seems that the record is no longer considered new and gets caught by the validation rule.
My options to get around this are too add an exception for the field being updated into the validation rule, or to add something like 'CreatedDate > Now()-0.0007', i.e. error if an update is being attempted more than a minute or so after creation.

Kindly mark this post as solved if the information help's so that it gets removed from the unanswered queue which results in helping others who are really in need of it.

Best Regards,