salesforce的按钮执行js代码如何链接到apex代码

在Salesforce中,你可以通过自定义JavaScript按钮或链接来触发Apex代码的执行。这可以通过使用JavaScript Remoting或Visualforce页面来实现。以下是一些步骤来将JavaScript按钮与Apex代码链接起来:

使用JavaScript Remoting链接JavaScript按钮到Apex代码:

  1. 创建Apex类 :首先,创建一个Apex类,其中包含你要执行的Apex代码。这个Apex类需要使用@RemoteAction注解来标记方法,以便从JavaScript中调用它。
apex 复制代码
public class MyApexController {
    @RemoteAction
    public static String executeApexCode() {
        // 在这里编写你的Apex代码
        // 返回任何结果
        return 'Apex code executed successfully';
    }
}
  1. 创建JavaScript按钮:在对象的设置中,创建一个自定义JavaScript按钮。在按钮的JavaScript代码中,使用JavaScript Remoting来调用Apex代码。下面是一个示例按钮的代码:
javascript 复制代码
{!REQUIRESCRIPT("/soap/ajax/41.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/41.0/apex.js")}

var result = sforce.apex.execute("MyApexController", "executeApexCode", {});

// 处理Apex代码执行的结果
alert(result);
  1. 关联按钮:将这个JavaScript按钮与你的对象相关联,以便在记录详情页上显示它。

使用Visualforce页面链接JavaScript按钮到Apex代码:

  1. 创建Visualforce页面 :创建一个Visualforce页面,其中包含你的JavaScript按钮。在Visualforce页面中,你可以使用<apex:page>标签的standardController属性指定对象,然后在页面中包含JavaScript按钮。
html 复制代码
<apex:page standardController="Your_Object__c">
    <script>
        function executeApexCode() {
            Visualforce.remoting.Manager.invokeAction(
                '{!$RemoteAction.MyApexController.executeApexCode}',
                function(result, event) {
                    if (event.status) {
                        // 处理Apex代码执行的结果
                        alert(result);
                    } else {
                        // 处理错误
                        alert('Error: ' + event.message);
                    }
                }
            );
        }
    </script>
    <button onclick="executeApexCode();">Execute Apex Code</button>
</apex:page>
  1. 关联Visualforce页面:在对象的设置中,创建一个新的JavaScript按钮或链接,然后将其链接到上面创建的Visualforce页面。

这些步骤将使你能够通过JavaScript按钮或链接执行Apex代码。确保在Apex代码中包含所需的逻辑,并根据需要处理结果。同时,确保在按钮或链接的可见性和访问控制方面进行适当的设置,以确保只有授权的用户可以执行Apex代码。

Calling a Lightning component from a Classic custom button involves a slightly different approach than doing it in Lightning Experience. Here's how you can achieve this:

  1. Create a Visualforce Page:

    To bridge the gap between Classic and Lightning components, you can create a Visualforce page that contains your Lightning component.

    • Go to "Setup."
    • In the Quick Find box, enter "Visualforce Pages" and select it.
    • Click "New" to create a new Visualforce page.

    Here's an example of what the Visualforce page might look like:

    html 复制代码
    <apex:page >
        <apex:includeLightning />
        <div id="lightning-component-container"></div>
        <script>
            $Lightning.use("c:YourApp", function() {
                $Lightning.createComponent(
                    "c:YourLightningComponent",
                    {},
                    "lightning-component-container",
                    function(component) {}
                );
            });
        </script>
    </apex:page>
    • In this example, replace "c:YourApp" with the name of your Lightning app (if applicable) and "c:YourLightningComponent" with the name of your Lightning component.
  2. Create a Custom Button in Classic:

    • Navigate to the object (e.g., Account, Contact) for which you want to add the custom button.
    • In the object's page, go to the "Buttons, Links, and Actions" section and click "New Button or Link."
  3. Choose Button Behavior:

    • Select "Visualforce Page" as the content source.
    • Choose the Visualforce page you created in step 1 from the dropdown list.
  4. Configure Button Properties:

    • Give your button a name.
    • Optionally, specify a label for the custom button that will appear on the object's page layout.
    • Configure other properties as needed, such as behavior and display options.
  5. Add the Button to Page Layout:

    • Save the custom button.
    • After saving, add the button to the desired page layout. You can do this by editing the page layout for the object and dragging the button from the "Mobile & Lightning Actions" section to the "Salesforce Classic Actions" section.
  6. Test the Custom Button:

    • Save the page layout.
    • Go to the record detail page for the object you modified in Classic.
    • You should see your custom button (with the label you specified) on the page.
    • Click the button to execute the Visualforce page, which in turn will load your Lightning component.

This approach leverages a Visualforce page as a bridge between Classic and Lightning components. When the custom button is clicked, it loads the Visualforce page, which then embeds and initializes your Lightning component.

相关推荐
熊的猫31 分钟前
JS 中的类型 & 类型判断 & 类型转换
前端·javascript·vue.js·chrome·react.js·前端框架·node.js
别拿曾经看以后~2 小时前
【el-form】记一例好用的el-input输入框回车调接口和el-button按钮防重点击
javascript·vue.js·elementui
川石课堂软件测试2 小时前
性能测试|docker容器下搭建JMeter+Grafana+Influxdb监控可视化平台
运维·javascript·深度学习·jmeter·docker·容器·grafana
JerryXZR2 小时前
前端开发中ES6的技术细节二
前端·javascript·es6
problc3 小时前
Flutter中文字体设置指南:打造个性化的应用体验
android·javascript·flutter
Gavin_9153 小时前
【JavaScript】模块化开发
前端·javascript·vue.js
懒大王爱吃狼4 小时前
Python教程:python枚举类定义和使用
开发语言·前端·javascript·python·python基础·python编程·python书籍
待磨的钝刨5 小时前
【格式化查看JSON文件】coco的json文件内容都在一行如何按照json格式查看
开发语言·javascript·json
Channing Lewis9 小时前
salesforce case可以新建一个roll up 字段,统计出这个case下的email数量吗
数据库·salesforce
前端青山10 小时前
Node.js-增强 API 安全性和性能优化
开发语言·前端·javascript·性能优化·前端框架·node.js