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
nishant shrivastava 40nishant shrivastava 40 

I need to call Custom Button from Apex Class

Hi,
I have a requirement where i want to call Custom button from Apex class. kindly suggest. 
I shall be grateful to you for your kind cooperation.
Thanks and regards
PriyaPriya (Salesforce Developers) 
Hi Nishant,

Can you please elaborate your requirement here?
Suraj Tripathi 47Suraj Tripathi 47
Hi nishant,
VisualForce page
<apex:page standardController="Case" extensions="EscalCase" action="{!caseEscalation}">
<apex:form>
<apex:inputHidden value="{!case.OwnerId}"/>
</apex:form>
</apex:page>
public class EscalCase {
    //Apex properties or variables

    public Id owner {get; set;}
    public Id Id { get; set; }
    public Case cas { get; set; }

    //constructor to get the Case record
    public EscalCase(ApexPages.StandardController controller) {
    cas =  (Case) controller.getRecord();
    Id = cas.Id;
    System.debug('The case record: ' + cas);
    owner = cas.OwnerId;
    }

    //Method that can is called from the Visual Force page action attribute
    public PageReference caseEscalation() {
       System.debug('Case Owner: ' + owner);
        System.debug('Case Id: ' + Id);
        //build your code logic here
       PageReference pageRef = new PageReference('/'+Id);
        pageRef.setRedirect(true);
       return pageRef; //Returns to the case page
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 


 
nishant shrivastava 40nishant shrivastava 40

Hi Guys, thanks for your kind responses. The requirement is:

i have created a custom button to call another functionality. I want to call this button from apex class so that i can use it in Trigger. kindly suggest :)

PriyaPriya (Salesforce Developers) 
Hi Nilesh,

Why you need to call the custom button instead you can directly call its method from Apex class.
nishant shrivastava 40nishant shrivastava 40
Hi Priya, i already think about workaround. There is only one way to call the button from Apex Class. how can i refer the button in apex class and can call it. kindly suggest :)
mukesh guptamukesh gupta
Hi Nilesh,

Please check the below sample code.
 
window.parent.location='/apex/rejectChangeDealer?Id={!Change__c.Id}&retURL=/{!Change__c.Id}';

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
nishant shrivastava 40nishant shrivastava 40
Hi Mukesh, i don't need to call url. i am calling functionality, that's why i need custom button reference only in apex class. Please Suggest :)
mukesh guptamukesh gupta
Hi Nishant,

Where this button will display like visualforce page/Lightning component?
nishant shrivastava 40nishant shrivastava 40
That button is a custom button i have created in opp. i want to refer this button in my apex class.
Omar Rajab 94Omar Rajab 94

Hi Nishant,


you should translate the functionallity of this button to apex code, then you can call this button "function" in the apex class!

Which functionallity do this bitton?

regards,
Omar

nishant shrivastava 40nishant shrivastava 40
Hi Omar, i need screen flow functionality, that can't be called directly, therefore, i need to call button from apex.
Omar Rajab 94Omar Rajab 94

I still did not understand you. 

what will happen if the user click on the Button?