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
Matt Cooper 7Matt Cooper 7 

Query not assigning to variable

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?
Martijn SchwarzerMartijn Schwarzer
Hi Matt,

In you for-loop you assign records of ProcessInstance to PIlist. But I don't see code that instantiates the PIList variable.

Do you also have a statement like
List<ProcessInstance> PIList = new List<ProcessInstance>();

in your code?

If you want to debug what's going wrong, I would suggest to add the following lines of code to your catch section:
 
system.debug(LoggingLevel.Error, 'Errormessage: ' + e.getMessage());
system.debug(LoggingLevel.Error, 'Stack Trace: ' + e.getStackTraceString());

Hope this helps!

Best regards,
Martijn Schwärzer