Schema.getGlobalDescribe().get()
on a custom Share object is returning null when the user is a CP user and the class has the with sharing modifier even though the custom object has an OWD of private. If you remove with sharing or invoke the transaction as a standard SF user, it works as expected. I have other methods in the class that need sharing rule enforcement.
static sObject shareRecordWithUser(Id recordId, String objectName, Id userId) {
Schema.SObjectType shareType = Schema.getGlobalDescribe().get(objectName + '__Share');
SObject share = shareType.newSObject(); // <--- NullPointerException
share.put('ParentId', recordId);
share.put('UserOrGroupId', userId);
share.put('AccessLevel', 'Edit');
return share;
}
Any ideas on what's causing this?