您将如何使用NetSuite数据更新Salesforce? 我知道您将需要使用Suitescript 2.0和某种令牌身份验证。
答案 0 :(得分:0)
我有一个执行此操作的suitescript 2.0用户事件脚本,它连接到salesforce,并将netsuite客户字段更新为salesforce帐户。
该脚本将更新salesforce帐户上的三个自定义字段:NSID,同步,同步。
/**
*@NApiVersion 2.x
*@NScriptType UserEventScript
*/
define(['N/https', 'N/search', 'N/record'],
function (https, search, record)
{
_HTTPS = https;
_SEARCH = search;
_RECORD = record;
//handle after submition of customer on create and edit
function afterSubmit(context)
{
var body;
var NSID;
var sfEntityID;
var sync;
var syncTime;
var accountURL;
var response;
var recordData = {};
//If customer is getting deleted reflect that to SF
if(context.type === "delete")
NSID = 'deleted';
else
NSID = context.newRecord.id;
//get the SF account ID
sfEntityID = context.newRecord.getValue({fieldId:"custentity_sf_account_id"});
if (sfEntityID == undefined || sfEntityID == "") //If there is no SFID stop
{
log.debug("afterSubmit","No sfEntityID on customer with NSID: " + NSID);
return;
}
var date = new Date();
var month = date.getUTCMonth() + 1; // jan = 0
if (month < 10)
{
month = "0" + month;
}
syncTime = date.getUTCFullYear() + '-' + month + '-' + date.getUTCDate() + 'T' + date.getUTCHours() + ':' + date.getUTCMinutes() + ':' + date.getUTCSeconds() + '.000Z';
//login to SF (loginSalesforceNLAP controls sandbox/production login)
body = loginSalesforceNLAP();
//check if I got an access token back
if(body.access_token == undefined || body.access_token == "")
{
log.debug('afterSubmit','could not get SF access token');
return;
}
//set fields that will be updated on SF account
recordData.Netsuite_Internal_ID__c = NSID;
recordData.Sync__c = true;
recordData.Synced_Time__c = syncTime;
recordData = JSON.stringify(recordData);
//send update request
response = _HTTPS.post({
url: (getURL(body) + "/sobjects/Account/" + sfEntityID + "?_HttpMethod=PATCH"),
body: recordData,
headers: {"Authorization": "OAuth " + body.access_token,"Content-Type": "application/json"}
});
log.debug("response",response);
//log an error if it occurred
if(response.code != 204)
{
log.debug("afterSubmit","Could not update sf Account: " + sfEntityID + ' NSID: ' + NSID);
return;
}
//success message
log.debug("afterSubmit","successfully updated sf Account: " + sfEntityID + ' NSID: ' + NSID);
return;
}
return {afterSubmit: afterSubmit};
});
//get max SF version for SF update request
function getURL(body)
{
var max
var arr
var header = {"Authorization": "OAuth " + body.access_token };
var recordData = {};
var url = body.instance_url + "/services/data/";
response = _HTTPS.get({
url: url,
body: recordData,
headers: header
});
if(response.code == 200 || response.code == 204)
{
arr = JSON.parse(response.body)
for(var i = 0; i < arr.length; i++)
{
//find max version
if(!max || parseInt(arr[i]["version"]) > parseInt(max["version"]))
max = arr[i];
}
return body.instance_url + max.url;
}
else
return "";
}
//Connect to Salesforce instance and obtain the Access Token used for subsequent Salesforce calls this session
function loginSalesforceNLAP()
{
//production
var clientID = "3MVG9QDx8IX8nP5SpP0endmsendmeMepopopBvuB074i_7h7fakehoB0hnnhK7FyfTUgxH2234vR6QPoVXpDE";
var clientSecret = "1231232320412308455";
var securityToken = "N0bx9dI321F732aO12iC33gm6";
var username = "sflogin@login.com";
var password = "password";
var loginURL = "https://login.salesforce.com/services/oauth2/token";
var header = [];
header['Content-Type'] = 'application/json;charset=UTF-8';
var recordData = {};
var url = loginURL + "?grant_type=password&client_id=" + clientID + "&client_secret=" + clientSecret + "&username=" + username + "&password=" + password + securityToken;
try
{
response = _HTTPS.post({
url: url,
body: recordData,
headers: header
});
response = JSON.parse(JSON.stringify(response));
if (response.code == 200 || response.code == 204)
return JSON.parse(response.body); //return body
}
catch (er02)
{
log.error('ERROR:loginSalesforceNLAP', er02);
}
return "";
}
为了获取clientID,clientSecret和安全令牌,请遵循以下步骤:https://developer.salesforce.com/forums/?id=906F0000000AfcgIAC