Sweet.apex

  • Docs
  • Tutorials
Docs Menu
  • Sweet.apex Core
    • Transpilation
    • Grammar
    • Command
    • Config
  • Features
    • Action
    • Apex Doc
    • Array Creation
    • Aspect
    • Cast
    • Default Value
    • Enum
    • File
    • Function
    • Identity
    • Injection
    • Lambda
    • Log
    • Mod
    • Not Null
    • Operator
    • Optional
    • Reflection
    • Rethrow
    • Switch
    • Template String
    • Template
    • Script
    • Tagged String
    • Annotation
    • Nullable
    • Var
    • Val
    • Map Access
    • Constructor
    • Transaction
    • Destructure
    • Import Static
    • Pipeline
    • Varargs
    • Patch
    • Import As
  • Plugin Development
    • Feature
    • Test Case

Action Guide

Action

Feature Overview

This feature converts a static method into an Action.

Prerequisite

You need to include Action.apex if you want to enable this feature.

Sweet Apex Example

public class ActionDemo {
    /**
     * Some descriptions
     *
     * @param a The first number
     * @param b The second number
     * */
    @AuraEnabled
    @action
    public static Integer add(Integer a, Integer b) {
        return a + b;
    }
}

Transpiled Apex

public class ActionDemo {
    private static Action.Registry registry = new Action.Registry();

    static {
        registry.action(new AddAction());
    }

    @AuraEnabled
    public static Object invoke(String name, Map args) {
        return registry.invoke(name, args);
    }

    @AuraEnabled
    public static Map apiDescriptorForLightning() {
        return registry.actions;
    }

    /**
     * Some descriptions
     *
     * @param a The first number
     * @param b The second number
     * */
    private class AddAction extends Action {
        public AddAction() {
            super('add');
            param('a', Integer.class, 'The first number');
            param('b', Integer.class, 'The second number');

        }
        public override Object execAction(Object arg0, Object arg1) {
            Integer a = (Integer)arg0;
            Integer b = (Integer)arg1;

            return a + b;
        }
    }
}

Usage

@action can only be used on public/global static methods with @AuraEnabled.

Some variations are:

ExampleDescription
@actionConvert this method to Action
@action(true)Convert this method to Action and set returnRaw(true)
@action(returnRaw=true)Convert this method to Action and set returnRaw(true)

Contribute on Github! Edit this section.