简体   繁体   中英

How to update NetSuite through Salesforce?

How would you go about updating NetSuite through Salesforce. I know that you would use NetSuite's RESTlet and Salesforce Apex code to connect the two, but how would you actually go about doing this in a step by step process?

To send data from Salesforce to NetSuite (specifically customer/account data) you will need to do some preliminary setup in both.

In NetSuite:

Create a RESTlet Script that has at the bare minimum a get and post. For instance I would create a javascript file on my desktop that contains:

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

After You've saved this file, go to NetSuite>Customization>Scripting>Scripts>New.

Select the new file that you've saved and create the script record. Your script record in NetSuite should have GET and POST checked under scripts.

Next, click deploy script and choose who will call this script, specifically the user that will login on the salesforce end into NetSuite.

On the deployment page you will need the External URL it goes something like: https://1234567.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=123&deploy=1

Note: If any data that you are updating is critical for a process, I would highly recommond creating this in the sandbox first for testing, before moving to production.

In Salesforce sandbox:

Click YourName>Developer Console In the developer console click File>New and create an 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());
    }
}

You will have to set the endpoint here to the external url, the authorization nlauth_account=[your netsuite account number], and the header to your login email and password to a person who is on the deployment of the NS script, the body will be set in the trigger that calls this class.

Next create the trigger that will call this class. I made this script run every time I update the account in 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
    }
}

In this script data is the body that you are sending to NetSuite.

Additionally, you will have to create an authorized endpoint for NetSuite in your remote site setings in salesforce (sandbox and production). Go to setup, and quickfind remote site settings which will be under security controls. Create a new Remote site that has its remote site URL set to the first half of your external url: https://1234567.restlets.api.netsuite.com .

From here, do some testing in the sandbox. If all looks well deploy the class and trigger to salesforce production.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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