0

I have created an approval process and provided field update such that each time I submit a record for approval,the pick list value changes to 'Approval Pending'. On continuation to this, I have added a trigger to capture the user who requests for the approval and the one who approves it.

In short field updates have been provided to the approval process and each time a person requests for approval the picklist value changes to Approval Pending. After a user approves it, the value changes to 'Approved'. If the value becomes 'Approval Pending', the requested user's data is captured. If the value changes to 'Approved', the approved user's data is captured.

Attaching the code:

trigger Approval_Trigger on Grant_Contract__c (after update) {
    User gUser = new User();
    gUser=[SELECT Id FROM User WHERE Id =:UserInfo.getUserId()];

    List<Grant_Contract__c> lstAcc= new List<Grant_Contract__c>();
   
        For(Grant_Contract__c c:trigger.New){
            If(RecursiveHandler.IsNotRecursive){
        RecursiveHandler.IsNotRecursive = false;
                If( c.Approval_process__c =='Approval pending'){
                    Grant_Contract__c obj=new Grant_Contract__c();
                    obj.id= c.Id;
                    obj.Requested_By__c = gUser.Id;
                    System.debug('obj.Requested_By__c'+obj.Requested_By__c);
                    lstAcc.add(obj);
                }
                else if(c.Approval_process__c=='Approved'){
                    Grant_Contract__c obj=new Grant_Contract__c();
                    obj.id= c.Id;
                    obj.Approved_By__c = gUser.Id;
                    System.debug('obj.Approved_By__c'+obj.Approved_By__c);
                    lstAcc.add(obj);
                }
            }
        }    
        update lstAcc;
        System.debug('lstAcc'+lstAcc);
    }  

Issues:

But right now if any user manually changes the picklist value instead of submitting for approval that users name is recorded in the field.

The modification I require is that:

  1. I should be able to lock the records to a particular profile say 'FA' such that only users assigned to the 'FA' and the System Administrator should have access to edit the records.

  2. These fields should get the value of the requested and approved user details only through submit for approval process and once a value is obtained it should be locked. It shouldn't be overwritten again.

How can I do this? Is it possible in the trigger?

2
  • 1
    Please stop spamming tags. Tags are meant to describe what things a question contains. The "community" tag, for example, is for asking about Community Sites (now known as Experience Sites). You can add up to 5 tags to a question, but that doesn't mean that you should or need to add 5. Also, in English, there is always one space after punctuation. One question?This is wrong vs One question? This is right (same thing for commas, exclamation marks, periods/full-stops, etc...).
    – Derek F
    Jun 27 at 11:14
  • @DerekF Okay will check on the tags from now on.
    – JK123
    Jun 27 at 11:32

0

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.