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
ramya1ramya1 

Approval Process not getting called Automatically while using Save&New Button

Hi

 

I called the Approvalprocess dynamically as soon as the record is created by using the code in the following method.

 

 public pagereference updateTimeSheet(){
         
         Timesheet__c getCurrentTimesheet = [select id,Comments__c,Project_Task__r.Assigned_To__c,Project_Task__r.CreatedById ,Timesheet__c.Project__r.ownerid,Approve__c,Approval_Status__c from timesheet__c where id=:this.id];
         
         system.debug(getCurrentTimesheet);
         User[] Approver = [select id from user where id=:getCurrentTimesheet.Project_Task__r.Assigned_To__c];
         system.debug('!!!!!!!!!!!!!!!!!!!!!!'+Approver );
         id[] uid = new id[]{};
         uid.add(Approver[0].id);
         
         Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
         app.setObjectId(getCurrentTimesheet.id);
         app.setNextApproverIds(uid);
         Approval.ProcessResult result = Approval.process(app);      
        
         update getCurrentTimesheet;
         
         return null;
    }

 

I Called the method on the Page..So that when the records are created,Approval process will be called Automatically..

 

Page :

 

<apex:page standardController="Timesheet__c" extensions="ApproveBillController" action="{!updateTimeSheet}">
 
</apex:page>

 

Issue is,When a Timesheet is created using Save button,then the approval process is getting called..

 

But,When we use Save&New  button while creating multiple records..Then the Approval process is not getting called.

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
I presume that the page overrides Timesheet__c.View action? If so, then Save & New won't call that code because the View page is never shown. Instead, you'll need to use a trigger to accomplish this goal.

All Answers

sfdcfoxsfdcfox
I presume that the page overrides Timesheet__c.View action? If so, then Save & New won't call that code because the View page is never shown. Instead, you'll need to use a trigger to accomplish this goal.
This was selected as the best answer
ramya1ramya1

Thanks for the Reply..

 

I Even used the Following Trigger..

 

 

 trigger is working only on the Save Button ,but not on the Save&New.

 

trigger TimesheetForApproval on Timesheet__c(after update, after insert) {
    try{
        for (Timesheet__c t : trigger.new) {
            if (t.Approval_Status__c == '') {
                
                // create the new approval request to submit
                Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
                req.setComments('Submitted for approval. Please approve.');
                req.setObjectId(t.Id);
                system.debug('!!!!!!!!!!!!'+req);
                // submit the approval request for processing
                Approval.ProcessResult result = Approval.process(req);
                
                // display if the reqeust was successful
                System.debug('Submitted for approval successfully: '+result.isSuccess());
            }
        }
    }catch(System.DMLException e) { }
sfdcfoxsfdcfox
I'll look at it and get back to you.
ramya1ramya1

Trigger Worked !!!!!!!

 

Thanks for the Reply.