Activity › Forums › Salesforce® Discussions › How to share/delete the records through Apex?
Tagged: Database, Salesforce Apex, Salesforce Records
-
How to share/delete the records through Apex?
Posted by Himanshu on April 30, 2016 at 5:19 PMHow to share/delete the records through Apex?
Gourav replied 9 years, 11 months ago 2 Members · 1 Reply -
1 Reply
-
Hope this will help to share records using Apex:-
public class JobSharing {
public static boolean manualShareRead(Id recordId, Id userOrGroupId){
// Create new sharing object for the custom object Job.
Job__Share jobShr = new Job__Share();// Set the ID of record being shared.
jobShr.ParentId = recordId;// Set the ID of user or group being granted access.
jobShr.UserOrGroupId = userOrGroupId;// Set the access level.
jobShr.AccessLevel = ‘Read’;// Set rowCause to ‘manual’ for manual sharing.
// This line can be omitted as ‘manual’ is the default value for sharing objects.
jobShr.RowCause = Schema.Job__Share.RowCause.Manual;// Insert the sharing record and capture the save result.
// The false parameter allows for partial processing if multiple records passed
// into the operation.
Database.SaveResult sr = Database.insert(jobShr,false);// Process the save results.
if(sr.isSuccess()){
// Indicates success
return true;
}
else {
// Get first save result error.
Database.Error err = sr.getErrors()[0];// Check if the error is related to trival access level.
// Access level must be more permissive than the object’s default.
// These sharing records are not required and thus an insert exception is acceptable.
if(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION &&
err.getMessage().contains(‘AccessLevel’)){
// Indicates success.
return true;
}
else{
// Indicates failure.
return false;
}
}
}}
To delete records try this:-
global class deleteSubscribers implements Database.Batchable<sObject>{
global final String Query;
global deleteSubscribers(String q){
Query=q;
}global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}global void execute(Database.BatchableContext BC,List<Subscriber__c> scope){
delete scope;
}global void finish(Database.BatchableContext BC){}
}
Log In to reply.