---
schema_version: "1"
id: 40c2cc8ba0f0933150c6b8c80e39f24b08d5ddc9645892f606b9cde604de187f
title: Define Action Behavior with Apex Controller Class and Methods
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/analytics/264-0-0/bi_dashboards_configure_bulk_actions_example_apex_class.md
canonical_url: https://help.salesforce.com/s/articleView?id=analytics.bi_dashboards_configure_bulk_actions_example_apex_class.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: af1dafdfa5309eb6ab2cc2cc325a60d5aa0a589c78251a6e970985f31f13c48f
description: Define an Apex controller class and methods that generate a list of accounts shown in the table widget, create an opportunity for each account, and return the progress of each task.
products:
  - CRM Analytics
  - Cross Cloud
release: "264"
---
# Define Action Behavior with Apex Controller Class and Methods

Define an Apex controller class and methods that generate a list of accounts shown in the table widget, create an opportunity for each account, and return the progress of each task.

1. From setup, enter Apex Classes in the Quick Find box.
2. Select **Apex Classes**.
3. Click **New**.
    
    ![You can create new classes from the Apex Classes page.](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-analytics-bi-264-0-0-production-enus/ec791473-f5b4-4f64-a656-9104f455d966/bi/images/bi/bi_dashboards_apex_classes_new.png)
4. Add the following code.
    
    ```
    public class CreateOpportunitiesController {
        public string query{get; set;}
     
    /* To determine the records to perform the bulk action on, extract the SAQL query */
        public PageReference init() {
            query = ApexPages.currentPage().getParameters().get('query');
            return null;
        }
    
    /* Takes the account records from the SAQL query, creates an opportunity for each account, and then returns a map between account ID and new opportunity name. Note: Account.Name and AccountId referenced below refer to the dataset field names. Update them to match your dataset fields. */
        @RemoteAction
        public static Map<String, String> create(List <Map<String, String>> accountRecords) {
            Map<String, String> result = new Map<String, String>();
    
    
            List<Opportunity> opps = new List<Opportunity>();
            for (Map<String, String> accountRecord : accountRecords) {
            
                String name = accountRecord.get('Account.Name') + ' - Sprint Review - 12/2';
                String accountId = accountRecord.get('AccountId');
                result.put(accountId, name);
                Opportunity opp = new Opportunity(
    /* You can set different fields from the Opportunity object than those listed below, like Amount. */
                    Name = name,
                    Type ='New Customer',
                    AccountId = Id.valueOf(accountId),
                    CloseDate=Date.valueOf('2016-12-31'),
                    StageName='Prospecting'); 
                opps.add(opp);
            }
            insert opps;
            return result;
        }
    }
    ```
5. Click **Save**.