0

Whenever I create a Survey record, it is automatically shared with "All Internal Users". This is not letting me share the records according to Sharing settings. Instead of manually deleting that row, I want to delete it through trigger After Insert. I have tried using SurveyShare but still not working as expected.enter image description here

Below is the code I am trying to delete the highlighted sharing.

try{
        for(Survey surveyShareToBeDeleted : newSurvey){
            System.debug('New Survey Id ' + surveyShareToBeDeleted.Id);
            SurveyShare surveyShare  = new SurveyShare();
            allInternalUser = [SELECT Id FROM Group WHERE DeveloperName = 'AllInternalUsers' LIMIT 1];
            System.debug('All Internal User Group Id ' + allInternalUser.Id);
            // Set the ID of record being shared.
            surveyShare.ParentId = surveyShareToBeDeleted.Id;

            // Set the ID of user or group being granted access.
            surveyShare.UserOrGroupId = allInternalUser.Id;

            // Set the access level.
            surveyShare.AccessLevel = 'Read';

            // Set rowCause to 'manual' for manual sharing.
            // This line can be omitted as 'manual' is the default value for sharing objects.
            surveyShare.RowCause = Schema.SurveyShare.RowCause.Manual;
            System.debug('Row cause fetched ' + surveyShare.RowCause);

            // Insert the sharing record and capture the save result. 
            // The false parameter allows for partial processing if multiple records passed 
            // into the operation.
            Database.SaveResult sr = Database.insert(surveyShare,false);
        }

        List<SurveyShare> surveySharesToDelete = [SELECT Id 
                                                  FROM SurveyShare 
                                                  WHERE ParentId IN :trigger.newMap.keyset() 
                                                  AND RowCause = 'Manual'];
                                                  System.debug('Size of list ' + surveySharesToDelete.size());
        if(!surveySharesToDelete.isEmpty()){
            Database.Delete(surveySharesToDelete, false);
        }
    }
    catch(Exception ex){
        System.debug('Exception occured while deleting survey sharing for All Internal Users ' + ex);
    }
}

Can anyone please help me with this? Thanks!

  • 1
    You'll need to do more debugging before we can help you. Without an error message or something to work from, there's not much we can do to help you. – sfdcfox Apr 13 at 9:11
  • 1
    I’m super confused. The code you share is inserting the rowshare you want to delete. (And then you’re attempting to delete it). What’s worse, you’re inserting it inside a loop so you’re going to violate limits. Seems like we’re missing a lot of code. – Sebastian Kessel Apr 13 at 15:25

Your Answer

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

Browse other questions tagged or ask your own question.