如何通过Salesforce更新NetSuite?

时间:2019-05-28 15:59:30

标签: salesforce apex restlet suitescript2.0

您将如何通过Salesforce更新NetSuite。 我知道您将使用NetSuite的RESTlet和Salesforce Apex代码来连接两者,但是您将如何逐步进行操作呢?

1 个答案:

答案 0 :(得分:0)

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

在NetSuite中:

创建一个RESTlet脚本,该脚本至少具有一个get and post。 例如,我会在桌面上创建一个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>自定义>脚本>脚本>新建。

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

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

在部署页面上,您将需要如下所示的外部URL: https://1234567.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=123&deploy=1

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

在Salesforce沙箱中:

点击YourName>开发者控制台 在开发人员控制台中,单击“文件”>“新建”并创建一个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 = [您的网络套件帐号],并将登录电子邮件的标头和密码发送给正在部署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

从这里,在沙箱中进行一些测试。 如果一切看起来不错,请部署该类并触发Salesforce生产。