You need to sign in to do that
Don't have an account?
Cris9931
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.
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