function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Cris9931Cris9931 

trigger to not let users to edit records if they are not the creator or the owner

Hello, I have a requirement to create a trigger to not allow users to edit records if they are not the creator OR if they are not the owner. Owner can be a simple user or a QUEUE.
 

This is what I wrote until now... my code is not working.

 

trigger permissionToEditAdminTasks on SIG_Administrative_Task__c (after update) {
  
      //fetch the id of the creator of the record <start>
           Set<String> ownerIds = new Set<String>();
        for (SIG_Administrative_Task__c record : Trigger.New) {
            ownerIds.add(record.OwnerId);
             
        }
        
       
        
      // <end>
        
        List<Group> groups = [SELECT Id, (SELECT Id, UserOrGroupId FROM GroupMembers) FROM Group WHERE Id =: ownerIds];
        System.debug('####groups' + groups);    
        
          
        Map<String, Set<String>> memberIdsByGroupId = new Map<String, Set<String>>();
        
        //fetch the id's of the users from the the current owner queue  <start>
        for (Group groupRec : groups) {
            Set<String> memberIds = new Set<String>();
            for (GroupMember member : groupRec.GroupMembers) {
                memberIds.add(member.UserOrGroupId);
            }
               System.debug('####memberIds  ' + memberIds );   
        }
        // <end>
       

        for (SIG_Administrative_Task__c record : Trigger.New) {
            if (memberIdsByGroupId.get(record.OwnerId) != null && memberIdsByGroupId.get(record.OwnerId).contains(UserInfo.getUserId()))
            {
             
            }else{
                 record.addError('You have not permission for editing this record');
                  System.debug('record.OwnerId' + record.OwnerId);
            }
        }
    

}
I cannot edit the record no matter what, I don't know what is wrong in my code.. please help.
SUCHARITA MONDALSUCHARITA MONDAL
Hi Sarah,
You can do this using Configuration, over object you can give READ access and other user who needs to have Edit/Delete access, assign them using permission set..  So,  using READ access user can edit/read it's own record until and unless other share their records with them/ or higher in Role hierarchy.

For trigger, try to check whether the logged In user comes in owner/createdby list, if no then before saving --> show error message. (trigger will on Before update)..

I hope it'll help.

Thanks,
Sucharita
Cris9931Cris9931
trigger permissionToEditAdminTasks on SIG_Administrative_Task__c (before update) {
          public Set<String> memberIds = new Set<String>();
          String currentUserID = UserInfo.getUserId();
          SIG_Administrative_Task__c admTask = new SIG_Administrative_Task__c();
          
          
     
      //fetch the id of the creator of the record <start>
           Set<String> ownerIds = new Set<String>();
        for (SIG_Administrative_Task__c record : Trigger.New) {
            ownerIds.add(record.OwnerId);
             
        }
        
      // <end>
        
        List<Group> groups = [SELECT Id, (SELECT Id, UserOrGroupId FROM GroupMembers) FROM Group WHERE Id =: ownerIds];
        System.debug('####groups' + groups);    
        
          
         Set<String> memberIdsByGroupId = new Set <String>();
   
        //fetch the id's of the users from the the current owner queue  <start>
        for (Group groupRec : groups) {
         
            for (GroupMember member : groupRec.GroupMembers) {
                memberIds.add(member.UserOrGroupId);
            }
              
        }
        // <end>
       system.debug('memberIds123'+memberIds);

        for (SIG_Administrative_Task__c record : Trigger.New) {
            if(memberIds.contains(currentUserID) || record.createdById == UserInfo.getUserId())
            {
             
            }
            else
            {
               record.addError('You need to be the owner or to be inside of a queue to edit an Admin Task');
            }
        }
    

}

Fix it.
Now the creator or the owner(user must be inside the queue) can edit the records. others cannot :)