[英]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.