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
Apex developer 21Apex developer 21 

Create a trigger to remove create edit delete record on note record on cusom object on condition aproval is pending

Hi All, Please help!!

I need to create a trigger on a custom object with a related list note, where the create edit delete permission is removerd from the note record when the approval is set to pending. Could you help with an example and point me in the right direction. I have attached an example below to make thing clearer.
User-added image
HARSHIL U PARIKHHARSHIL U PARIKH
This is automatically set by approval process by it self!
When the record is sent for an approval.. Status is pending and whomever submitted the record he/she can't edit it.

Right now, you are loggedIn as admin that's why you are able to edit it otherwise you won't be able to.

Try to do the following,
1) Try to submit the record for an approval from any other user besides system admin profile
2) See he/she can edit it during these pending seasion
3) Once approver approves the record then record is locked as well. If approver rejects it then record becomes unlocked. If approvers recalls it then it becomes unlocked. But as I said before, when submitter submits for an approval record automatically becomes locked.
Look for "Initial Submission Actions" under approval process, you will see that you cannot modify the Record Lock action.

Hope this helps!
Apex developer 21Apex developer 21
Thanks for you explanation Govind. Maby i didnt explain it very wel the record is locked, but what i want to achieve is that the Notes are locked when the approval is 'pending'. I managed to make a trigger but i get the error: 

caused by: System.QueryException: Implementation restriction: ContentDocumentLink requires a filter by a single Id on ContentDocumentId or LinkedEntityId using the equals operator or multiple Id's using the IN operator.: Trigger.Locked: line 3, column 1.

Here is my trigger:
trigger Locked on ContentVersion (before insert, before update, before delete) {
	for (ContentVersion c : Trigger.new)  {
        for (ContentDocumentLink link : [
            SELECT LinkedEntityId 
            FROM ContentDocumentLink
            WHERE ContentDocumentId = :c.ContentDocumentId
        ]){
            Id parentId = link.LinkedEntityId;
            System.debug('link.LinkedEntityId '+ link);
            
            Boolean isOrderRegel = parentId.getSObjectType() == Orderregel__c.SObjectType;
            
            if (isOrderRegel && Approval.isLocked(parentId)){
                c.addError('The order note is locked.');
            }
		}
    }
}

Please help.
HARSHIL U PARIKHHARSHIL U PARIKH
Try to accomplish this via changing a page layouts and record Types. (RT - 1 /PG - 1    and    RT - 2/PG - 2)
1) Create a two pagelayouts (Pg-1 & Pg-2) and one of them (Pg-1) have notes section visiable and on other one (Pg-2) make in not visiable.
2) Create a field on record named Approval_Status__c which has picklist values as New, Pending, Approved, Recalled, Rejected. Make sure your approval is updating an appropriate picklist value. (E.g., if it's pending then Approval_Status__c should be Pending - This can be accomplished right from Approval Process field update)
3) Your initial record type and pagelayout should be RT-1/PG-1 (This one has Nots section visiable).
4) Create a workflow which changes the record type and pagelayout to RT-2/PG-2 (the one doesn't have Notes section visiable) when Approval_Status__c is "Pending."

I am not 100% sure if this works out but this is what I can think of initially & There is one down side for this .. Which is approver won't be able to see the notes prior to approval since the status is pending and there is no related list of Notes. <-- Even this can be workout with profiles though..

Like.. if Approver is from different Profile then create an additional Page Layout PG-3 for them which has related-list of "Notes" will be visiable all the time.

Try it out if it works..