• Agar
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 2
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

I'm testing a trigger on our Opportunities which kick off an Approval Process when the Opportunity's Process_Stage__c field changes. If I create an Opportunity and move it between stages through the UI I can see the ProcessInstance, ProcessInstanceStep and ProcessInstanceWorkitem are all created. I'm using Force.com Explorer Beta and have verified this. So far so good.

 

In my test class I'm creating an Opportunity and inserting it, then updating the Process_Stage__c. After the update I'm seeing the ProcessInstance and ProcessInstanceStep are being created but there are no ProcessInstanceWorkitems at all in the database. I want to test rejecting and approving the Approval but without the ProcessInstanceWorkitem I can't do this (as far as I understand the Approval Process).

 

Below is my code, there are a few lines where I've tried various ways to retrieve the ProcessInstanceWorkitems, finally resulting in [SELECT ActorId, CreatedById, Id FROM ProcessInstanceWorkitem]; because up until this point I thought my SOQL was wrong, now I don't think the ProcessInstanceWorkitems are being created for some reason. Is there something fundamental I'm missing? Should this be possible to do from a unit test?

 

// Yadda yadda yadda Create Opportunity (o) here
		insert(o); 
		
        // Make sure this isn't already queued up for approval before saving as a Stage 2
		List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :o.id];
		System.assertEquals(processInstances.size(),0,'Approval Process already triggered when saving at Stage 1');

		o.Process_Stage__c = TestConstants.STAGE_2; 
		update(o);

        // Assert an approval process has been initiated
		processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :o.id];
		System.assertEquals(processInstances.size(),1, 'Only 1 approval processes should be created when saving at Stage 2, found '+processInstances.size());
		
		List<ProcessInstanceStep> processInstanceSteps = [SELECT ActorId, OriginalActorId, StepStatus 
			FROM ProcessInstanceStep 
			WHERE ProcessInstanceId = :processInstances[0].id];
        System.assert(processInstanceSteps.size() > 0,'No approval processes steps found when saving at Stage 2');

        List<ProcessInstanceWorkitem> processInstanceWorkitems = [SELECT ActorId, CreatedById, Id FROM ProcessInstanceWorkitem]; // WHERE ProcessInstanceId = :processInstances[0].id];
        //List<ProcessInstanceWorkitem> processInstanceWorkitems = [Select p.Id from ProcessInstanceWorkitem p where p.ProcessInstance.TargetObjectId = :o.id];
        //List<ProcessInstance> processInstancesWithWorkitems = [SELECT Id, (SELECT Id, ActorId, ProcessInstanceId FROM Workitems) FROM ProcessInstance WHERE TargetObjectId = :o.id];
		System.assert(processInstanceWorkitems.size() > 0,'No approval processes Workitems found when saving at Stage 2');
 
  • October 04, 2013
  • Like
  • 2

I'm testing a trigger on our Opportunities which kick off an Approval Process when the Opportunity's Process_Stage__c field changes. If I create an Opportunity and move it between stages through the UI I can see the ProcessInstance, ProcessInstanceStep and ProcessInstanceWorkitem are all created. I'm using Force.com Explorer Beta and have verified this. So far so good.

 

In my test class I'm creating an Opportunity and inserting it, then updating the Process_Stage__c. After the update I'm seeing the ProcessInstance and ProcessInstanceStep are being created but there are no ProcessInstanceWorkitems at all in the database. I want to test rejecting and approving the Approval but without the ProcessInstanceWorkitem I can't do this (as far as I understand the Approval Process).

 

Below is my code, there are a few lines where I've tried various ways to retrieve the ProcessInstanceWorkitems, finally resulting in [SELECT ActorId, CreatedById, Id FROM ProcessInstanceWorkitem]; because up until this point I thought my SOQL was wrong, now I don't think the ProcessInstanceWorkitems are being created for some reason. Is there something fundamental I'm missing? Should this be possible to do from a unit test?

 

// Yadda yadda yadda Create Opportunity (o) here
		insert(o); 
		
        // Make sure this isn't already queued up for approval before saving as a Stage 2
		List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :o.id];
		System.assertEquals(processInstances.size(),0,'Approval Process already triggered when saving at Stage 1');

		o.Process_Stage__c = TestConstants.STAGE_2; 
		update(o);

        // Assert an approval process has been initiated
		processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :o.id];
		System.assertEquals(processInstances.size(),1, 'Only 1 approval processes should be created when saving at Stage 2, found '+processInstances.size());
		
		List<ProcessInstanceStep> processInstanceSteps = [SELECT ActorId, OriginalActorId, StepStatus 
			FROM ProcessInstanceStep 
			WHERE ProcessInstanceId = :processInstances[0].id];
        System.assert(processInstanceSteps.size() > 0,'No approval processes steps found when saving at Stage 2');

        List<ProcessInstanceWorkitem> processInstanceWorkitems = [SELECT ActorId, CreatedById, Id FROM ProcessInstanceWorkitem]; // WHERE ProcessInstanceId = :processInstances[0].id];
        //List<ProcessInstanceWorkitem> processInstanceWorkitems = [Select p.Id from ProcessInstanceWorkitem p where p.ProcessInstance.TargetObjectId = :o.id];
        //List<ProcessInstance> processInstancesWithWorkitems = [SELECT Id, (SELECT Id, ActorId, ProcessInstanceId FROM Workitems) FROM ProcessInstance WHERE TargetObjectId = :o.id];
		System.assert(processInstanceWorkitems.size() > 0,'No approval processes Workitems found when saving at Stage 2');
 
  • October 04, 2013
  • Like
  • 2

I'm testing a trigger on our Opportunities which kick off an Approval Process when the Opportunity's Process_Stage__c field changes. If I create an Opportunity and move it between stages through the UI I can see the ProcessInstance, ProcessInstanceStep and ProcessInstanceWorkitem are all created. I'm using Force.com Explorer Beta and have verified this. So far so good.

 

In my test class I'm creating an Opportunity and inserting it, then updating the Process_Stage__c. After the update I'm seeing the ProcessInstance and ProcessInstanceStep are being created but there are no ProcessInstanceWorkitems at all in the database. I want to test rejecting and approving the Approval but without the ProcessInstanceWorkitem I can't do this (as far as I understand the Approval Process).

 

Below is my code, there are a few lines where I've tried various ways to retrieve the ProcessInstanceWorkitems, finally resulting in [SELECT ActorId, CreatedById, Id FROM ProcessInstanceWorkitem]; because up until this point I thought my SOQL was wrong, now I don't think the ProcessInstanceWorkitems are being created for some reason. Is there something fundamental I'm missing? Should this be possible to do from a unit test?

 

// Yadda yadda yadda Create Opportunity (o) here
		insert(o); 
		
        // Make sure this isn't already queued up for approval before saving as a Stage 2
		List<ProcessInstance> processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :o.id];
		System.assertEquals(processInstances.size(),0,'Approval Process already triggered when saving at Stage 1');

		o.Process_Stage__c = TestConstants.STAGE_2; 
		update(o);

        // Assert an approval process has been initiated
		processInstances = [select Id, Status from ProcessInstance where TargetObjectId = :o.id];
		System.assertEquals(processInstances.size(),1, 'Only 1 approval processes should be created when saving at Stage 2, found '+processInstances.size());
		
		List<ProcessInstanceStep> processInstanceSteps = [SELECT ActorId, OriginalActorId, StepStatus 
			FROM ProcessInstanceStep 
			WHERE ProcessInstanceId = :processInstances[0].id];
        System.assert(processInstanceSteps.size() > 0,'No approval processes steps found when saving at Stage 2');

        List<ProcessInstanceWorkitem> processInstanceWorkitems = [SELECT ActorId, CreatedById, Id FROM ProcessInstanceWorkitem]; // WHERE ProcessInstanceId = :processInstances[0].id];
        //List<ProcessInstanceWorkitem> processInstanceWorkitems = [Select p.Id from ProcessInstanceWorkitem p where p.ProcessInstance.TargetObjectId = :o.id];
        //List<ProcessInstance> processInstancesWithWorkitems = [SELECT Id, (SELECT Id, ActorId, ProcessInstanceId FROM Workitems) FROM ProcessInstance WHERE TargetObjectId = :o.id];
		System.assert(processInstanceWorkitems.size() > 0,'No approval processes Workitems found when saving at Stage 2');
 
  • October 04, 2013
  • Like
  • 2