Below trigger is giving error while creating sharing records, can some suggests where is the issue. Sharing setting is set to private for external user on this object.


AccessLevel (trivial share level Edit, for organization with default level Edit

trigger grnk_PunchListShare_Trigger on grnk_PunchList__c (after insert, after update) {

    if(trigger.isInsert || trigger.isUpdate){

        List<grnk_PunchList__Share> punchListShares = new List<grnk_PunchList__Share>();
        List<grnk_PunchList__Share> punchListSharesUpdate = new List<grnk_PunchList__Share>();

        List<grnk_PunchList__Share> punchInsertedRcrd = new List<grnk_PunchList__Share>([Select id,ParentId,UserOrGroupId,AccessLevel,RowCause from grnk_PunchList__Share]);
        Map<Id,grnk_PunchList__Share> MapParIdShareObj = new Map<Id,grnk_PunchList__Share>();
        for(grnk_PunchList__Share punchShare : punchInsertedRcrd){
        User u = [Select ContactId, Contact.AccountId From User Where Id = :UserInfo.getUserId()];
        if(u.ContactId == null) return;
        Id AccountId = u.Contact.AccountId;
        system.debug('User Account Id::'+AccountId);

        Set<Id> allInsertedPLIds = trigger.newMap.keySet();
        Set<Id> setAccountIdOnPunchList = New set<Id>();
        List<grnk_PunchList__c> lstNewPunchList = trigger.new;
        for(grnk_PunchList__c plRecInst : trigger.new){

        Map<id,List<Contact>> MapIdAccLstConts = new Map<Id,List<Contact>>();
        for(account acc : [Select id,Name,(select Id,Name,OwnerId from contacts) From Account where id=:setAccountIdOnPunchList]){

        for(grnk_PunchList__c plRecInst : trigger.new){

            if(plRecInst.grnk_Account__c != null && MapIdAccLstConts.containsKey(plRecInst.grnk_Account__c) && AccountId == plRecInst.grnk_Account__c){
                if(MapIdAccLstConts.get(plRecInst.grnk_Account__c).size()>0 && MapIdAccLstConts.get(plRecInst.grnk_Account__c) != null){

                    for(Contact con : MapIdAccLstConts.get(plRecInst.grnk_Account__c)){

                            grnk_PunchList__Share updateShare = MapParIdShareObj.get(plRecInst.id);
                            updateShare.UserOrGroupId = con.OwnerId;
                            updateShare.AccessLevel = 'Edit';
                            updateShare.RowCause = Schema.grnk_PunchList__Share.RowCause.grnk_Access_PunchList_By_Customer__c;
                            grnk_PunchList__Share pucnhListShareRec = new grnk_PunchList__Share();
                            pucnhListShareRec.ParentId = plRecInst.Id; // record to be shared Id
                            pucnhListShareRec.UserOrGroupId = con.OwnerId; // User Id
                            pucnhListShareRec.AccessLevel = 'Edit';
                            pucnhListShareRec.RowCause = Schema.grnk_PunchList__Share.RowCause.grnk_Access_PunchList_By_Customer__c;


        if(punchListShares.size()>0 && punchListShares != null){
            Database.SaveResult[] punchListInsertResult = Database.insert(punchListShares,false);
            // Iterate through each returned result
            for (Database.SaveResult sr : punchListInsertResult) {
                if (sr.isSuccess()) {
                    // Operation was successful, so get the ID of the record that was processed
                    System.debug('Successfully inserted account. Account ID: ' + sr.getId());
                }else {
                    // Operation failed, so get all errors                
                    for(Database.Error err : sr.getErrors()) {
                        System.debug('The following error has occurred.');                    
                        System.debug(err.getStatusCode() + ': ' + err.getMessage());
                        System.debug('Account fields that affected this error: ' + err.getFields());
        if(punchListSharesUpdate.size()>0 && punchListSharesUpdate != null){
            Database.SaveResult[] punchListUpdateResult = Database.update(punchListSharesUpdate,false);


1 Answer 1


Your custom sharing rows have to provide more access than the Organization-Wide Default. In this case, the default for your external users is irrelevant, because you're inserting at least some shares to internal users - the owners of all of the Contacts on the Account. con.OwnerId does not give you the Id of their Community User.

Note from Using Apex Managed Sharing to Create Custom Record Sharing Logic, under Sharing Table -> Access Level,

This field must be set to an access level that is higher than the organization’s default access level for the parent object. For more information, see Access Levels.

As a side note, this

if(MapIdAccLstConts.get(plRecInst.grnk_Account__c).size()>0 && MapIdAccLstConts.get(plRecInst.grnk_Account__c) != null){

is backwards. Your null guard won't short-circuit the NullPointerException if it comes second.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .