如何通过Salesforce更新NetSuite?

如何通过Salesforce更新NetSuite?,salesforce,apex,suitescript2.0,Salesforce,Apex,Suitescript2.0,您将如何通过Salesforce更新NetSuite。 我知道您会使用NetSuite的RESTlet和Salesforce Apex代码来连接这两个应用程序,但您实际上是如何一步一步地完成这一工作的?要将数据从Salesforce发送到NetSuite(特别是客户/帐户数据),您需要在这两个应用程序中进行一些初步设置 在NetSuite中: 创建一个RESTlet脚本,它至少有一个get和post。 例如,我会在桌面上创建一个javascript文件,其中包含: /** *@NApiVers

您将如何通过Salesforce更新NetSuite。
我知道您会使用NetSuite的RESTlet和Salesforce Apex代码来连接这两个应用程序,但您实际上是如何一步一步地完成这一工作的?

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

在NetSuite中:

创建一个RESTlet脚本,它至少有一个get和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检查

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

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

注意:如果您正在更新的任何数据对于流程来说都是至关重要的,我强烈建议您首先在沙箱中创建这些数据进行测试,然后再进行生产

在Salesforce沙盒中:

单击YourName>Developer控制台 在开发人员控制台中,单击文件>新建并创建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_帐户=[您的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
    }
}
在此脚本中,数据是发送给NetSuite的主体

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

从这里开始,在沙箱中进行一些测试。 如果一切正常,则将类和触发器部署到salesforce生产