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
Suman KunduSuman Kundu 

System.LimitException: DML currently not allowed - from Approval.process(req)

public ReversePaymentController(ApexPages.StandardController con) 
    {
        this.controller = con;
        shouldRenderPageBlock = true;
        UserRole r = [select Id from UserRole where Name = 'Customer Service Managers'];
        if(r != null)
        {
            System.debug('::::::::::'+r.Id);
            o2bc__Subscription__c subs = [select Id, o2bc__Account__c, o2bc__Invoice__c, Credit_Card__c from o2bc__Subscription__c where Id = :controller.getId()];
            if(subs.o2bc__Invoice__c != null)
            {
                List<o2bc__Payment__c> pList = [select Id, Name, Mes_Transaction_ID__c , o2bc__Payment_Amount__c, o2bc__Invoice__r.Name, o2bc__Invoice_Line__c, o2bc__Invoice_Line__r.Name, o2bc__Invoice_Line__r.o2bc__Unit_Rate__c, o2bc__Invoice_Line__r.o2bc__Subscription__c, o2bc__Account__c, o2bc__Account__r.Name, o2bc__Invoice__c, o2bc__Payment_Method__c, o2bc__Has_Processed__c, o2bc__Refund_Amount__c, o2bc__Status__c from o2bc__Payment__c where o2bc__Invoice__c = :subs.o2bc__Invoice__c and o2bc__Payment_Method__c = 'Credit Card'];
                if(pList != null && pList.size() > 0)
                {
                    for(o2bc__Payment__c pay1 : pList)
                    {
                        if(String.valueOf(pay1.o2bc__Status__c).equals('Processed') && pay1.o2bc__Has_Processed__c)
                        {
                            pay = pay1;
                            break;
                        }    
                    }
                    if(pay != null)
                    {
                        payamt = pay.o2bc__Payment_Amount__c;
                        payMethod = pay.o2bc__Payment_Method__c;
                        hasProcessed = pay.o2bc__Has_Processed__c; 
                        refAmount = String.valueOf(payamt);   
                        note = '';
                        reason = '';    
                        if(UserInfo.getUserRoleId() != r.Id)
                        {
                            // create approval
                            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
                            req.setComments('Submitted for reversing payment.');
                            req.setObjectId(pay.Id);
                            Approval.ProcessResult result = Approval.process(req); //throwing error line:52
                            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'CSR Manager is only granted to do this process.'));
                            shouldRenderPageBlock = false;
                        }
                    }
                    else
                    {
                        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'Reverse Payment is only allowed on processed payments.'));
                        shouldRenderPageBlock = false;
                    }
                }
                else
                {         
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'There is no payment processed by Credit Card.'));
                    shouldRenderPageBlock = false;
                } 
            }
            else
            {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL,'There is no invoice in this subscription.'));
                shouldRenderPageBlock = false;
            }  
        }
        else
        {
            
        }
    } 
Here from 52nd line where process method has been called, throws this error.
I have used a component where I have also used allowDML="true", but still it throws error.
Please help me.
Best Answer chosen by Admin (Salesforce Developers) 
Suman KunduSuman Kundu

Yes it is working from page action method.

All Answers

Damien_Damien_

You cannot have a DML statement run in your constructor.

Suman KunduSuman Kundu

Thank you very much. I didn't know it.

So in this case, should I call Approval.process method from page.action?

Suman KunduSuman Kundu

Yes it is working from page action method.

This was selected as the best answer