4

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?

2

This is in the documentation for "share" objects:

Customer Portal users can't access this object.

Therefore, you need to use one of the other methods:

  1. You can do this in a trigger, which always operate "without sharing" by default (preferred).
  2. You can have an inner class "without sharing," which will allow the DML to succeed.
  3. You can have a separate class, such as a utility class, that houses the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.