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
Vimal DVimal D 

user should not click submit for approval button twice

Hi,

Users are receiving an error while tried to click "submit for Approval button in Visualforce page. I am not able to redirect the below error into a custom message. could you please help. Below is the submit for approval code.

Process failed. First exception on row 0; first error: ALREADY_IN_PROCESS, Cannot submit object already in process.: []
Error is in expression '{!submit_for_approval}' in component <apex:commandButton> in page customagreementedit: Class.Agreement_Edit_AC.submit_for_approval: line 72, column 1

An unexpected error has occurred. Your development organization has been notified.


submit for approval code
public PageReference submit_for_approval() {
        Id id = ApexPages.currentPage().getParameters().get('id');    
        Approval.ProcessSubmitRequest req1=new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval.');
        //req1.setNextApproverIds(new Id[]{UserInfo.getUserId()});
        req1.setObjectId(id);
        Approval.ProcessResult result = Approval.process(req1);
        if(result.isSuccess())
        {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Approval flow submitted successfully'));
        }
        else {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'SubmissionFailed as the contract not in Submitted Status'));
        }
        return null;
    }
Best Answer chosen by Vimal D
AshlekhAshlekh
Hi,

If the record is already is submited for approval than this kind of problem occur.

You can use try and catch block to handle this 

public PageReference submit_for_approval() {
        Id id = ApexPages.currentPage().getParameters().get('id');    
        Approval.ProcessSubmitRequest req1=new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval.');
        //req1.setNextApproverIds(new Id[]{UserInfo.getUserId()});
        req1.setObjectId(id);
		try{
			Approval.ProcessResult result = Approval.process(req1);
			if(result.isSuccess())
			{
				ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Approval flow submitted successfully'));
			}
			else {
				ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'SubmissionFailed as the contract not in Submitted Status'));
			}
		catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.info,e));
		}
        return null;
    }

And you can also get the status of record that is it already in process or not ? Below code helps you to get the status of currrent record 

public PageReference submit_for_approval() {
        Id id = ApexPages.currentPage().getParameters().get('id');    
        Approval.ProcessSubmitRequest req1=new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval.');
        //req1.setNextApproverIds(new Id[]{UserInfo.getUserId()});
        req1.setObjectId(id);
		try
		{
			if(CheckRecordforApproval(id))
			{
				Approval.ProcessResult result = Approval.process(req1);
				if(result.isSuccess())
				{
					ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Approval flow submitted successfully'));
				}
				else {
					ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'SubmissionFailed as the contract not in Submitted Status'));
				}	
			}
			else
			{
				ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.info,'This reocrd is in process'));
			}
		}catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.info,e));
		}
        return null;
    }
	
	public boolean CheckRecordforApproval(String recordId)
	{
		Boolean isSubmitted = false;
		if(recordId != null && recordId instanceof ID)
		{
			for(ProcessInstance p : [SELECT Id,status FROM ProcessInstance where TargetObjectId =:recordId])
			{
				if( !p.Status.equalsIgnoreCase('Approved') && !isSubmitted){
					isSubmitted = true;
				}
			}
		}
		return isSubmitted;
	}

IF it helps you than please mark it as a solution and ENJOY APEX

All Answers

nitesh gadkarinitesh gadkari
Hi Vimal,
is the bold value below correctly written?I think it should be Agreement_Edit__c.Tell me if it is correct.
Class.Agreement_Edit_AC.submit_for_approval: line 72, column 1

Regards 
Nitesh
Vimal DVimal D
Hi Nitesh,

Yes it is the apex class name and it is correct. 
AshlekhAshlekh
Hi,

If the record is already is submited for approval than this kind of problem occur.

You can use try and catch block to handle this 

public PageReference submit_for_approval() {
        Id id = ApexPages.currentPage().getParameters().get('id');    
        Approval.ProcessSubmitRequest req1=new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval.');
        //req1.setNextApproverIds(new Id[]{UserInfo.getUserId()});
        req1.setObjectId(id);
		try{
			Approval.ProcessResult result = Approval.process(req1);
			if(result.isSuccess())
			{
				ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Approval flow submitted successfully'));
			}
			else {
				ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'SubmissionFailed as the contract not in Submitted Status'));
			}
		catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.info,e));
		}
        return null;
    }

And you can also get the status of record that is it already in process or not ? Below code helps you to get the status of currrent record 

public PageReference submit_for_approval() {
        Id id = ApexPages.currentPage().getParameters().get('id');    
        Approval.ProcessSubmitRequest req1=new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval.');
        //req1.setNextApproverIds(new Id[]{UserInfo.getUserId()});
        req1.setObjectId(id);
		try
		{
			if(CheckRecordforApproval(id))
			{
				Approval.ProcessResult result = Approval.process(req1);
				if(result.isSuccess())
				{
					ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Approval flow submitted successfully'));
				}
				else {
					ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'SubmissionFailed as the contract not in Submitted Status'));
				}	
			}
			else
			{
				ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.info,'This reocrd is in process'));
			}
		}catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.info,e));
		}
        return null;
    }
	
	public boolean CheckRecordforApproval(String recordId)
	{
		Boolean isSubmitted = false;
		if(recordId != null && recordId instanceof ID)
		{
			for(ProcessInstance p : [SELECT Id,status FROM ProcessInstance where TargetObjectId =:recordId])
			{
				if( !p.Status.equalsIgnoreCase('Approved') && !isSubmitted){
					isSubmitted = true;
				}
			}
		}
		return isSubmitted;
	}

IF it helps you than please mark it as a solution and ENJOY APEX

This was selected as the best answer
Vimal DVimal D
Thank You ashlek.