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 );
}
}
}
Attribution to: EricSSH
Possible Suggestion/Solution #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)
{
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();
Attribution to: Alex Tennant
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33638