I had master detailed relationship with the Case and Payment1,2 etc object. Now this relationship is changed to lookup. Now OWD
for Case
, Payment1,2
is Private. Now User is not able to access the old cases, basically unable to see any records of Payment1,2 at all.
Now I need to identify all child object records of that Case and need to make them a owner like as Case.
Could you please confirm what do I need to do ?
- Make Child Object as Owner like Case?
- Do I need to create Child Share Object ?
Here is the code
Set<Id> caseOwnerUpdatesSet = new Set<Id>();
caseOwnerUpdatesSet.add('KKK'); // Here will provide the list of Ids
Map<Id, Case> newCasesMap = new Map<Id,Case>([SELECT Id, OwnerId FROM Case WHERE Id IN ('KKK')]);
Map<Id, List<Payment1__c>> paymentsiInfoMap = new Map<Id, List<Payment1__c>>();
List<Payment1__c> payment1InfoToUpdateOwnerLst = new List<Payment1__c>();
for(Payment1__c si : [SELECT Id, OwnerId, Case__c
FROM Payment1__c
WHERE Case__c IN :caseOwnerUpdatesSet]){
if(!paymentsiInfoMap.containsKey(si.Case__c))
paymentsiInfoMap.put(si.Case__c, new List<Payment1__c>{si});
else
paymentsiInfoMap.get(si.Case__c).add(si);
}
for(Case objCase : newCasesMap.values()){
// check Owner to be updated present
if(caseOwnerUpdatesSet.contains(objCase.Id) && paymentsiInfoMap.containsKey(objCase.Id)){
for(Payment1__c si : paymentsiInfoMap.get(objCase.Id)){
si.OwnerId = objCase.OwnerId;
payment1InfoToUpdateOwnerLst.add(si);
}
}
}
if(!payment1InfoToUpdateOwnerLst.isEmpty()){
update payment1InfoToUpdateOwnerLst;
}
Map<Id, List<Payment_2__c>> si2InfoMap = new Map<Id, List<Payment_2__c>>();
List<Payment_2__c> payment2InfoToUpdateOwnerLst = new List<Payment_2__c>();
// Get all Service Info records for which owner has to be changed
for(Payment_2__c si2 : [SELECT Id, OwnerId, Case__c
FROM Payment_2__c
WHERE Case__c IN :caseOwnerUpdatesSet]){
if(!si2InfoMap.containsKey(si2.Case__c))
si2InfoMap.put(si2.Case__c, new List<Payment_2__c>{si2});
else
si2InfoMap.get(si2.Case__c).add(si2);
}
for(Case objCase : newCasesMap.values()){
// check Owner to be updated present
if(caseOwnerUpdatesSet.contains(objCase.Id) && si2InfoMap.containsKey(objCase.Id)){
for(Payment_2__c si2 : si2InfoMap.get(objCase.Id)){
si2.OwnerId = objCase.OwnerId;
payment2InfoToUpdateOwnerLst.add(si2);
}
}
}
if(!payment2InfoToUpdateOwnerLst.isEmpty()){
update payment2InfoToUpdateOwnerLst;
}