Skip to main content
added 109 characters in body
Source Link
Alex Tennant
  • 13.7k
  • 5
  • 58
  • 85

In a trigger on Lead (which is what you're using), Trigger.new only contains the list of Lead objects that has been inserted/updated/deleted/undeleted (depending on context), which is why you cannot iterate over it as a collection of User objects.

You can however build your own list of user IDs and then use that to get the User objects you require:

Set<ID> userIds = new Set<Id>();

for(Lead aLead : Trigger.new)
{
    userIds.add(aLead.CreatedById);
}

List<Id>Map<Id, User> userMap = new Map<Id, User>
([
    SELECT 
        Id
    FROM 
        User 
    WHERE 
        Id IN :userIds 
        AND UserType = 'PowerPartner' 
        AND IsActive = true
];]);

Set<Id> userIds = userMap.keySet();

In a trigger on Lead (which is what you're using), Trigger.new only contains the list of Lead objects that has been inserted/updated/deleted/undeleted (depending on context), which is why you cannot iterate over it as a collection of User objects.

You can however build your own list of user IDs and then use that to get the User objects you require:

Set<ID> userIds = new Set<Id>();

for(Lead aLead : Trigger.new)
{
    userIds.add(aLead.CreatedById);
}

List<Id> userMap = 
[
    SELECT 
        Id
    FROM 
        User 
    WHERE 
        Id IN :userIds 
        AND UserType = 'PowerPartner' 
        AND IsActive = true
];

In a trigger on Lead (which is what you're using), Trigger.new only contains the list of Lead objects that has been inserted/updated/deleted/undeleted (depending on context), which is why you cannot iterate over it as a collection of User objects.

You can however build your own list of user IDs and then use that to get the User objects you require:

Set<ID> userIds = new Set<Id>();

for(Lead aLead : Trigger.new)
{
    userIds.add(aLead.CreatedById);
}

Map<Id, User> userMap = new Map<Id, User>
([
    SELECT 
        Id
    FROM 
        User 
    WHERE 
        Id IN :userIds 
        AND UserType = 'PowerPartner' 
        AND IsActive = true
]);

Set<Id> userIds = userMap.keySet();
Source Link
Alex Tennant
  • 13.7k
  • 5
  • 58
  • 85

In a trigger on Lead (which is what you're using), Trigger.new only contains the list of Lead objects that has been inserted/updated/deleted/undeleted (depending on context), which is why you cannot iterate over it as a collection of User objects.

You can however build your own list of user IDs and then use that to get the User objects you require:

Set<ID> userIds = new Set<Id>();

for(Lead aLead : Trigger.new)
{
    userIds.add(aLead.CreatedById);
}

List<Id> userMap = 
[
    SELECT 
        Id
    FROM 
        User 
    WHERE 
        Id IN :userIds 
        AND UserType = 'PowerPartner' 
        AND IsActive = true
];