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) {
if(Trigger.isAfter){
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]){
{
pUserMap.add(u.id);
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);
if(groupMap.containsKey(ld.CreatedById)){
System.debug('Inside GroupMap Contains');
LeadShare ldShare = new LeadShare (LeadId = ld.id, LeadAccessLevel = 'Edit');
leadShareList.add(ldShare );
}
}
}