Add members to Klaviyo list (Lost Lead sync)

Script: Userevent

When creating the lost lead or lead record on after submit trigger will fetch the required details and post it to Klaviyo by API POST request.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
/************************************************************************************************
 * * NetSuite - Klaviyo Integration Lost leads**
 * 
 * User event script for reak time API integration from NetSuite to Klaviyo
 *
 * **********************************************************************************************
 *
 * Author: Jobin & Jismi IT Services LLP
 *
 * Date Created : 04.04.2023
 *
 * Created By: JJ0199,  Jobin & Jismi IT Services LLP
 *
 * REVISION HISTORY
 *
 *
 * REVISED BY : Created by JJ0199 on 04.04.2023
 * LAST EDITOR:
 ***********************************************************************************************/
define(['N/record', 'N/https', 'N/runtime'],
    /**
     * 
     * @param record 
     * @param https 
     * @param runtime 
     */
    (record, https, runtime) => {

        /**
         * Constant values used for syncing the lost lead from NetSuite to Klaviyo
         */
        const API_VALUES_OBJ = {
            "URL": "https://a.klaviyo.com/api/v2/list/",
            "LIST_ID": "Enter list id",                                   
            "API_KEY": "Enetr private key from Klaviyo",
            "PATH_PARAM": "members"
        }
        const LOST_LEAD_REC_DETAILS={
            "first_name":"Update NetSuite field id",
            "last_name":"Update NetSuite field id",
            "email":"Update NetSuite field id",
            "phone":"Update NetSuite field id"
        }

        /**
         * @description The API request call for updating the Klaviyo list with a new lost lead
         * @param finalBodyArray Array containing the object of lead details
         */

        function lostLeadSyncApiRequest(finalBodyArray) {
            try {
                let header = [];
                header['Content-Type'] = 'application/json';
                header['Accept'] = 'application/json';
                let requestUrlConstruction = API_VALUES_OBJ.URL + '/' + API_VALUES_OBJ.LIST_ID + '/' + API_VALUES_OBJ.PATH_PARAM + '?api_key=' + API_VALUES_OBJ.API_KEY;
                let updateKlaviyoList = https.post({
                    headers: header,
                    url: requestUrlConstruction,
                    body: JSON.stringify({ profiles: finalBodyArray })
                });
                log.debug("response", { code: updateKlaviyoList.code, body: updateKlaviyoList.body });
            } catch (err) {
                log.error("error @ lostLeadSyncApiRequest function", err)
            }
        }
        /**
         * Defines the function definition that is executed after record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const afterSubmit = (scriptContext) => {
            try {
                let leadRec = scriptContext.newRecord;
                if (scriptContext.type === 'create') {
                    let finalBodyObj = {}
                    if (leadRec) {
                        finalBodyObj.id = leadRec.id;
                        finalBodyObj.first_name = leadRec.getValue({ fieldId: LOST_LEAD_REC_DETAILS.first_name });;
                        finalBodyObj.last_name = leadRec.getValue({ fieldId: LOST_LEAD_REC_DETAILS.last_name });;
                        finalBodyObj.email = leadRec.getValue({ fieldId: LOST_LEAD_REC_DETAILS.email });
                        finalBodyObj.phone_number = leadRec.getValue({ fieldId: LOST_LEAD_REC_DETAILS.phone });
                    }
                    let finalBodyArray = [finalBodyObj];
                    lostLeadSyncApiRequest(finalBodyArray);
                }
            } catch (e) {
                log.error({ title: e.name, details: e.message });
            }
        }

        return { afterSubmit }

    });

Leave a comment

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