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
ChristianralphChristianralph 

Testing Workflow Rules from Apex

We are trying to test Workflow rules from Apex tests.  WE ARE NOT INTERESTED IN JUST MEETING CODE COVERAGE but in actually testing the functionality of the rule. 

 

Our Rule: (defined manually from SalesForce builder)

When an Opportunity is created, we create a new Task linking it to that opportunity

 

Apex test (pseudo code)

create valid opportunity

insert opportunity

get all Tasks

assert that new Task was created. (FAILS)

 

The rule however works when you manually create the opportunity through SalesForce UI 

 

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

I would guess this is a timing issue - if the tasks are created by WF, then they probably get created after your SOQL query is executed...

 

Try using test.starttest() and test.stoptest() around the data creation, and then query for the tasks after the stop test

All Answers

BritishBoyinDCBritishBoyinDC

I would guess this is a timing issue - if the tasks are created by WF, then they probably get created after your SOQL query is executed...

 

Try using test.starttest() and test.stoptest() around the data creation, and then query for the tasks after the stop test

This was selected as the best answer
ChristianralphChristianralph

Great thanks!

 

This appears to work :smileyhappy: