Send Email when ASN file generation is failed

Run a script that collects all item fulfillments of only two specific customers which has status shipped, the ASN file is not generated and the package is updated
const BH_PHOTO = 132;
const ADORAMA =4110 ;
      
        const getAllItemFulfillmentRecords = () => {
            try {
                let resultObj = {};
                let searchResultIf = search.create({
                    type: "itemfulfillment",
                    filters:
                        [
                            ["type", "anyof", "ItemShip"],
                            "AND",
                            ["status", "anyof", "ItemShip:C"],
                            "AND",
                            ["custbody_sps_trans_carton_ct", "isnotempty", ""],
                            "AND",
                            ["customermain.internalid", "anyof", BH_PHOTO, ADORAMA],
                            "AND",
                            ["linefile.name", "doesnotcontain", "doc_asnFileCabinet_"],
                            "AND",
                            ["mainline", "is", "T"],
                            "AND",
                            ["custrecord_pack_content_fulfillment.custrecord_parent_pack_carton_index", "greaterthan", "0"],
                            "AND",
                            ["count(file.name)", "equalto", "0"],
                            "AND",
                            ["custbody_jj_marl951_emailsent", "is", "F"],
                            "AND",
                            ["custbody_sps_package_data_source", "noneof", "@NONE@"]

                        ],
                    columns:
                        [

                            search.createColumn({
                                name: "tranid",
                                summary: "GROUP",
                                label: "Document Number"
                            }),
                            search.createColumn({
                                name: "entity",
                                summary: "GROUP",
                                label: "Name"
                            }),
                            search.createColumn({
                                name: "name",
                                join: "file",
                                summary: "COUNT",
                                sort: search.Sort.ASC,
                                label: "Name"
                            }),
                            search.createColumn({
                                name: "internalid",
                                summary: "GROUP",
                                label: "Internal ID"
                            })
                        ]
                });
                let searchResultCount = searchResultIf.runPaged().count;
                if (searchResultCount > 0) {
                    searchResultIf.run().each(function (result) {
                        let ifId = result.getValue({
                            name: "internalid",
                            summary: "GROUP",
                            label: "Internal ID"
                        });
                        let docNo = result.getValue({
                            name: "tranid",
                            summary: "GROUP",
                            label: "Document Number"
                        });
                        resultObj[ifId] = docNo
                        return true;
                    });
                }
                return resultObj;
            } catch (error) {
                log.error("Error @getAllItemfulfillmentRecords", getAllItemFulfillmentRecords);
                return false;
            }
        }

        /**
         * Defines the Scheduled script trigger point.
         * @param {Object} scriptContext
         * @param {string} scriptContext.type - Script execution context. Use values from the scriptContext.InvocationType enum.
         * @since 2015.2
         */

        const execute = (scriptContext) => {
            try {
                let currentScript = runtime.getCurrentScript();
                let resultObj = getAllItemFulfillmentRecords();
                let itemFulfillmentRecordsArray = Object.keys(resultObj);
                let text = "";
                let exit = true;
                    for (let i = 0; i < itemFulfillmentRecordsArray.length && exit; i++) {
                        let output = url.resolveScript({
                            scriptId: 'customscript_sps_sl_svc_create_asn',
                            deploymentId: 'customdeploy_sps_sl_svc_create_asn',
                            returnExternalUrl: true,
                            params: {
                                'param1': itemFulfillmentRecordsArray[i]
                            }
                        })
                        let response = https.get({
                            url: output,
                            body: 'body',
                            headers: {"Accept": "application/json"}
                        });
                        record.submitFields({
                            type: record.Type.ITEM_FULFILLMENT,
                            id: itemFulfillmentRecordsArray[i],
                            values: {
                                'custbody_jj_marl951_emailsent': true
                            }
                        });
                        if (response.code != 200) {
                            let output1 = url.resolveRecord({
                                recordType: 'itemfulfillment',
                                recordId: itemFulfillmentRecordsArray[i],
                                isEditMode: false
                            });
                            let output2 = url.resolveDomain({
                                hostType: url.HostType.APPLICATION
                            });
                            let ifURL = output2 + output1;
                            text = text + `<br/><a href=${output1} target="_blank">${resultObj[itemFulfillmentRecordsArray[i]]}</a>`;
                        }
                        let remainingUsage = currentScript.getRemainingUsage();
                        if (remainingUsage < 500) {
                            let scriptTask = task.create({
                                taskType: task.TaskType.SCHEDULED_SCRIPT,
                                scriptId: 'customscript_jj_asn_file_gen_tr_marl_951',
                                deploymentId: 'customdeploy_jj_asn_file_gen_tr_marl_951'
                            });
                            let scriptTaskId = scriptTask.submit();
                            exit = false;
                        }
                    }
                    if (text) {
                        email.send({
                            author: '17351',
                            recipients: ['11697','15339'],
                            subject: 'ASN(s) failed to generate ',
                            body: 'Hello,<br/> The following item fulfillments failed to generate ASN files for SPS. <br/>' + text + '<br/><br/>Please visit the transaction records and click the “Create Advanced Shipping Notification” button. Then visit the Communication > Files tab to confirm the file was generated. <br/> <br/> Thank you, <br/><br/> Administrator'
                        });
                    }
            } catch (e) {
                log.debug("Error @execute", e);
            }
        }
        return {execute}
    });

Leave a comment

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