You need to sign in to do that
Don't have an account?
Skeeter
Apex sharing
I am trying to create apex sharing for read-only access on a private object to a custom lookup field, but it doesn't seem to be making the record visible.
Any help is greatly appreciated.
Any help is greatly appreciated.
Trigger Requester_Ticket_Sharing on BMCServiceDesk__Incident__c (after insert, after update) { if(trigger.isInsert){ List<BMCServiceDesk__Incident__Share> ticketShares = new List<BMCServiceDesk__Incident__Share>(); for(BMCServiceDesk__Incident__c inc : trigger.new){ BMCServiceDesk__Incident__Share hiringManagerShare = new BMCServiceDesk__Incident__Share(); hiringManagerShare.ParentId = inc.Id; hiringManagerShare.UserOrGroupId = inc.Requester__c; hiringManagerShare.AccessLevel = 'Read'; hiringManagerShare.RowCause = Schema.BMCServiceDesk__Incident__Share.RowCause.Requester_Access__c; // Add the new Share record to the list of new Share records. ticketShares.add(hiringManagerShare); } // Insert sharing records and capture save result Database.SaveResult[] lsr = Database.insert(ticketShares,false); // Create counter Integer i=0; // Process the save results for(Database.SaveResult sr : lsr){ if(!sr.isSuccess()){ // Get the first save result error Database.Error err = sr.getErrors()[0]; if(!(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION && err.getMessage().contains('AccessLevel'))){ // Throw an error when the error is not related to trivial access level. trigger.newMap.get(ticketShares[i].ParentId). addError( 'Unable to grant sharing access due to following exception: ' + err.getMessage()); } } i++; } } }
Not sure what is wrong in your code, but below are some links which have explanation thought it could help you.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_bulk_sharing_creating_with_apex.htm
http://www.jitendrazaa.com/blog/salesforce/apex-based-sharing-in-salesforce/
http://salesforce.stackexchange.com/questions/123966/record-sharing-using-lookup-field-criteria-using-apex-similar-to-controlled-by
Hope this helps you!
If this helps you please mark it as solved.
Thanks and Regards
Sandhya