On the project I'm currently working with, we have Apex Sharing Rules. I see we are passing rowCause as a String everywhere. But the values of these Strings look like API names.
Is there a typesafe way we can work with these?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.Sign up to join this community
Refer this documentation: Sharing a Record Using Apex
Apex sharing reasons are defined on an object's detail page. Each Apex sharing reason has a label and a name:
All Apex sharing reason names have the following format:
Apex sharing reasons can be referenced programmatically as follows:
For example, an Apex sharing reason called Recruiter for an object called Job can be referenced as follows:
So, sharing reason must be used which you have defined and that's why it takes API name. It always hold String value.
Just to test out... I created a sharing reason for a test object and ran the following line in Developer Console:
System.debug(Schema.MyObj__Share.rowCause.MyCause__c instanceof String);
I got back the error Operation instanceof is always true since an instance of String is always an instance of String. So that tells me it returns a String and not some special type.
However if you want to validate that it's a proper RowCause, you can do:
List<Schema.PicklistEntry> rowCauses = MyObj__Share.rowCause.getDescribe().getPicklistValues();
The results you get back will include all the custom row causes and the standard allowable ones too.