如何通过 Salesforce 更新 NetSuite?
How to update NetSuite through Salesforce?
您将如何通过 Salesforce 更新 NetSuite。
我知道您会使用 NetSuite 的 RESTlet 和 Salesforce Apex 代码来连接两者,但您实际上是如何一步一步地做到这一点的?
要将数据从 Salesforce 发送到 NetSuite(特别是 customer/account 数据),您需要在两者中进行一些初步设置。
在 NetSuite 中:
创建一个至少具有 get 和 post 的 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>自定义>脚本>脚本>新建。
Select 您保存的新文件并创建脚本记录。您在 NetSuite 中的脚本记录应该在脚本下检查 GET 和 POST。
接下来,单击部署脚本并选择谁将调用此脚本,特别是将在 salesforce 端登录到 NetSuite 的用户。
在部署页面上,您将需要外部 URL 它类似于:
https://1234567.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=123&deploy=1
注意:如果您正在更新的任何数据对流程至关重要,我强烈建议您先在沙盒中创建它以进行测试,然后再转移到生产环境。
在 Salesforce 沙盒中:
点击你的名字>开发者控制台
在开发人员控制台中单击文件>新建并创建一个 Apex Class:
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帐号]以及header您的登录电子邮件和密码对于正在部署 NS 脚本的人来说,body 将在调用此 class.
的触发器中设置
接下来创建将调用此 class 的触发器。每次我在 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 的 body。
此外,您必须在 salesforce(沙盒和生产)的远程站点设置中为 NetSuite 创建授权端点。转到设置,并快速找到将受到安全控制的远程站点设置。
创建一个新的远程站点,将其远程站点 URL 设置为外部 url 的前半部分:https://1234567.restlets.api.netsuite.com
.
从这里开始,在沙盒中进行一些测试。
如果一切顺利,部署 class 并触发销售人员生产。
您将如何通过 Salesforce 更新 NetSuite。 我知道您会使用 NetSuite 的 RESTlet 和 Salesforce Apex 代码来连接两者,但您实际上是如何一步一步地做到这一点的?
要将数据从 Salesforce 发送到 NetSuite(特别是 customer/account 数据),您需要在两者中进行一些初步设置。
在 NetSuite 中:
创建一个至少具有 get 和 post 的 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>自定义>脚本>脚本>新建。
Select 您保存的新文件并创建脚本记录。您在 NetSuite 中的脚本记录应该在脚本下检查 GET 和 POST。
接下来,单击部署脚本并选择谁将调用此脚本,特别是将在 salesforce 端登录到 NetSuite 的用户。
在部署页面上,您将需要外部 URL 它类似于:
https://1234567.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=123&deploy=1
注意:如果您正在更新的任何数据对流程至关重要,我强烈建议您先在沙盒中创建它以进行测试,然后再转移到生产环境。
在 Salesforce 沙盒中:
点击你的名字>开发者控制台 在开发人员控制台中单击文件>新建并创建一个 Apex Class:
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帐号]以及header您的登录电子邮件和密码对于正在部署 NS 脚本的人来说,body 将在调用此 class.
的触发器中设置接下来创建将调用此 class 的触发器。每次我在 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 的 body。
此外,您必须在 salesforce(沙盒和生产)的远程站点设置中为 NetSuite 创建授权端点。转到设置,并快速找到将受到安全控制的远程站点设置。
创建一个新的远程站点,将其远程站点 URL 设置为外部 url 的前半部分:https://1234567.restlets.api.netsuite.com
.
从这里开始,在沙盒中进行一些测试。 如果一切顺利,部署 class 并触发销售人员生产。