I am working on a requirement to achieve the below:
- Whenever a lead is created by a user:
- Check if that user is Rep Type (field present at user level), then add that user to Lead Share object with Edit Access Note: The person who creates this lead, doesn't becomes the lead owner because either a Queue or some other user becomes the Owner
Example: User-1 should become part of Lead Share record for this Lead
- Now when the Lead Owner is Updated:
- Check if the new user/owner is Rep Type. If yes, then add him to Lead Share object with Edit Access. Here we also need to ensure, that the previous users added to Lead Share object should retain the Edit Access
Example: So basically both User-1 & User-2 should be present in Lead Share object now.
The 1st Part of requirement is fulfilled through writing a method which would be executed After Insert "leadShareToRepNew"(working absolutely fine!!). But the issue is with the 2nd Part which is executed After Update"leadShareToRepUpdate".
Error: After Creation of Lead by User-1, when User-1 changes the owner to User-2, the following is shown "System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []"
PS: Lead sharing model is set to Private. Both the users have same profile and the "Transfer Lead" permission on that is set to true
Can someone suggest something here ?
public static void leadShareToRepNew(final List<Lead> listOfNewLeads){
final Id directRecordTypeId = Schema.SObjectType.Lead.RecordTypeInfosByName.get('Direct').RecordTypeId;
final Id forwardRecordTypeId = Schema.SObjectType.Lead.RecordTypeInfosByName.get('Forward').RecordTypeId;
final Id rotaryRecordTypeId = Schema.SObjectType.Lead.RecordTypeInfosByName.get('Rotary').RecordTypeId;
final Id convertRecordTypeId = Schema.SObjectType.Lead.RecordTypeInfosByName.get('VSG Convert Lead').RecordTypeId;
set<Id> setRecordTypeId = new set<Id>{forwardRecordTypeId,directRecordTypeId,rotaryRecordTypeId,convertRecordTypeId};
set<Id> setOfLeadRepUserIds = new set<Id>();
set<Id> setOfLeadUserIds = new set<Id>();
List<LeadShare> leadShareListNew = new List<LeadShare>();
System.debug('@@@@Inside leadShareToRep method');
for(Lead instOfLead : listOfNewLeads){
setOfLeadUserIds.add(instOfLead.CreatedById);
}
System.debug('@@@@setOfLeadUserIds'+setOfLeadUserIds);
for( User instOfUser : [Select Id, Dvp2_IsPartnerUser__c, Dvp2_Is_Rep__c from User where Id IN :setOfLeadUserIds and Dvp2_IsPartnerUser__c = true and Dvp2_Is_Rep__c = true ]){
setOfLeadRepUserIds.add(instOfUser.Id);
}
System.debug('@@@@setOfLeadRepUserIds'+setOfLeadRepUserIds);
for(Lead newLead : listOfNewLeads){
System.debug('@@Condition-1'+setRecordTypeId.contains(newLead.RecordTypeId));
System.debug('@@Condition-2'+setOfLeadRepUserIds.contains(newLead.CreatedById));
if( setRecordTypeId.contains(newLead.RecordTypeId) && setOfLeadRepUserIds.contains(newLead.CreatedById) ){
System.debug('@@@@Condition met'+newLead.Id+'@@@@'+newLead.CreatedById+'@@@@'+newLead);
LeadShare ls = new LeadShare();
ls.leadId = newLead.Id;
ls.leadAccessLevel = 'Edit';
ls.userOrGroupId = newLead.CreatedById;
System.debug('@@Printing LeadShare record'+ls);
leadShareListNew.add(ls);
}
}
System.debug('@@leadShareList.isEmpty()'+leadShareListNew.isEmpty());
if(!leadShareListNew.isEmpty()){
System.debug('@@@@Inside leadShareListNew'+leadShareListNew);
insert leadShareListNew;
}
}
public static void leadShareToRepUpdate(final List<Lead> listOfNewLeads){
final Id directRecordTypeId = Schema.SObjectType.Lead.RecordTypeInfosByName.get('Direct').RecordTypeId;
final Id forwardRecordTypeId = Schema.SObjectType.Lead.RecordTypeInfosByName.get('Forward').RecordTypeId;
final Id rotaryRecordTypeId = Schema.SObjectType.Lead.RecordTypeInfosByName.get('Rotary').RecordTypeId;
final Id convertRecordTypeId = Schema.SObjectType.Lead.RecordTypeInfosByName.get('VSG Convert Lead').RecordTypeId;
set<Id> setRecordTypeId = new set<Id>{forwardRecordTypeId,directRecordTypeId,rotaryRecordTypeId,convertRecordTypeId};
set<Id> setOfLeadRepUserIds = new set<Id>();
set<Id> setOfLeadUserIds = new set<Id>();
List<LeadShare> leadShareListUpdate = new List<LeadShare>();
System.debug('@@@@Inside leadShareToRepUpdate method');
for(Lead instOfLead : listOfNewLeads){
setOfLeadUserIds.add(instOfLead.OwnerId);
List<LeadShare> lstOfLeadShare1 = [ Select Id, LeadId, userOrGroupId from LeadShare where LeadId = :instOfLead.Id ];
System.debug('@@lstOfLeadShare-1'+lstOfLeadShare1);
}
for( User instOfUser : [Select Id, Dvp2_IsPartnerUser__c, Dvp2_Is_Rep__c from User where Id IN :setOfLeadUserIds and Dvp2_IsPartnerUser__c = true and Dvp2_Is_Rep__c = true ]){
setOfLeadRepUserIds.add(instOfUser.Id);
}
System.debug('@@@@setOfLeadRepUserIds'+setOfLeadRepUserIds);
for(Lead newLead : listOfNewLeads){
System.debug('@@Condition-1'+setRecordTypeId.contains(newLead.RecordTypeId));
System.debug('@@Condition-2'+setOfLeadRepUserIds.contains(newLead.OwnerId));
System.debug('@@Printing Lead'+newLead.Id+'@@@@'+newLead.OwnerId+'@@@@'+newLead.OwnerId+'@@@@'+newLead);
if( setRecordTypeId.contains(newLead.RecordTypeId) && setOfLeadRepUserIds.contains(newLead.OwnerId) ){
System.debug('@@@@Condition met'+newLead.Id+'@@@@'+newLead.OwnerId+'@@@@'+newLead);
LeadShare ls = new LeadShare();
ls.leadId = newLead.Id;
ls.leadAccessLevel = 'Edit';
ls.userOrGroupId = newLead.OwnerId;
System.debug('@@Printing LeadShare record'+ls);
leadShareListUpdate.add(ls);
}
List<LeadShare> lstOfLeadShare2 = [ Select Id, LeadId, userOrGroupId from LeadShare where LeadId = :newLead.Id ];
System.debug('@@lstOfLeadShare-2'+lstOfLeadShare2);
}
System.debug('@@leadShareListUpdate.isEmpty()'+leadShareListUpdate.isEmpty());
if(!leadShareListUpdate.isEmpty()){
System.debug('@@@@Inside leadShareListNew'+leadShareListUpdate);
insert leadShareListUpdate;
}
}