Must be really simple but I am struggling with how to loop through this map. Gone through some examples but was unable to figure it out.

Question is, how to loop through user IDs from TargetActorMap map and check if they are in RMUserIDs list? Here is my code.

Map<Id, Id> TargetActorMap = new Map<Id, Id>();
    Set<Id> userIds = new Map<Id, User>([select id from user where Approval_Level__c in ('RM', 'GM','DM')]).keySet();

        for(ProcessInstance pi : [SELECT Id,TargetObjectId, (SELECT ID, StepStatus,TargetObjectId,ActorId 
                                    FROM StepsAndWorkitems where StepStatus = 'Pending' 
                                    and ActorId in :userIds order by CreatedDate desc limit 1) 
                                    FROM ProcessInstance where targetObjectId in :trigger.newMap.keySet()]) {
            for(ProcessInstancehistory wi :pi.StepsAndWorkitems) {
                    TargetActorMap.put(pi.TargetObjectId, wi.ActorId);
    Set<Id> RMUserIds = new Map<Id, User>([select id from user where Approval_Role__c = 'RM']).keySet();
        for(ST_Orders__c s2 : [Select id,Approval_Status__c from ST_Orders__c where id in :TargetActorMap.keySet()]) {
            if(s2.Approval_Status__c == 'Level 1 Approval'/* && User Level is not RM */ ) {
           // S2.addError(System.Label.Approval_Error1);
            else if(s2.Approval_Status__c != 'Level 1  Approval' /* && /*User Level is RM */) {

1 Answer 1


Luckily, you don't need to do a loop here because you're using containers that can perform access and membership checking in constant time.

Inside your for loop,

for (ST_Orders__c s2 : [Select id,Approval_Status__c from ST_Orders__c where id in :TargetActorMap.keySet()]) {

you know for sure that s2.Id is a key in TargetActorMap. You can get the Id that is its associated value with TargetActorMap.get(s2.Id). Then, you can find out if that same Id is present in RMUserIds just by doing


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .