You need to sign in to do that
Don't have an account?
Rupesh A 8
How to share multiple object's records in Batch Apex
Hi,
I have two custom objects like Private1 and Private2. For these two objects given lookup relationship to Account object. Here whatever the records are entered into these two objects then that particular Account team member can able to see the two custom object's records.
Actually, I have tried for one custom object and below is the code but I need the same scenario for multiple objects. Can anybody please help me on this.
And also I am new to salesforce inaddition to this, Can anybody help on test class for the above scenario?
Single Object record sharing (Private1):
global class PrivateSharingAccMember implements Database.Batchable<sObject> {
static String emailAddress = 'rupesh@gmail.com';
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator([select Id, Account__c from Private1__c]);
}
global void execute(Database.BatchableContext BC, List<sObject> scope) {
Map<ID, Private1__c> pvtMap = new Map<ID, Private1__c>((List<Private1__c>)scope);
List<Private1__Share> newJobShrs = new List<Private1__Share>();
List<Private1__Share> oldJobShrs = [select Id from Private1__Share WHERE ParentId IN :pvtMap.keySet() AND (RowCause = :Schema.Private1__Share.rowCause.Private_One__c)];
for(Private1__c pvt : pvtMap.values()){
Private1__Share pvtAccountShr = new Private1__Share();
pvtAccountShr.ParentId = pvt.Id;
pvtAccountShr.UserOrGroupId = pvt.Account__c;
pvtAccountShr.AccessLevel = 'Read';
pvtAccountShr.RowCause = Schema.Private1__Share.RowCause.Private_One__c;
newJobShrs.add(pvtAccountShr);
}
}
global void finish(Database.BatchableContext BC){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddress = new String[] {emailAddress};
mail.setToAddresses(toAddress);
mail.setSubject('Apex Sharing Records Completed');
mail.setPlainTextBody('The Apex Sharing Records finished processing');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
Please help me on this at your earliest convenience anyone.
I have two custom objects like Private1 and Private2. For these two objects given lookup relationship to Account object. Here whatever the records are entered into these two objects then that particular Account team member can able to see the two custom object's records.
Actually, I have tried for one custom object and below is the code but I need the same scenario for multiple objects. Can anybody please help me on this.
And also I am new to salesforce inaddition to this, Can anybody help on test class for the above scenario?
Single Object record sharing (Private1):
global class PrivateSharingAccMember implements Database.Batchable<sObject> {
static String emailAddress = 'rupesh@gmail.com';
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator([select Id, Account__c from Private1__c]);
}
global void execute(Database.BatchableContext BC, List<sObject> scope) {
Map<ID, Private1__c> pvtMap = new Map<ID, Private1__c>((List<Private1__c>)scope);
List<Private1__Share> newJobShrs = new List<Private1__Share>();
List<Private1__Share> oldJobShrs = [select Id from Private1__Share WHERE ParentId IN :pvtMap.keySet() AND (RowCause = :Schema.Private1__Share.rowCause.Private_One__c)];
for(Private1__c pvt : pvtMap.values()){
Private1__Share pvtAccountShr = new Private1__Share();
pvtAccountShr.ParentId = pvt.Id;
pvtAccountShr.UserOrGroupId = pvt.Account__c;
pvtAccountShr.AccessLevel = 'Read';
pvtAccountShr.RowCause = Schema.Private1__Share.RowCause.Private_One__c;
newJobShrs.add(pvtAccountShr);
}
}
global void finish(Database.BatchableContext BC){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddress = new String[] {emailAddress};
mail.setToAddresses(toAddress);
mail.setSubject('Apex Sharing Records Completed');
mail.setPlainTextBody('The Apex Sharing Records finished processing');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
Please help me on this at your earliest convenience anyone.
All Answers
Is your code working properly for the single object ? Why u not writing the trigger for this scenerio, like whenever u create a record it will get share with the Account Team Member. Your batch class will be schedule on once a day, or u have u run it manually every time. Let me know if your above code working.
Thanks for the reply. Yes, the above code is working fine for one object and for one Account Team Member for that Account. Actually I have given that particular user id in the place of "pvt.Account__c" in the above code. Now, I need if I assign multiple Account team members for one Account then for all the team members should able to see the records. And the second case is, it should work for multiple objects.
As you mentioned that, we can able to write trigger also but my requirement is to write Batch class for this. Please help me on this class and also test class.
Thanks.
Please try the following code,
I have added few comments as well, please read them as well.
As you said, I have place Account field API name which I have create in Pravate1 object in the place of ID. But is showing the below error.
Line 14: invalid ID field: Account__c (// Here Account__c is the field of Private1 Object)
Instead of the field name directly whatelse we can give. Thanks.
Use Account__r.Id in place of Account__c, also add this field in your soql query.
Private1__c p = [Select Id, Account__c, Account__r.Id from Private1__c where Id = 'a009000002p9SeP' limit 1];
AccountTeamMember acc = [Select Id , AccountId from AccountTeamMember where AccountId =: p.Account__r.Id];
system.debug('*********'+acc);
ok Rohit, If this is the case how you will write the trigger. Can you please provide me that......
Thanks.
Please find "OnePrivateSharingAccMember" Apex class in my dev edition. Credentials I have sent to your email id.
Thanks.