Delete order from Bronto using checkbox

Jira Code: EN-55

DESCRIPTION

1)Delete order from Bronto once it is cancelled in Netsuite.

Action: User can cancel an order by checking the custom field in NetSuite as you suggested.

Solution: We will create a custom field(checkbox) in sales order named “cancel”. A user can delete an order from Bronto once it is cancelled in NetSuite using the order id.

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
 * EN-56 CS DELETE ORDER FROM BRONTO 
 * *************************************************************************
 * 
 * Date: 01-08-2018
 * 
 * Updated date :
 * 
 * Author: Jobin & Jismi IT Services LLP
 * 
 *****************************************************************************
 **/
define(['N/currentRecord', 'N/http', 'N/https', 'N/runtime', 'N/url', 'N/record'],

    function(currentRecord, http, https, runtime, url, record) {
        /**
         * Function to be executed after page is initialized.
         *
         * @param {Object} scriptContext
         * @param {Record} scriptContext.currentRecord - Current form record
         * @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
         *
         * @since 2015.2
         */
        var main = {
            pageInit: function(scriptContext) {

            },

            /**
             * Function to be executed when field is changed.
             *
             * @param {Object} scriptContext
             * @param {Record} scriptContext.currentRecord - Current form record
             * @param {string} scriptContext.sublistId - Sublist name
             * @param {string} scriptContext.fieldId - Field name
             * @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
             * @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
             *
             * @since 2015.2
             */
            fieldChanged: function(scriptContext) {
                var currRecord = currentRecord.get();
                log.debug('currRecord', currRecord);
                var fieldID = scriptContext.fieldId;
                log.debug('fieldID', fieldID)

                if (fieldID == "custbody_jj_en_56_cancelled") {
                    var value = currRecord.getValue({
                        fieldId: 'custbody_jj_en_56_cancelled'
                    });
                    console.log("value", value)
                    //to get the order Id
                    if (value == true) { //check whether the check box is checked or not
                        var userConfirm = confirm("Do want to cancel the order?");
                        if (userConfirm == true) {


                            var access_token = main.getToken();
                            log.debug('access_token', JSON.parse(access_token).access_token);
                            var objOrderId = currRecord.getValue({
                                fieldId: 'custbody_bronto_id'
                            });

                            var headerObj = {
                                "Authorization": 'Bearer ' + JSON.parse(access_token).access_token,
                                "Accept": 'application/json',
                                "Content-Type": 'application/json'
                            };
                            log.debug('headerObj', headerObj)
                            var response = https.delete({
                                url: 'https://rest.bronto.com/orders/' + objOrderId,
                                headers: headerObj
                            });
                            log.debug("response", response.code);
                            if (response.code == 204) {
                                var custId = currRecord.getValue({
                                    fieldId: 'entity'
                                });
                                /*var custRec = record.load({
                                    type: record.Type.CUSTOMER,
                                    id: custId
                                });
                                custRec.setValue({
                                    fieldId: 'custentity_bronto_imported',
                                    value: false
                                });
                                var saveRec = custRec.save();*/

                                currRecord.setValue({
                                    fieldId: 'custbody_bronto_imported',
                                    value: false
                                });
                                currRecord.setValue({
                                    fieldId: 'custbody_bronto_suppressed',
                                    value: true
                                });
                                currRecord.setValue({
                                    fieldId: 'custbody_bronto_error_count',
                                    value: "1"
                                });
                            }
                        } else {
                            currRecord.setValue({
                                fieldId: 'custbody_jj_en_56_cancelled',
                                value: false
                            });
                        }

                    }
                }
            },
            getToken: function() {
                var url = 'https://auth.bronto.com/oauth2/token'
                var body = {
                    client_secret: 'bd2bde692e7ca6e6f5c0d2e5e7f40a6bbc56deed',
                    client_id: '1100b2ad08daec7ab916d6a6e23d3e86f1898a3c',
                    grant_type: 'client_credentials'
                };
                var headers = {
                    "Content-Type": 'application/x-www-form-urlencoded',
                    "Accept": 'application/json'
                };
                var data = https.post({
                    url: url,
                    body: body,
                    headers: headers
                });
                var access_token = data.body;
                log.debug('access_token', access_token);

                return access_token;
            }

        }


        for (var key in main) {
            if (typeof main[key] === 'function') {
                main[key] = trycatch(main[key], key);
            }
        }

        function trycatch(myfunction, key) {
            return function() {
                try {
                    return myfunction.apply(this, arguments);
                } catch (e) {
                    log.debug("e in  " + key, e);
                    return false
                }
            }
        };
        return main;

    });

Leave a comment

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