Add lead details to Klaviyo

Solution

We can integrate with Klaviyo and post the netsuite data using their API

A sample UE is added below

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/*******************************************************************************
 * Date : 6 Sep 2021
 *
 * Author: Jobin & Jismi IT Services LLP
 * Script Description : Send Lead Details to Klaviyo
 *
 ******************************************************************************
 * Scipt :User Event Script
 * This script summarises on sending lead details to Klaviyo
 *
 * SCENARIO: On each submission of lead form, leads are created in Netsuite the same will be pushed to Klaviyo
 *
 * *****************************************************************************
 * REVISION HISTORY
 *
 * Revision 1.0 ${6 Sep 2021} JJ0131 : created
 */
define(['N/record', 'N/https', 'N/runtime'],
    /**
     * @param{record} record
     */
    (record, https, runtime) => {

        const afterSubmit = (scriptContext) => {
            try {
                var header = [];
                //API header Config
                header['Content-Type'] = 'application/json';
                header['Accept'] = 'application/json';
                var user = runtime.getCurrentUser();
                var userid = user.id;

                var leadRec = scriptContext.newRecord;
                //On creation of leads, lead details are fetched
                if (scriptContext.type === 'create') {
                    var recID = leadRec.id;
                    var leadRecord = record.load({
                        type: record.Type.LEAD,
                        id: recID,
                        isDynamic: true
                    });
                    //lead datas are stored as object
                    var leadObj = [];
                    var obj = {};

                    var fname = leadRecord.getValue({fieldId: 'firstname'});
                    var lname = leadRecord.getValue({fieldId: 'lastname'});
                    var email = leadRecord.getValue({fieldId: 'email'});
                    var city = leadRecord.getValue({fieldId: 'shipcity'});
                    var state = leadRecord.getValue({fieldId: 'shipaddr2'});

                    var phone = leadRecord.getValue({fieldId: 'phone'});
                    var company = leadRecord.getValue({fieldId: 'companyname'});
                    var category = leadRecord.getText({fieldId: 'category'});

                    obj.id = recID;
                    obj.first_name = fname;
                    obj.last_name = lname;
                    obj.City = city;
                    obj.State = state;
                    obj.organization = company + " at " + category;
                    obj.email = email;

                    leadObj.push(obj);
                    log.debug("obj", leadObj);
                    //Lead data is posted to Klaviyo using API
                    var postLead = https.post({
                        headers: header,
                        url: 'https://a.klaviyo.com/api/v2/list/Ty8sTV/members?api_key=pk_4c6c95d8fab82ab95630526785c14ad4c2',
                        body: JSON.stringify({profiles: leadObj})
                    });
                    log.debug("Code", postLead.code);
                    log.debug("Body", postLead.body);
                }

            } catch (e) {
                log.debug({title: e.name, details: e.message});
                log.error({title: e.name, details: e.message});
            }
        }

        return {afterSubmit}

    });

Leave a comment

Your email address will not be published. Required fields are marked *