I have a requirement to where I need create a LeadShare with my PowerPartners. So if a Lead is created by a PowerPartner then it will placed into LeadShare. My Orignal thinking was to take the User object and find all of the User Ids that are type "PowerPartners", then once I find all of them I will go into my GroupMembers and I will find said Users. Then from there assignments will be made stuff in LeadShare will be set. Is my logic correct? Trigger will be below..

Also I can't do this type of loop on User Object. So I need logic help

trigger PartnerLeadShare on Lead (after insert,after update) {

 List<LeadShare> leadShareList = new list<LeadShare>();   
Set<id>pUserMap=new set<id>();  
  for(User u: [select id,UserType,isActive from user where UserType = 'PowerPartner'  and IsActive = True]){       
            System.debug('pUserMap ' +pUserMap);
Map<Id, GroupMember> groupMap = new Map <Id, GroupMember>();
for(GroupMember grp : [select GroupId, UserOrGroupId from GroupMember where   UserORGroupID IN:pUserMap]){
groupMap.put(grp.UserOrGroupId, grp);
System.debug('groupList    '+groupMap ); 
 if(pUserMap != null){
//System.debug('Inside PartUserMap != null');
for(Lead ld: trigger.new){  
    System.debug('Created BY!!  ' +ld.CreatedById);  
            System.debug('Inside GroupMap Contains');          
            LeadShare ldShare = new LeadShare (LeadId = ld.id, LeadAccessLevel =   'Edit');   
            leadShareList.add(ldShare ); 
  • Btw, you probably need an insert leadShareList; at the end.
    – dphil
    Apr 28, 2014 at 16:25
  • I am I just cut that code off because I'm still trying to get a comparison. I'm starting a new thread in hope I can help for my test class
    – EricSSH
    Apr 28, 2014 at 16:31
  • Did you manage to get where you wanted with this in the end? May 9, 2014 at 10:39

1 Answer 1


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)

Map<Id, User> userMap = new Map<Id, User>
        Id IN :userIds 
        AND UserType = 'PowerPartner' 
        AND IsActive = true

Set<Id> userIds = userMap.keySet();
  • Hey Alex, that gives me an error "Illegal assignment from List<User> to List<id>"
    – EricSSH
    Apr 28, 2014 at 16:10
  • @EricSSH Sorry about that, I've fixed that issue now. Apr 28, 2014 at 16:17

You must log in to answer this question.

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