繁体   English   中英

如何通过 Salesforce 更新 NetSuite?

[英]How to update NetSuite through Salesforce?

您将如何通过 Salesforce 更新 NetSuite。 我知道您会使用 NetSuite 的 RESTlet 和 Salesforce Apex 代码将两者连接起来,但是您将如何在分步过程中实际执行此操作?

要将数据从 Salesforce 发送到 NetSuite(特别是客户/帐户数据),您需要在两者中进行一些初步设置。

在 NetSuite 中:

创建一个至少具有获取和发布功能的 RESTlet 脚本。 例如,我会在我的桌面上创建一个 javascript 文件,其中包含:

/**
 *@NApiVersion 2.x
 *@NScriptType restlet
 */

//Use: Update NS customer with data (context) that is passed from SF

define(['N/record'], function(record) //use the record module
{
    function postData(context)
    {
        //load the customer I'm gonna update
        var cust = record.load({type:context.recordtype, id:context.id});
        log.debug("postData","loaded the customer with NSID: " + context.id);

        //set some body fields
        cust.setValue("companyname", context.name);
        cust.setValue("entityid", context.name + " (US LLC)");
        cust.setValue("custentity12", context.formerName);
        cust.setValue("phone",context.phone);
        cust.setValue("fax",context.fax);

        //remove all addresses
        while(cust.getLineCount('addressbook') != 0)
            cust.removeLine('addressbook',0);

        //add default billing address
        cust.insertLine('addressbook',0);
        cust.setSublistValue('addressbook','defaultbilling',0,true);
        cust.setSublistValue('addressbook','label',0,'BILL_TO');
        var billingAddress=cust.getSublistSubrecord('addressbook','addressbookaddress',0);
        billingAddress.setValue('country',context.billingCountry);
        billingAddress.setValue('addr1', context.billingStreet);
        billingAddress.setValue('city',context.billingCity);
        billingAddress.setValue('state',context.billingState);
        billingAddress.setValue('zip',context.billingZip);

        //add default shipping address
        cust.insertLine('addressbook',0);
        cust.setSublistValue('addressbook','defaultshipping',0,true);
        cust.setSublistValue('addressbook','label',0,'SHIP_TO');
        var shippingAddress=cust.getSublistSubrecord('addressbook','addressbookaddress',0);
        shippingAddress.setValue('country',context.shippingCountry);
        shippingAddress.setValue('addr1',context.shippingStreet);
        shippingAddress.setValue('city',context.shippingCity);
        shippingAddress.setValue('state',context.shippingState);
        shippingAddress.setValue('zip',context.shippingZip);

        //save the record
        var NSID = cust.save();
        log.debug("postData","saved the record with NSID: " + NSID);
        return NSID; //success return the ID to SF
    }

    //get and post both required, otherwise it doesn't work
    return {
      get : function (){return "get works";},
      post : postData //this is where the sauce happens
    };
});

保存此文件后,转到 NetSuite>Customization>Scripting>Scripts>New。

选择您保存的新文件并创建脚本记录。 您在 NetSuite 中的脚本记录应在脚本下检查 GET 和 POST。

接下来,单击部署脚本并选择谁将调用此脚本,特别是将在 salesforce 端登录到 NetSuite 的用户。

在部署页面上,您将需要外部 URL,它类似于: https://1234567.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=123&deploy=1 : https://1234567.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=123&deploy=1

注意:如果您要更新的任何数据对流程至关重要,我强烈建议先在沙箱中创建此数据以进行测试,然后再进入生产环境。

在 Salesforce 沙箱中:

单击 YourName>Developer Console 在开发者控制台中单击 File>New 并创建一个 Apex 类:

global class NetSuiteWebServiceCallout
{
    @future (callout=true) //allow restlet callouts to run asynchronously
    public static void UpdateNSCustomer(String body)
    {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndPoint('https://1234567.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=123&deploy=1'); //external URL
        request.setMethod('POST');
        request.setHeader('Authorization', 'NLAuth nlauth_account=1234567, nlauth_email=login@login.com, nlauth_signature=password'); //login to netsuite, this person must be included in the NS restlet deployment
        request.setHeader('Content-Type','application/json');
        request.setBody(body);
        HttpResponse response = http.send(request);
        System.debug(response);
        System.debug(response.getBody());
    }
}

您必须将此处的端点设置为外部 url,授权 nlauth_account=[您的 netsuite 帐号],以及您的登录电子邮件和密码的标头给正在部署 NS 脚本的人,正文将是在调用此类的触发器中设置。

接下来创建将调用此类的触发器。 每次更新 Salesforce 中的帐户时,我都会运行此脚本。

trigger UpdateNSCustomer on Account (after update)
{
    for(Account a: Trigger.new)
    {
        String data = ''; //what to send to NS
        data = data + '{"recordtype":"customer","id":"'+a.Netsuite_Internal_ID__c+'","name":"'+a.Name+'","accountCode":"'+a.AccountCode__c+'",';
        data = data + '"formerName":"'+a.Former_Company_Names__c+'","phone":"'+a.Phone+'","fax":"'+a.Fax+'","billingStreet":"'+a.Billing_Street__c+'",';
        data = data + '"billingCity":"'+a.Billing_City__c+'","billingState":"'+a.Billing_State_Province__c+'","billingZip":"'+a.Billing_Zip_Postal_Code__c+'",';
        data = data + '"billingCountry":"'+a.Billing_Country__c+'","shippingStreet":"'+a.Shipping_Street__c+'","shippingCity":"'+a.Shipping_City__c+'",';
        data = data + '"shippingState":"'+a.Shipping_State_Province__c+'","shippingZip":"'+a.Shipping_Zip_Postal_Code__c+'","shippingCountry":"'+a.Shipping_Country__c+'"}';
        data = data.replaceAll('null','').replaceAll('\n',',').replace('\r','');
        System.debug(data);
        NetSuiteWebServiceCallout.UpdateNSCustomer(data); //call restlet
    }
}

在此脚本中, data是您发送到 NetSuite 的正文。

此外,您必须在 salesforce(沙盒和生产)的远程站点设置中为 NetSuite 创建一个授权端点。 转到设置,并快速找到将受安全控制的远程站点设置。 创建一个新的远程站点,将其远程站点 URL 设置为外部 url 的前半部分: https://1234567.restlets.api.netsuite.com : https://1234567.restlets.api.netsuite.com

从这里开始,在沙箱中进行一些测试。 如果一切正常,则部署该课程并触发 Salesforce 生产。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM