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
AsitAsit 

How to fire an approval process when you delete a record of an object?

Hello,

 

The scenario is something like whenever a user "Jr. Executive" tries to delete a record of an object ,it cannot be deleted unless this action is approved by the "Manager" above in the hierarchy. I have tried this by the help  of  a dummy status field but was unable to execute it succesfully.

 

Please help me out!!

 

Regards

 

Asit Mishra

cmlcml

I believe you are trying to delete a record but before deletion of that record you need to have approval for deletion of that record and if approved you want to delete that record permanently.

 

I don't  think it will be possible to update and delete the same record in same transaction. Try deleting record in a future method when deletion of record is approved.

I hope this helps.

 

other wise you can also use another object to track your approval and then delete record once approval recieved for deletion.

 

Let me know if you need any furhter help on this.

 

Thanks

Starz26Starz26

You could override the delete button to use a vf page with a controller that submits the record for an applicable approval process (set a flag on the record and if true enter that process). then if the approval process is approved have it set a second flag that will fire off a trigger to delete the record (may have to be an @future method). If not approve, simply reset the delete flag.

AsitAsit
Hello, Thanks for the suggestion but can you please help me out with any code snippet of what you said !!
AsitAsit

Hello


Can you give the code snippet which may work and implement this logic??

cmlcml

1. First step would be to setup a approval process in place.

2. Override you standard 'Delete' button with a visualforce page. Suppose your object is 'Test_Object__C' .Your visualforce page could be like below ;

 

  <apex:page StandradController="Test_Object__C"  action="{!submitForApproval}" extensions="TestController">
  </apex> 

 

Your test controller will be something like below :

 

public Class TestController

{

  String recordId{get;set;}

  public TestController(ApexPages.StandradController standardController)

{

   recordId=ApexPages.currentPage().getParameters().get('id');

}

 

public PageReference submitForApproval()

{

  Test_Object__c tObj=new TestObject__c(id=recordId,Status='Submitted');

   update tObj;

    Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
     req.setComments('Submitted for approval. Please approve.');
     req.setObjectId(recordId); 

     try

     {

      Approval.ProcessResult result = Approval.process(req);
      } 

      catch(Exception e)

     {

      return null;

     }

  return null;

 } 

}

 

Above code will submit your record for approval when user hits delete button.

Now when it is approved by manager, you have to setup your approved actions or final rejections such a way that oon approval process updates 'status' field to 'Approved' and in case of rejection updates it to blank or whatever your requirement says.

 

So in case of approved you have to write a trigger to capture the event of update on 'Test_Object__c', it would be something like this:

 

trigger testTrigger on Test_Object__c (after update)

{

  List<Id> testObjIdList=new List<Id>();

 for (Test_Object__c tobj : trigger.New)

 {

  if(tobj.status =='Approved' && trigger.oldMap.get(tobj.Id).status=='Submitted')

  {

    testObjIdList.add(tobj.Id);

   }

 }

 

if(testObjIdList.size()>0)

{

 TestObjClass.deleteTestObject(testObjIdList);

 }

}

 

 

Now you have to write a future method 'deleteTestObject(Sting[] objIdList)' in TestObjClass. It will be like below :

 

public class TestObjClass

{

 

 @future

 

public static void deleteTestObject(Sting[] objIdList)

{

 List<Test_Object__c> testObjList=new List<Test_Object__c>();

 for(Id objId: objIdList)

 {

   Test_Object__c tobj=new Test_Object__c(id=objId);

    testObjList.add(tobj);

  }

 delete testObjList;

 }

}

 

Hopefully this should work.

 

 

Chandra

Blog: http://cmlohani-force.blogspot.in/

AsitAsit

Thanks for the help. I will just execute and see whether this works !!

AsitAsit

Hi,

 

Actually I have made a field Dummy_Status__C in my custom object Merchandise(Test_Object as in your snippet).

This dummy field takes a value 'F' by default and only after the manager approves the deletion it is updated as 'T'.

This may require a "<before delete>" trigger i guess.

Can you please improvise the code accordingly.

cmlcml

If you are not updating your status field at the time when user hit save, then you can remove first two lines in 'submitForApproval' method of 'TestController' class.

 

Replace 'status' with your 'Dummy_Status__C' and 'Submitted' with 'F' value and 'Approved' with 'T' value.

 

Also there is no need of before trigger as updation of 'Dummy_Status__C' field to 'T' can be taken care by Approval Process's Final Action after record is approved which you have to define when you would be defining Approval Process.

 

Thanks

Chandra

 

AsitAsit

Hi Chandra,

 

Thanks for the valuable suggestions. I have updated and the code works fine but the issue is with the approval which is not showing in CEO page.Also the VF for delete button works fine yet it shows "Insufficent Priviledges" when the user assigned to "Jr.Executive" deletes a record!!

 

Thank You

 

Regards

Asit

cmlcml

You have to enable that visualforce page to Uesr. You can do it either enabling it for User's profile or through permission set.

 

AsitAsit

 

 

Sorry to be disturbing you again but using the way to said to fire an approval, it is not firing an approval request to the admin or CEO (in my case). There is no such approval/reject request in my home page??

Please help me if you can.

 

Regards

Asit

 

 

cml26sepcml26sep

are you sending approval to user's manager? Then issue could be that CEO doesn't have a manager. You have to midify your approval process to handle that scenario. Something like if manager is null on a user record then send approval to someone.

 

AsitAsit

No the user here is under the role CFO and he is assigned under CEO who is the admin itself. So every request should be coming to the CEO right? Why is it not coming then ??

cml26sepcml26sep

you mean to say when CFO tries to delete record, approval process not firing and approval mail is not going to CEO. is that correct?

 

I am not sure why it is not happening.

is it working for other users ?

 

 Try to put some debug log and see if approval process is getting triggerred or not. Also modify approval process to send mail to someone else and see if that person getting that mail or not.

 

AsitAsit

Ya I already ran degug log and no approval process in getting fired for the CFO(user)!!

its showing error!

cml26sepcml26sep

Can you share the debug logs and error ?

AsitAsit
The following code is for the USER-role: CFO
APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
23:02:06.027 (27172000)|EXECUTION_STARTED
23:02:06.027 (27215000)|CODE_UNIT_STARTED|[EXTERNAL]|06690000001ByoV|VF: /apex/Approval
23:02:06.742 (30253000)|CUMULATIVE_LIMIT_USAGE
23:02:06.742|CUMULATIVE_LIMIT_USAGE_END

23:02:06.030 (30276000)|CODE_UNIT_FINISHED|VF: /apex/Approval
23:02:06.030 (30287000)|EXECUTION_FINISHED
This is the debug log for the CEO(admin):

25.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
03:07:19.029 (29033000)|EXECUTION_STARTED
03:07:19.029 (29067000)|CODE_UNIT_STARTED|[EXTERNAL]|06690000001ByoV|VF: /apex/Approval
03:07:19.037 (37502000)|CODE_UNIT_STARTED|[EXTERNAL]|01p90000001CpDW|TestController <init>
03:07:19.037 (37525000)|SYSTEM_MODE_ENTER|true
03:07:19.038 (38457000)|METHOD_ENTRY|[1]|01p90000001CpDW|TestController.TestController()
03:07:19.038 (38541000)|METHOD_EXIT|[1]|TestController
03:07:19.038 (38606000)|SYSTEM_METHOD_ENTRY|[6]|ApexPages.currentPage()
03:07:19.038 (38666000)|SYSTEM_METHOD_EXIT|[6]|ApexPages.currentPage()
03:07:19.038 (38695000)|SYSTEM_METHOD_ENTRY|[6]|System.PageReference.getParameters()
03:07:19.038 (38747000)|SYSTEM_METHOD_EXIT|[6]|System.PageReference.getParameters()
03:07:19.038 (38773000)|SYSTEM_METHOD_ENTRY|[6]|MAP.get(ANY)
03:07:19.038 (38794000)|SYSTEM_METHOD_EXIT|[6]|MAP.get(ANY)
03:07:19.038 (38816000)|METHOD_ENTRY|[6]|01p90000001CpDW|TestController.__sfdc_recordId(String)
03:07:19.038 (38829000)|METHOD_EXIT|[6]|01p90000001CpDW|TestController.__sfdc_recordId(String)
03:07:19.038 (38847000)|CODE_UNIT_FINISHED|TestController <init>
03:07:19.038 (38940000)|CODE_UNIT_STARTED|[EXTERNAL]|01p90000001CpDW|TestController invoke(submitForApproval)
03:07:19.039 (39247000)|METHOD_ENTRY|[15]|01p90000001CpDW|TestController.__sfdc_recordId()
03:07:19.039 (39279000)|METHOD_EXIT|[15]|01p90000001CpDW|TestController.__sfdc_recordId()
03:07:19.039 (39319000)|SYSTEM_METHOD_ENTRY|[18]|Approval.process(Approval.ProcessRequest)
03:07:19.039 (39377000)|DML_BEGIN|[18]|Op:Process|Type:ProcessRequest|Rows:1
03:07:19.059 (59678000)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:ApprovalProcessActions
03:07:19.070 (70906000)|WF_APPROVAL_SUBMIT|[Merchandise: Cell Phones a0090000003P5Pq]
03:07:19.105 (105995000)|WF_RULE_FILTER|[Merchandise : Dummy Status equals F]
AND [User : Role not equal to CEO]
03:07:19.106 (106023000)|WF_RULE_EVAL_VALUE|F
03:07:19.106 (106035000)|WF_RULE_EVAL_VALUE|00E90000000qu4J
03:07:19.106 (106050000)|WF_EVAL_ENTRY_CRITERIA|Deletion Approval|00X90000000PKE8|false
03:07:19.106 (106057000)|WF_NO_PROCESS_FOUND
03:07:19.110 (110542000)|CODE_UNIT_FINISHED|Workflow:ApprovalProcessActions
03:07:19.110 (110624000)|DML_END|[18]
03:07:19.110 (110721000)|EXCEPTION_THROWN|[18]|System.DmlException: Process failed. First exception on row 0; first error: NO_APPLICABLE_PROCESS, No applicable approval process found.: []
03:07:19.111 (111455000)|SYSTEM_METHOD_EXIT|[18]|Approval.process(Approval.ProcessRequest)
03:07:19.120 (120458000)|CODE_UNIT_FINISHED|TestController invoke(submitForApproval)
03:07:19.121 (121490000)|VF_APEX_CALL|j_id0|{!submitForApproval}|PageReference: none
03:07:20.073 (144901000)|CUMULATIVE_LIMIT_USAGE
03:07:20.073|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 0 out of 100
Number of query rows: 0 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 1 out of 150
Number of DML rows: 1 out of 10000
Number of script statements: 7 out of 200000
Maximum heap size: 0 out of 6000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 100
Number of record type describes: 0 out of 100
Number of child relationships describes: 0 out of 100
Number of picklist describes: 0 out of 100
Number of future calls: 0 out of 10

03:07:20.073|CUMULATIVE_LIMIT_USAGE_END

03:07:19.144 (144932000)|CODE_UNIT_FINISHED|VF: /apex/Approval
03:07:19.144 (144945000)|EXECUTION_FINISHED

cml26sepcml26sep

It looks like your process entry criteria is not getting satisfied :

It is executing following criteria and it is not finding any approval process satisfying below criteria and that is why it is failing:

 

WF_RULE_FILTER|[Merchandise : Dummy Status equals F]
AND [User : Role not equal to CEO]

AsitAsit

Hi actually the thing is an approval process has been created but the snippet u gave is unable to use the approval process (i.e here named  as " Deletion Approval")

cml26sepcml26sep

As i told you approval process is not getting triggerred because it is satisfying entry criteria. Code will trigger the approval proces which satisfies the entry process.

 

Just for testing remove the filter criteria and make it 'true' only in your approval process entry criteria and see if you get that error again.