Send Email when the Bill status in the Custom record changes to Cancel

Send Email to the person who changes the status in the field Bill status to Cancel and the primary contact of the vendor

                    const oldRec = scriptContext.oldRecord;
                    const newRec = scriptContext.newRecord;


                    const recipientsArray = [];

                    const oldBillStatus = oldRec.getValue({
                        fieldId: 'custrecord_vbn_bill_status'
                    });

                    if (oldBillStatus != 3) {

                        const newBillStatus = newRec.getValue({
                            fieldId: 'custrecord_vbn_bill_status'
                        });

                        if (newBillStatus == 3) {

                            const currentUser = runtime.getCurrentUser();
                            log.debug("current user", currentUser);

                            const userEmail = currentUser.email;
                            log.debug("user eamil", userEmail);

                            if (userEmail) {
                                recipientsArray.push(userEmail);
                            }

                            const vendor = newRec.getValue({
                                fieldId: 'custrecord_vbn_vendor'
                            });


                            const vendorSearchObj = search.create({
                                type: "vendor",
                                filters:
                                    [
                                        ["internalid", "anyof", vendor],
                                        "AND",
                                        ["contact.role", "anyof", "1"]
                                    ],
                                columns:
                                    [
                                        search.createColumn({
                                            name: "entityid",
                                            sort: search.Sort.ASC,
                                            label: "Name"
                                        }),
                                        search.createColumn({
                                            name: "contactrole",
                                            join: "contact",
                                            label: "Role"
                                        }),
                                        search.createColumn({
                                            name: "email",
                                            join: "contact",
                                            label: "Email"
                                        })
                                    ]
                            });

                            log.debug("search", vendorSearchObj);

                            const searchResultCount = vendorSearchObj.runPaged().count;

                            log.debug("vendorSearchObj result count", searchResultCount);

                            if (searchResultCount > 0) {

                                vendorSearchObj.run().each(function (result) {

                                    const billingContactEmail = result.getValue({
                                        name: "email",
                                        join: "contact",
                                        label: "Email"
                                    });

                                    log.debug("billing contact email",billingContactEmail);

                                    if (billingContactEmail) {
                                        recipientsArray.push(billingContactEmail);
                                    }
                                    return true;
                                });

                                log.debug("recipients array", recipientsArray);

                            }
                            log.debug("recipients Array", recipientsArray);

                            if (recipientsArray.length > 0) {
                                email.send({
                                    author: 6,
                                    recipients: recipientsArray,
                                    subject: 'Bill status changed to cancel',
                                    body: 'The Bill status changed to Cancel in the VBN Vendor Billing Note',
                                });
                            }


                        }


                    }


                }

Leave a comment

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