Call an Apex Class Method Using a Button | Techila
855
single,single-post,postid-855,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

Call an Apex Class Method Using a Button

Call an Apex Class Method Using a Button

This blog shows you how to create a custom button on the Invoice detail page that calls the method for the current invoice.

By default, every detail page includes several standard buttons, including Edit, Delete, and Clone. You can also create custom buttons and add them to page layouts as needed. Use the following steps to create a custom button for the Invoice detail page.

1.       From Setup in your Developer Edition organization, enter Objects in the Quick Find box, select Objects, and then clickInvoice.

2.       Scroll down to the Buttons, Links, and Actions section and click New Button or Link.

3.       In the Label field, enter Renumber Line Items.

4.       For Display Type choose Detail Page Button.

5.       For Behavior choose Execute JavaScript.

6.       For Content Source choose OnClick JavaScript.

7.       Notice that you are creating a Detail Page button that executes some JavaScript.

1

{!REQUIRESCRIPT(“/soap/ajax/29.0/connection.js”)}

2

{!REQUIRESCRIPT(“/soap/ajax/29.0/apex.js”)}

8.

3

var result = sforce.apex.execute(“InvoiceUtilities”,”renumberLineItems”,{invoiceName:”{!

4

Invoice__c.Name}”});

9.

5

alert(result);

6

window.location.reload();

10.   Make sure your form matches the following screen, and then click Save.

11.   When prompted, click OK. We’ll add this button to a page layout in the next step.

Description: custom button details before save

Step 2: Add the Button to the Page Layout

Next up is to add the new button to the Invoice detail page layout. (Page layout modification is something that this tutorial assumes you already understand, so this step is brief.) To add the custom button to the Invoice page layout:

1.       From the Invoice custom object detail page, scroll down to the Page Layouts.

2.       Click Edit next to the Invoice Layout.

3.       In the palette at the top, click Buttons.

4.       Drag the Renumber Line Items button to the Custom Buttons section of the page layout, then click Save.

Description: save the file

Step 3: Modify the Apex Class

The JavaScript in the custom button leverages the AJAX Toolkit to make SOAP calls from JavaScript. Considering this, there are two minor changes that you need to make to the Apex class and method so that it supports SOAP API calls.

To modify the Apex class without leaving the browser:

1.       From Setup, enter “Apex Classes” in the Quick Find box, then select Apex Classes.

2.       Notice next to the InvoiceUtilities class there are three links: Edit, Del, and Security. Click Edit.

3.       Modify the scope for both the class and the method from public to global (line 1) and webservice (line 4), then clickSave.

Your final code should be as follows:

global with sharing class InvoiceUtilities {

   // Class method to renumber Line Items for a given Invoice number.

   // Returns a string that indicates success or failure.

   webservice static String renumberLineItems(String invoiceName) {

      // Create a copy of the target Invoice object and its Line Items.

      Invoice__c invoice =

         [SELECT i.Name, (SELECT Name FROM Line_Items__r ORDER BY Name)

          FROM Invoice__c i

          WHERE i.Name = :invoiceName LIMIT 1];

      // Loop through each Line Item, renumbering as you go.

      Integer i = 1;

      for (Line_Item__c item : invoice.Line_Items__r) {

         item.Name = String.valueOf(i);

         System.debug(item.Name);

         i++;

      }

      // Update the Line Items in one transaction, rollback if any problems

      // and return error messages to the calling environment.

      try {

         Database.update(invoice.Line_Items__r);

      }

      catch (DmlException e) {

         return e.getMessage();

      }

      // On success, return a message to the calling program.

      return ‘Line items renumbered successfully.’;

  }

}

When you return to the list of Apex classes, notice that there’s a new link for the InvoiceUtilities class: WSDL. If you click it, you see a WSDL file that apps can use to interface with the class.

Courtesy: https://developer.salesforce.com/docs/atlas.en-us.workbook.meta/workbook/button_intro.htm

“Explore – Techila Global Services, A Salesforce development company”

Author: techila

No Comments

Post A Comment