APL函数代码开放场景-按钮Java接口说明

2024-12-21 09:52:17 423 本站

前验证

package fx.custom.apl.example.button;
import com.alibaba.fastjson.JSON;
import com.fxiaoke.functions.FunctionContext;
import com.fxiaoke.functions.Fx;
import com.fxiaoke.functions.client.DebugHelper;
import com.fxiaoke.functions.model.ButtonValidateResult;
import com.fxiaoke.functions.template.IButtonBeforeAction;
import com.fxiaoke.functions.utils.Maps;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
/**
* 1. 根据命名空间以及返回,选择对应的Action
* 2. 这个Action是函数触发入口,对象勾子触发时,会到Jar包中查找这个类并且检查接口以及接口提供的方法
* <p>
* 场景:如新建编辑对象时,添加前验证校验数据,或者对部分数据做一些初始化功能
*/
public class ButtonBeforeExample implements IButtonBeforeAction {
   @Override
   public ButtonValidateResult validate(FunctionContext context, Map<String, Object> map) {
       String name = (String) context.getData().get("name");
       context.getData().put("name", name + "test");
       if (Objects.equals("1", context.getData().get("field_Tnvig__c"))) {
           return ButtonValidateResult.builder().error(true).errorMessage("类型错误").block(true).build();
       }
       return ButtonValidateResult.builder().error(false).errorMessage("成功").build();
   }

   /**
    * 调试入口
    */
   public static void main(String[] args) throws IOException {
       //调试器
       DebugHelper helper = new DebugHelper();
       //调试器初始化,包括身份以及服务器的地址
       //身份信息联系APL平台获取,具体查看application.properties配置
       helper.init();

       //构造当前执行类
       ButtonBeforeExample example = new ButtonBeforeExample();
       //模拟调试的上下文,例如开发时想模拟一个客户对象的上下文,以方便开发
       FunctionContext context = helper.context("object_zBB6O__c", "63fd7a30ffd89f00013c7be3");

       //构造被触发时的参数
       Map<String, Object> argument = Maps.newHashMap();

       //执行函数
       ButtonValidateResult validate = example.validate(context, argument);
       Fx.log.info(JSON.toJSONString(validate));
   }}

执行

package fx.custom.apl.example.button;
import com.fxiaoke.functions.FunctionContext;
import com.fxiaoke.functions.Fx;
import com.fxiaoke.functions.client.DebugHelper;
import com.fxiaoke.functions.template.IButtonBusinessAction;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 点击按钮后页面可以跳转到该返回值对应的URL
*
* 场景:点击按钮后,弹出提示框,提示设定的提示信息
*/
public class ButtonBusinessExample implements IButtonBusinessAction {
   /**
    * 业务按钮函数的运行方法
    */
   @Override
   public String onClick(FunctionContext context, Map<String, Object> args) {
       //做一些业务相关的事情
       return "这是我业务的提示信息";
   }


   public static void main(String[] args) throws IOException {
       DebugHelper helper = new DebugHelper();
       helper.init();
       //模拟调试的上下文,例如开发时想模拟一个客户对象的上下文,以方便开发
       FunctionContext context = helper.context("object_zBB6O__c", "63fd7a30ffd89f00013c7be3");
       Map<String, Object> map = new HashMap<>();
       String s = new ButtonBusinessExample().onClick(context, map);
       Fx.log.info(s);
   }}

后动作

package fx.custom.apl.example.button;
import com.fxiaoke.functions.FunctionContext;
import com.fxiaoke.functions.client.DebugHelper;
import com.fxiaoke.functions.template.IButtonAfterAction;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 后置处理
*/
public class ButtonAfterExample implements IButtonAfterAction {

   @Override
   public void after(FunctionContext context, Map<String, Object> args) {
       //do something
   }


   public static void main(String[] args) throws IOException {
       DebugHelper helper = new DebugHelper();
       helper.init();
       //模拟调试的上下文,例如开发时想模拟一个客户对象的上下文,以方便开发
       FunctionContext context = helper.context("object_zBB6O__c", "63fd7a30ffd89f00013c7be3");
       Map<String, Object> map = new HashMap<>();
       new ButtonAfterExample().after(context, map);
   }}



部分内容来源于互联网,如有侵权,请联系客服删除处理。
在线咨询 拨打电话