0

I am having an issue where I am running a query in a trigger to search for a ProcessInstance record. In my debug, the query returns a record and values. However, when I try to assign that query result to a ProcessInstance variable or add it to a ProcessInstance list, it fails.

try{
    for(ProcessInstance p: [SELECT CompletedDate,CreatedById,CreatedDate,ElapsedTimeInDays,ElapsedTimeInHours,ElapsedTimeInMinutes,Id,IsDeleted,LastActorId,LastModifiedById,LastModifiedDate,ProcessDefinitionId,Status,SubmittedById,SystemModstamp,TargetObjectId FROM ProcessInstance WHERE TargetObjectId = :AR.id  AND Status = 'Pending' LIMIT 1]){
         PIlist.add(p);
         system.debug('PIlist.size() = ' + PIlist.size());
         }
         PIlist = [SELECT CompletedDate,CreatedById,CreatedDate,ElapsedTimeInDays,ElapsedTimeInHours,ElapsedTimeInMinutes,Id,IsDeleted,LastActorId,LastModifiedById,LastModifiedDate,ProcessDefinitionId,Status,SubmittedById,SystemModstamp,TargetObjectId FROM ProcessInstance WHERE TargetObjectId = :AR.id  AND Status = 'Pending' LIMIT 1];
         PI=[SELECT CompletedDate,CreatedById,CreatedDate,ElapsedTimeInDays,ElapsedTimeInHours,ElapsedTimeInMinutes,Id,IsDeleted,LastActorId,LastModifiedById,LastModifiedDate,ProcessDefinitionId,Status,SubmittedById,SystemModstamp,TargetObjectId FROM ProcessInstance WHERE TargetObjectId = :AR.id  AND Status = 'Pending' LIMIT 1];
                }
      catch(Exception e){
           system.debug('No ProcesssInstance.  AR.Id=' + AR.id + '    Heres the query:' + [SELECT CompletedDate,CreatedById,CreatedDate,ElapsedTimeInDays,ElapsedTimeInHours,ElapsedTimeInMinutes,Id,IsDeleted,LastActorId,LastModifiedById,LastModifiedDate,ProcessDefinitionId,Status,SubmittedById,SystemModstamp,TargetObjectId FROM ProcessInstance WHERE TargetObjectId = :AR.id]);
                }   

The debug returns:

10:56:21:180 USER_DEBUG [21]|DEBUG|No ProcesssInstance. AR.Id=a1QR0000002zuy8MAA Heres the query:(ProcessInstance:{CreatedById=005o0000000Ip8lAAC, CreatedDate=2016-05-11 17:56:21, ElapsedTimeInDays=0.000, ElapsedTimeInHours=0.00, ElapsedTimeInMinutes=0, Id=04gR0000000JEOnIAO, IsDeleted=false, LastModifiedById=005o0000000Ip8lAAC, LastModifiedDate=2016-05-11 17:56:21, ProcessDefinitionId=04aR00000008TsmIAE, Status=Started, SubmittedById=005o0000000Ip8lAAC, SystemModstamp=2016-05-11 17:56:21, TargetObjectId=a1QR0000002zuy8MAA})"

Any idea why this would be happening?

  • 1
    We're missing a ton of code and context here, Matt – Sebastian Kessel May 11 '16 at 18:10
  • Are you initializing your PIList variable before trying to add things to the list? If you aren't, that could be what's throwing the exception that you're catching. – Derek F May 11 '16 at 18:11
1

According to your debug the ProcessInstance status is "started"

ProcessInstance:{CreatedById=005o0000000Ip8lAAC, CreatedDate=2016-05-11 17:56:21, ElapsedTimeInDays=0.000, ElapsedTimeInHours=0.00, ElapsedTimeInMinutes=0, Id=04gR0000000JEOnIAO, IsDeleted=false, LastModifiedById=005o0000000Ip8lAAC, LastModifiedDate=2016-05-11 17:56:21, ProcessDefinitionId=04aR00000008TsmIAE, Status=Started, SubmittedById=005o0000000Ip8lAAC, SystemModstamp=2016-05-11 17:56:21, TargetObjectId=a1QR0000002zuy8MAA}

However in all of your queries except the last one you have status filtered to 'Pending'. Therefore you will never return the ProcessInstance. You need to remove the Status = 'Pending' filter from your WHERE clause

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.