简体   繁体   English

如何通过NetSuite更新Salesforce?

[英]How to update Salesforce through NetSuite?

How would you go about updating Salesforce with NetSuite data? 您将如何使用NetSuite数据更新Salesforce? I know that you would need to use Suitescript 2.0 and some sort of token authentication. 我知道你需要使用Suitescript 2.0和某种令牌认证。

I have a suitescript 2.0 user event script that does just this, It makes a connection to salesforce and updates netsuite customer fields to salesforce accounts. 我有一个套件脚本2.0用户事件脚本,它可以实现这一点,它连接到salesforce并将netsuite客户字段更新为salesforce帐户。

The script will update three custom fields on the salesforce account: NSID, sync, syntime. 该脚本将更新salesforce帐户上的三个自定义字段:NSID,sync,syntime。

/**
 *@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 "";
}

In order to get clientID, clientSecret, and the security token follow these steps: https://developer.salesforce.com/forums/?id=906F0000000AfcgIAC 要获取clientID,clientSecret和安全令牌,请执行以下步骤: https//developer.salesforce.com/forums/? id = 906F0000000AfcgIAC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM