+ Start a Discussion
gtindugtindu 

Update ProcessInstanceWorkItem in an @future method

I have an @future method that runs to reassign open approval requests based on Out of office rules.  If I don't have the method decorated @future - I have no issues everything works fine.  But if I decorate the method @future - i get a NullReference exception 

 

 

System.DmlException: Update failed. First exception on row 0 with id 04iXXXXXXXXXXXXXXX; first error: UNKNOWN_EXCEPTION, java.lang.NullPointerException: Argument Error: Parameter value is null: []

 

The only thing that changed is the @future decorator.  I would like to use this in Code Scheduler - but I also get the same error.  Anyone worked around this?

 

 

 

forecast_is_cloudyforecast_is_cloudy

Can you please post the @future code that updates the ProcessInstanceWorkItem?

SatgurSatgur

Well, can you specify what is the signature of this method which you are decorating as @future?

 

Note - if this method was operating on some instance variables of the class it is part of, then instance variables will not be available to the method when you decorate it with @future annotation.

 

Reason is @future executes asynchronously at later point and hence @future method would not be having references to class level instance variables.

 

This could be the reason of NullPointerException from @future decorated method.

 

Solution - if you needed to work on sObject record within @future method, make sure when you call this method, you pass the ID of the sObject record as an argument of the method. Now within @future you can fire SOQL on sObject id to retrieve the relevant details and process the record further.

 

Hope this helps. Please mark it a solution if your issue has been addressed.

 

Thanks

 

gtindugtindu

 

public class DelegationOfAuthorityProcessor {
        @future
	public void process() {
		System.debug(LoggingLevel.INFO, UserInfo.getUserId());
		List<ProcessInstanceWorkItem> updateProcs = new List<ProcessInstanceWorkItem>();
		Map<Id, Temporary_DOA__c> activeDOAs = new Map<Id, Temporary_DOA__c>();
		Map<Id, ProcessInstanceStep> procStepMap = new Map<Id, ProcessInstanceStep>();
		Set<Id> delegatedUser = new Set<Id>();

		// Get approved re-assignments  
		for(Temporary_DOA__c doa : [SELECT id, name, Approval_Process__c, Comments__c, Delegate_From__c, Delegate_To__c, Start_Date__c, Stop_Date__c FROM Temporary_DOA__c where start_date__c <= :date.today() and stop_date__c >= :date.today() and Status__c = 'Approved']) {
			// Map the delegated from user to the approved re-assignment
			if (!activeDOAs.containsKey(doa.Delegate_From__c))
				activeDOAs.put(doa.Delegate_From__c, doa);
			delegatedUser.add(doa.Delegate_From__c);
		}
		
		// Get ProccessInstanceWorkItems outstanding for users with approved reassignments
		List<ProcessInstanceWorkItem> processes = new List<ProcessInstanceWorkItem>([Select p.ProcessInstanceId, p.OriginalActorId, p.Id, p.ActorId, processinstance.targetobjectid From ProcessInstanceWorkitem p where actorid in :delegatedUser]);

		for(ProcessInstanceWorkItem proc : processes) {
			// If an active reassignment exists for the user...
			if (activeDOAs.containsKey(proc.ActorId)) {
				// And it has someone to reassign to...
				if (activeDOAs.get(proc.ActorId).Delegate_To__c != null) {
					// update the ActorID
					proc.ActorId = activeDOAs.get(proc.ActorId).Delegate_To__c;
					updateProcs.add(proc);
				}
			}
		}
		System.debug(updateProcs);
		if (!updateProcs.isEmpty())
			update updateProcs;				
	}
}

 

 

 

 

gtindugtindu

If I try running the above - i will get a failure with null reference on the DML statement for update.  The ActorID is not null... and if I remove the @future - it works just fine.

I get the same symptoms as @future when I run as part of a schedulable apex job.

Jigen SanJigen San
No project is too big or too little for our highly trained professionals. We are devoted to resolving your garage door issues as quickly as possible and at a reasonable cost.
https://garagedoorrepairyorktown.net/garage-door-maintenance-101-tips-for-prolonging-lifespan-and-preventing-issues/