0

I have a requirement to prevent certain user profiles from approving accounts. Wrote below trigger which is partially working. However getting below error when a user in Standard User profile tries to approve the record.

AccountTrigger: execution of AfterUpdate caused by: System.UnexpectedException: Illegal filter on ProcessInstance
Class.AccountTriggerHandler.afterUpdateTriggerHandler: line 17, column 1
Trigger.AccountTrigger: line 4, column 1

Here is my handler class

public with sharing class AccountTriggerHandler {

    public static void afterUpdateTriggerHandler(Map<Id,account> accMapNew, Map<Id,account> accMapOld) {

         Id myProfileId = [Select id, name from profile where name = 'Standard User'].Id;

         set<Id> modfiedAccounts = new set<Id>();
         set<Id> ApprovalNotAllowed = new set<Id>();


         for(account a: accMapNew.values()){
             if(accMapNew.get(a.Id).Active__c != accMapOld.get(a.Id).Active__c && accMapNew.get(a.Id).Active__c=='Yes') {
             modfiedAccounts.add(a.Id);
             }
        }

         list<ProcessInstance> ProcessInstances = [SELECT TargetObjectId,LastActor.ProfileId,LastActor.Name,
                                                       (SELECT Id, ActorId,Actor.Name,OriginalActor.Name,OriginalActor.profileId,Comments,  
                                                        StepStatus,ProcessInstanceId FROM StepsAndWorkitems 
                                                        order by createdDate desc limit 1)
                                                        FROM ProcessInstance where 
                                                        TargetObjectId IN :modfiedAccounts 
                                                        AND LastActor.ProfileId IN (:myProfileId) //problem
                                                        order by createdDate desc limit 1];


        for(ProcessInstance pi: ProcessInstances) {
                 {
                 ApprovalNotAllowed.add(pi.TargetObjectId);

                 } 

        }
        set<account> ApprovalNotAllowedFinal = new set<account> ([Select Id from account where id in :ApprovalNotAllowed]);

        for(account acc :ApprovalNotAllowedFinal) {
            account ac1 = accMapNew.get(acc.Id); 
            ac1.addError('Standard user cannot approve accounts);
         }

    }
}

Why do I get this error? How can I fix it?

2
  • 1
    Have you tried querying the Id of all the User records which have that Profile and filtering on that Set<Id> instead? – Adrian Larson Feb 1 '19 at 18:18
  • Typing up answer one sec. – Adrian Larson Feb 1 '19 at 20:23
1

I recommend you try filtering on LastActorId rather than LastActor.ProfileId. You can get the filter value by adding a subquery.

Profile whietelistedProfile = [SELECT (SELECT Id FROM Users) FROM Profile WHERE ...];
Set<Id> userIds = new Map<Id, User>(whitelistedProfile.Users).keySet();

... WHERE LastActorId IN :userIds
1
  • That’s exactly what I did after looking at your previous comment. Thanks again. – sperikal Feb 1 '19 at 22:09

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.