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
Anjali RonnieAnjali Ronnie 

Error: Loop must iterate over collection: Id

trigger TaskAssignmentTrigger on Claims__c (before insert, before update) {
    List<Id> assignedUserIds = new List<Id>();

    for (Claims__c claim : Trigger.new) {
        if (claim.Assigns_to_be__c != null) {
            for (Assignment_Group_c__c membership : claim.Assigns_to_be__c) {
                assignedUserIds.add(membership.Users__c);
            }
        }
    }

    if (!assignedUserIds.isEmpty()) {
        TaskAssignmentHandler.addUsersToTask(assignedUserIds, Trigger.new[0].Id);
    }
}

 

Im getting an error "Loop must iterate over collection: Id". Dont know what is causing this error.Any help will be much apprecitated.

Naresh Kaneriya 9Naresh Kaneriya 9

Hi Anjali Ronnie,

"Loop must iterate over collection: Id" is indicating that there's an issue with the line of code where you're trying to iterate over claim.Assigns_to_be__c. The error is saying that you can't directly use a single Id value in a for-each loop like that. 

You can retry you code as below and let me know if you are still having the same issue.

// Code Start
trigger TaskAssignmentTrigger on Claims__c (before insert, before update) {
    List<Id> assignedUserIds = new List<Id>();
    Set<Id> assignstobeId = new Set<Id> (); 
    for (Claims__c claim : Trigger.new) {
        if (claim.Assigns_to_be__c != null) {
            AssignstobeId.add(claim.Assigns_to_be__c);
            
        }
    }
      
     for (Assignment_Group_c__c membership : [SELECT Users__c FROM Assignment_Group_c__c WHERE   
                                             Id IN : assignstobeId]) {
                assignedUserIds.add(membership.Users__c);
            }
    
    if (!assignedUserIds.isEmpty()) {
        TaskAssignmentHandler.addUsersToTask(assignedUserIds, Trigger.new[0].Id);
    }
} // Code End

Thanks, Naresh