write a scheduled script for customer it should create default contact

write a scheduled script for customer it should create default contact
first go to scripting tab in netsuite

then create new script for customer

/**
 * @NApiVersion 2.1
 * @NScriptType MapReduceScript
 * @NModuleScope SameAccount
*************************************************************************************/
define(['N/record', 'N/search'],
    /**
    * @param{record} record
    * @param{search} search
    */
    (record, search) => {
        "use strict";

        /**
         * Defines the function that is executed at the beginning of the map/reduce process and generates the input data.
         * @param {Object} inputContext
         * @param {boolean} inputContext.isRestarted - Indicates whether the current invocation of this function is the first
         *     invocation (if true, the current invocation is not the first invocation and this function has been restarted)
         * @param {Object} inputContext.ObjectRef - Object that references the input data
         * @typedef {Object} ObjectRef
         * @property {string|number} ObjectRef.id - Internal ID of the record instance that contains the input data
         * @property {string} ObjectRef.type - Type of the record instance that contains the input data
         * @returns {Array|Object|Search|ObjectRef|File|Query} The input data to use in the map/reduce process
         * @since 2015.2
         */
        const getInputData = (inputContext) => {
            try {
                let customerSearchObj = search.create({
                    type: "customer",
                    filters:
                    [
                       ["isinactive","is","F"], 
                       "AND", 
                       ["custentity1","anyof","2"], 
                       "AND", 
                       ["contact.internalid","anyof","@NONE@"], 
                       "AND", 
                       ["contact.isinactive","is","F"], 
                      "AND", 
                      ["isperson","is","F"]
                       //"AND", 
                       //["transaction.type","anyof","CustInvc","SalesOrd"], 
                       //"AND", 
                      // ["max(formulanumeric: DECODE(MAX(CASE WHEN {transaction.trandate} BETWEEN ADD_MONTHS({today},-36) AND ADD_MONTHS({today},0) then 1 else 0 end ),1,DECODE(MAX( CASE WHEN {transaction.trandate} BETWEEN ADD_MONTHS({today},-6) AND ADD_MONTHS({today},0) then 1 else 0 end),0,1,1,0),0,0))","equalto","1"]
                    ],
                    columns:
                    [
                       search.createColumn({
                          name: "entityid",
                          sort: search.Sort.ASC,
                          label: "Customer Name"
                       }),
                       search.createColumn({name: "email", label: "Customer Email"}),
                       search.createColumn({
                          name: "internalid",
                          join: "contact",
                          label: "Contact ID"
                       }),
                       search.createColumn({
                          name: "entityid",
                          join: "contact",
                          label: "Contact Name"
                       }),
                       search.createColumn({
                          name: "email",
                          join: "contact",
                          label: "Contact Email"
                       }),
                       search.createColumn({name: "internalid", label: "Internal ID"})
                    ]
                 });
                
                 let inputData = [];
                 customerSearchObj.run().each(function(result){
                    inputData.push({
                        customerId: result.getValue({name: "internalid", label: "Internal ID"}),
                        customerName: result.getValue({
                            name: "entityid",
                            sort: search.Sort.ASC,
                            label: "Customer Name"
                         }),
                        customerEmail: result.getValue({name: "email", label: "Customer Email"})
                    });
                    return true;
                 });
                 

                 log.debug("inputData", inputData);
                 return inputData;
            } catch (e) {
                log.debug('error@getInputData', e);
                return [];
            }
        }

        /**
         * Defines the function that is executed when the reduce entry point is triggered. This entry point is triggered
         * automatically when the associated map stage is complete. This function is applied to each group in the provided context.
         * @param {Object} reduceContext - Data collection containing the groups to process in the reduce stage. This parameter is
         * provided automatically based on the results of the map stage.
         * @param {Iterator} reduceContext.errors - Serialized errors that were thrown during previous attempts to execute the
         * reduce function on the current group
         * @param {number} reduceContext.executionNo - Number of times the reduce function has been executed on the current group
         * @param {boolean} reduceContext.isRestarted - Indicates whether the current invocation of this function is the first
         * invocation (if true, the current invocation is not the first invocation and this function has been restarted)
         * @param {string} reduceContext.key - Key to be processed during the reduce stage
         * @param {List<String>} reduceContext.values - All values associated with a unique key that was passed to the reduce stage
         * for processing
         * @since 2015.2
         */
        const reduce = (reduceContext) => {
            try {
                // Process each customer data in the reduce context
                // reduceContext.values.forEach(function(value) {
                //     var customerData = JSON.parse(value);

                //     // Create a contact record for the customer
                //     var contactId = createContact(customerData.customerId, customerData.customerName, customerData.customerEmail);

                //     log.debug('Contact Created', 'Contact ID: ' + contactId);
                // });
                log.debug("reduceContext",reduceContext);
                let contextVal = JSON.parse(reduceContext.values);
                log.debug("contextVal",contextVal);
                createContact(contextVal)

            } catch (e) {
                log.debug('error@reduce', e);
            }
        }

        /**
         * Helper function to create a contact for a customer
         * @param {string} customerId - Internal ID of the customer
         * @param {string} customerName - Customer Name
         * @param {string} customerEmail - Customer Email
         * @returns {string} - Internal ID of the created contact
         */
        function createContact(contextVal) {
            var contact = record.create({
                type: record.Type.CONTACT,
                isDynamic: true
            });

            contact.setValue({fieldId:"entityid", value: contextVal.customerName}); // Set the contact name to "prachanth"
            contact.setValue({fieldId:'email', value: contextVal.customerEmail}); // Set the email to the customer's email
            contact.setValue({fieldId:'company', value: contextVal.customerId});
            // You can set other fields for the contact here
            var contactId = contact.save({ enableSourcing: true, ignoreMandatoryFields: true });
            log.debug('contactId', contactId);       
        }

        

        return { getInputData, reduce }
    });

add this file and deploy the script .

NOTE: we are taking the records from the saved search change according to your needs.

Leave a comment

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