Creating journal entries from the vendor bill.

Jira Code: TRS-7

Create journal entries from the vendor bill approval button.

Create a suitelet form for creating a vendor bill. If the vendor bill has created, the button on the vendor bill is visible for approval and reject. If the approval button clicks, the journal entry will be created. On the reject button action, the vendor bill rejected and a new button will visible. (also transaction status will be changed according to the situations)

suitelet form for creating the vendor bill form.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */

/*******************************************************************************
* Trust Bridge
*  
 ******************************************************************************
* Date: 
 
* Author: Jobin & Jismi IT Services LLP
* Script Description:Created for button creation and validation.
* Date created :19/06/2019
 
******************************************************************************/
define(['N/record', 'N/search', 'N/runtime'],
    function(record, search, runtime) {
        function beforeLoad(context) {
            try {
                if (context.type == 'view') {
                    var vendorBill = context.newRecord;
                    var vendorBillId = vendorBill.id;
                    log.debug("vendorBillId", vendorBillId);
                    var currentUserRole = runtime.getCurrentUser().role;
                    log.debug("currentUserRole", currentUserRole)




                    var vendorBillBtn = context.form;

                    vendorBillBtn.clientScriptFileId = 644;
                    var mailIdFeld = null;
                    var approvalStatus = null;

                    var vendorbillSearchObj = search.create({
                        type: "vendorbill",
                        filters: [
                            ["type", "anyof", "VendBill"],
                            "AND",
                            ["internalid", "anyof", vendorBillId],
                            "AND",
                            ["mainline", "is", "T"]
                        ],
                        columns: [
                            search.createColumn({ name: "statusref", label: "Status" }),
                            search.createColumn({ name: "custbody_user_mailid", label: "USER MAILID" }),
                            search.createColumn({ name: "internalid", label: "Internal ID" })
                        ]
                    });
                    var searchResultCount = vendorbillSearchObj.runPaged().count;
                    log.debug("vendorbillSearchObj result count", searchResultCount);
                    vendorbillSearchObj.run().each(function(result) {
                        approvalStatus = result.getValue(vendorbillSearchObj.columns[0]);
                        mailIdFeld = result.getValue(vendorbillSearchObj.columns[1]);
                        return true;
                    });





                    if ((mailIdFeld) && (approvalStatus == 'pendingApproval')) {


                        var vendorBillApproveBtn = vendorBillBtn.addButton({
                            id: 'custpage_approve',
                            label: 'Approve',
                            functionName: 'ApproveBtn'
                        });

                        var VendorBillRejectBtn = vendorBillBtn.addButton({
                            id: 'custpage_reject',
                            label: 'Reject',
                            functionName: 'RejectBtn'
                        });
                    }
                    if ((mailIdFeld) && (approvalStatus == 'rejected')) {


                        var VendorBillRejectBtn = vendorBillBtn.addButton({
                            id: 'custpage_resubmit',
                            label: 'Resubmit',
                            functionName: 'ResubmitBtn'
                        });
                    }

                }
            } catch (er) {

                log.debug('er', er);
            }

        }

        function afterSubmit(context) {
            try {

                var vendorBillContent = context.newRecord;
                log.debug("vendorBillContent", vendorBillContent);
                var vendorBillIdAfter = vendorBillContent.id;
                log.debug("vendorBillIdAfter", vendorBillIdAfter);

                var journalEntryFieldAfter = null;
                var currentBillAmount = null;


                var vendorbillSearchObj = search.create({
                    type: "vendorbill",
                    filters: [
                        ["type", "anyof", "VendBill"],
                        "AND",
                        ["internalid", "anyof", vendorBillIdAfter],
                        "AND",
                        ["mainline", "is", "F"],
                        "AND",
                        ["taxline", "is", "F"],
                        "AND",
                        ["shipping", "is", "F"]
                    ],
                    columns: [
                        search.createColumn({ name: "fxamount", label: "Amount (Foreign Currency)" }),
                        search.createColumn({ name: "internalid", label: "Internal ID" }),
                        search.createColumn({ name: "custbody_journal_field", label: "JOURNAL FIELD" })
                    ]
                });
                var searchResultCount = vendorbillSearchObj.runPaged().count;
                log.debug("vendorbillSearchObj result count", searchResultCount);
                vendorbillSearchObj.run().each(function(result) {
                    currentBillAmount = result.getValue(vendorbillSearchObj.columns[0]);
                    journalEntryFieldAfter = result.getValue(vendorbillSearchObj.columns[2]);
                    return true;
                });

                if (journalEntryFieldAfter) {


                    var objRecord = record.load({
                        type: record.Type.JOURNAL_ENTRY,
                        id: journalEntryFieldAfter
                    });

                    objRecord.setSublistValue({
                        sublistId: 'line',
                        fieldId: 'debit',
                        line: 0,
                        value: currentBillAmount
                    })
                    objRecord.setSublistValue({
                        sublistId: 'line',
                        fieldId: 'credit',
                        line: 1,
                        value: currentBillAmount
                    })
                    var recordId = objRecord.save({
                        enableSource: true,
                        ignoreMandatoryFields: true
                    });
                    log.debug("recordId", recordId);
                }

            } catch (err) {
                log.debug("err", err);
            }

        }

        return {
            beforeLoad: beforeLoad,
            afterSubmit: afterSubmit
        };
    });

User event script for placing button and checking conditions.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */

/*******************************************************************************
* Trust Bridge
*  
 ******************************************************************************
* Date: 
 
* Author: Jobin & Jismi IT Services LLP
* Script Description:Created for button creation and validation.
* Date created :19/06/2019
 
******************************************************************************/
define(['N/record', 'N/search', 'N/runtime'],
    function(record, search, runtime) {
        function beforeLoad(context) {
            try {
                if (context.type == 'view') {
                    var vendorBill = context.newRecord;
                    var vendorBillId = vendorBill.id;
                    log.debug("vendorBillId", vendorBillId);
                    var currentUserRole = runtime.getCurrentUser().role;
                    log.debug("currentUserRole", currentUserRole)




                    var vendorBillBtn = context.form;

                    vendorBillBtn.clientScriptFileId = 644;
                    var mailIdFeld = null;
                    var approvalStatus = null;

                    var vendorbillSearchObj = search.create({
                        type: "vendorbill",
                        filters: [
                            ["type", "anyof", "VendBill"],
                            "AND",
                            ["internalid", "anyof", vendorBillId],
                            "AND",
                            ["mainline", "is", "T"]
                        ],
                        columns: [
                            search.createColumn({ name: "statusref", label: "Status" }),
                            search.createColumn({ name: "custbody_user_mailid", label: "USER MAILID" }),
                            search.createColumn({ name: "internalid", label: "Internal ID" })
                        ]
                    });
                    var searchResultCount = vendorbillSearchObj.runPaged().count;
                    log.debug("vendorbillSearchObj result count", searchResultCount);
                    vendorbillSearchObj.run().each(function(result) {
                        approvalStatus = result.getValue(vendorbillSearchObj.columns[0]);
                        mailIdFeld = result.getValue(vendorbillSearchObj.columns[1]);
                        return true;
                    });





                    if ((mailIdFeld) && (approvalStatus == 'pendingApproval')) {


                        var vendorBillApproveBtn = vendorBillBtn.addButton({
                            id: 'custpage_approve',
                            label: 'Approve',
                            functionName: 'ApproveBtn'
                        });

                        var VendorBillRejectBtn = vendorBillBtn.addButton({
                            id: 'custpage_reject',
                            label: 'Reject',
                            functionName: 'RejectBtn'
                        });
                    }
                    if ((mailIdFeld) && (approvalStatus == 'rejected')) {


                        var VendorBillRejectBtn = vendorBillBtn.addButton({
                            id: 'custpage_resubmit',
                            label: 'Resubmit',
                            functionName: 'ResubmitBtn'
                        });
                    }

                }
            } catch (er) {

                log.debug('er', er);
            }

        }

        function afterSubmit(context) {
            try {

                var vendorBillContent = context.newRecord;
                log.debug("vendorBillContent", vendorBillContent);
                var vendorBillIdAfter = vendorBillContent.id;
                log.debug("vendorBillIdAfter", vendorBillIdAfter);

                var journalEntryFieldAfter = null;
                var currentBillAmount = null;


                var vendorbillSearchObj = search.create({
                    type: "vendorbill",
                    filters: [
                        ["type", "anyof", "VendBill"],
                        "AND",
                        ["internalid", "anyof", vendorBillIdAfter],
                        "AND",
                        ["mainline", "is", "F"],
                        "AND",
                        ["taxline", "is", "F"],
                        "AND",
                        ["shipping", "is", "F"]
                    ],
                    columns: [
                        search.createColumn({ name: "fxamount", label: "Amount (Foreign Currency)" }),
                        search.createColumn({ name: "internalid", label: "Internal ID" }),
                        search.createColumn({ name: "custbody_journal_field", label: "JOURNAL FIELD" })
                    ]
                });
                var searchResultCount = vendorbillSearchObj.runPaged().count;
                log.debug("vendorbillSearchObj result count", searchResultCount);
                vendorbillSearchObj.run().each(function(result) {
                    currentBillAmount = result.getValue(vendorbillSearchObj.columns[0]);
                    journalEntryFieldAfter = result.getValue(vendorbillSearchObj.columns[2]);
                    return true;
                });

                if (journalEntryFieldAfter) {


                    var objRecord = record.load({
                        type: record.Type.JOURNAL_ENTRY,
                        id: journalEntryFieldAfter
                    });

                    objRecord.setSublistValue({
                        sublistId: 'line',
                        fieldId: 'debit',
                        line: 0,
                        value: currentBillAmount
                    })
                    objRecord.setSublistValue({
                        sublistId: 'line',
                        fieldId: 'credit',
                        line: 1,
                        value: currentBillAmount
                    })
                    var recordId = objRecord.save({
                        enableSource: true,
                        ignoreMandatoryFields: true
                    });
                    log.debug("recordId", recordId);
                }

            } catch (err) {
                log.debug("err", err);
            }

        }

        return {
            beforeLoad: beforeLoad,
            afterSubmit: afterSubmit
        };
    });

client script for the button action.

/**

  • @NApiVersion 2.x
  • @NScriptType ClientScript
  • @NModuleScope SameAccount
    */

/*

  • Trust Bridge

  • Date:
  • Author: Jobin & Jismi IT Services LLP
  • Script Description:Created for journal entry creation and buttons action.
  • Date created :20/06/2019

/
define([‘N/currentRecord’, ‘N/url’, ‘N/https’, ‘N/search’, ‘N/runtime’, ‘N/record’, ‘N/email’],

function(currentRecord, url, https, search, runtime, record, email) {

    function pageInit(scriptContext) {
        try {
            console.log("inn");
            document.getElementById("inpt_approvalstatus2").disabled = true;
            jQuery("#inpt_approvalstatus2,#inpt_approvalstatus2_arrow").css('pointer-events', 'none');
        } catch (err) {
            console.log("err", err)
        }

    }



    function ApproveBtn() {
        try {
            console.log("enter")
            document.getElementById("custpage_approve").disabled = true;

            var vendorBillApprove = currentRecord.get();
            console.log("vendorBillApprove", vendorBillApprove)
            var vendorBiilId = vendorBillApprove.id;
            console.log("vendorBiilId", vendorBiilId);


            var billArr = [];

            var vendorbillSearchObj = search.create({
                type: "vendorbill",
                filters: [
                    ["type", "anyof", "VendBill"],
                    "AND",
                    ["mainline", "is", "F"],
                    "AND",
                    ["internalid", "anyof", vendorBiilId],
                    "AND",
                    ["taxline", "is", "F"],
                    "AND",
                    ["shipping", "is", "F"]
                ],
                columns: [
                    search.createColumn({ name: "memomain", label: "Memo (Main)" }),
                    search.createColumn({ name: "subsidiary", label: "Subsidiary" }),
                    search.createColumn({ name: "currency", label: "Currency" }),
                    search.createColumn({ name: "trandate", label: "Date" }),
                    search.createColumn({ name: "fxamount", label: "Amount (Foreign Currency)" }),
                    search.createColumn({
                        name: "entityid",
                        join: "vendor",
                        label: "Name"
                    }),
                    search.createColumn({ name: "account", label: "Account" }),
                    search.createColumn({ name: "cseg5", label: "Fund Number" })

                ]
            });
            var searchResultCount = vendorbillSearchObj.runPaged().count;
            console.log("vendorbillSearchObj result count", searchResultCount);

            vendorbillSearchObj.run().each(function(result) {
                var billObj = {};
                billObj.memomain = result.getValue(vendorbillSearchObj.columns[0]);
                billObj.subsidiary = result.getValue(vendorbillSearchObj.columns[1]);
                billObj.currency = result.getValue(vendorbillSearchObj.columns[2]);
                billObj.trandate = result.getValue(vendorbillSearchObj.columns[3]);
                billObj.amount = result.getValue(vendorbillSearchObj.columns[4]);
                billObj.Name = result.getValue(vendorbillSearchObj.columns[5]);
                billObj.account = result.getValue(vendorbillSearchObj.columns[6]);
                billObj.fundnumber = result.getValue(vendorbillSearchObj.columns[7]);
                billArr.push(billObj);
                return true
            });
            console.log("billArr", billArr);

            // search for getting virtal currency account equalent for currency type 
            var currencyAccountName = null;
            var currencyAccountId = null;

            var accountSearchObj = search.create({
                type: "account",
                filters: [
                    ["custrecord_virtual_currency", "anyof", billArr[0].currency]
                ],
                columns: [
                    search.createColumn({
                        name: "name",
                        sort: search.Sort.ASC,
                        label: "Name"
                    }),
                    search.createColumn({ name: "internalid", label: "Internal ID" })
                ]
            });
            var searchResultCount = accountSearchObj.runPaged().count;
            console.log("accountSearchObj result count", searchResultCount);
            accountSearchObj.run().each(function(result) {
                currencyAccountName = result.getValue(accountSearchObj.columns[0]);
                currencyAccountId = result.getValue(accountSearchObj.columns[1]);
                return true;
            });
            console.log("currencyAccountName", currencyAccountName);
            console.log("currencyAccountId", currencyAccountId);


            var journalRecord = record.create({
                type: record.Type.JOURNAL_ENTRY
            });
            journalRecord.setValue({
                fieldId: 'trandate',
                value: new Date(billArr[0].trandate)
            });
            journalRecord.setValue({
                fieldId: 'subsidiary',
                value: billArr[0].subsidiary
            });
            journalRecord.setValue({
                fieldId: 'currency',
                value: billArr[0].currency
            });

            journalRecord.setValue({
                fieldId: 'memo',
                value: billArr[0].memomain
            });




            journalRecord.setSublistValue({ sublistId: 'line', fieldId: 'account', line: 0, value: currencyAccountId });
            journalRecord.setSublistValue({ sublistId: 'line', fieldId: 'debit', line: 0, value: billArr[0].amount });
            journalRecord.setSublistValue({ sublistId: 'line', fieldId: 'cseg5', line: 0, value: billArr[0].fundnumber });
            journalRecord.setSublistValue({ sublistId: 'line', fieldId: 'account', line: 1, value: currencyAccountId });
            journalRecord.setSublistValue({ sublistId: 'line', fieldId: 'credit', line: 1, value: billArr[0].amount });
            journalRecord.setSublistValue({ sublistId: 'line', fieldId: 'cseg5', line: 1, value: billArr[0].fundnumber });

            var recordId = journalRecord.save({
                enableSource: false,
                ignoreMandatoryFields: true
            });
            console.log("recordid", recordId);

            var journalField = record.submitFields({
                type: record.Type.VENDOR_BILL,
                id: vendorBiilId,
                values: {
                    custbody_journal_field: recordId,
                    approvalstatus: 2 // setting approval status to open
                }

            });
            console.log("journalField", journalField);

            window.location.reload();


        } catch (er) {
            console.log('er', er);
        }

    }

    function RejectBtn() {
        try {
            document.getElementById("custpage_reject").disabled = true;

            var vendorBillReject = currentRecord.get();
            console.log("vendorBillReject", vendorBillReject)

            var vendorBiilIdMail = vendorBillReject.id;
            console.log("vendorBiilIdMail", vendorBiilIdMail);
            var currentUserMail = runtime.getCurrentUser().email;
            console.log("currentUserMail", currentUserMail);

            var mailRecipient = null;

            var journalField = record.submitFields({
                type: record.Type.VENDOR_BILL,
                id: vendorBiilIdMail,
                values: {
                    approvalstatus: 3
                }

            });

            var vendorbillSearchObj2 = search.create({
                type: "vendorbill",
                filters: [
                    ["type", "anyof", "VendBill"],
                    "AND",
                    ["internalid", "anyof", vendorBiilIdMail],
                    "AND",
                    ["mainline", "is", "T"]
                ],
                columns: [
                    search.createColumn({ name: "custbody_user_mailid", label: "USER MAILID" })
                ]
            });
            var searchResultCount = vendorbillSearchObj2.runPaged().count;
            console.log("vendorbillSearchObj2 result count", searchResultCount);

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

                mailRecipient = result.getValue(vendorbillSearchObj2.columns[0]);
                return true;
            });
            console.log("mailRecipient", mailRecipient);
            if (mailRecipient) {
                console.log("innn")
                email.send({
                    author: -5, //administrator Mail,
                    recipients: mailRecipient,
                    subject: 'Vendor bill id - ' + vendorBiilIdMail + ' : Rejected',
                    body: 'The vendor bill of id = ' + vendorBiilIdMail + ' has been rejected. Please do necessary changes and resubmit.'
                });
            }

            window.location.reload();


        } catch (err2) {
            console.log('err2', err2);
        }
    }

    function ResubmitBtn() {
        try {

            document.getElementById("custpage_resubmit").disabled = true;
            var vendorBillResubmit = currentRecord.get();
            console.log("vendorBillResubmit", vendorBillResubmit)
            var vendorBiilIdResubmitBtn = vendorBillResubmit.id;
            console.log("vendorBiilIdResubmitBtn", vendorBiilIdResubmitBtn);

            var satusField3 = record.submitFields({
                type: record.Type.VENDOR_BILL,
                id: vendorBiilIdResubmitBtn,
                values: {
                    approvalstatus: 1
                }

            });
            window.location.reload();

        } catch (err3) {
            console.log('err3', err3);
        }
    }

    return {
        pageInit: pageInit,
        ApproveBtn: ApproveBtn,
        RejectBtn: RejectBtn,
        ResubmitBtn: ResubmitBtn


    };

});

client script for validatiing form and journal entry creation.

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */

/*******************************************************************************
* Trust Bridge
*  
 ******************************************************************************
* Date: 
 
* Author: Jobin & Jismi IT Services LLP
* Script Description:Created for auto populating field in the vendonor name selection.
* Date created :18/06/2019
 
******************************************************************************/

define(['N/record', 'N/currentRecord', 'N/search'], function(record, currentRecord, search) {



    function fieldChanged(context) {
        try {

            var currentRecord = context.currentRecord;
            console.log("currentRecord", currentRecord);

            var FieldName = context.fieldId;

            var currentVendor = currentRecord.getValue({
                'fieldId': 'custpage_name'
            });
            console.log("currentVendor", currentVendor);

            if (FieldName == 'custpage_name') {

                currentRecord.setValue({
                    fieldId: 'custpage_currency',
                    value: ""
                });

                var sourceItem = search.lookupFields({
                    type: search.Type.VENDOR,
                    id: currentVendor,
                    columns: ['cseg6']
                });

                console.log("sourceItem", sourceItem);

                if (sourceItem.cseg6[0])
                    var primaryCharity = sourceItem.cseg6[0].value;

                console.log("primaryCharity", primaryCharity);


                if (primaryCharity != null && primaryCharity != "" && primaryCharity != undefined) {
                    currentRecord.setValue({
                        fieldId: 'custpage_primarychar',
                        value: primaryCharity
                    });
                }

            }


            if (FieldName == 'custpage_currency') {

                var currentVendorCurrency = currentRecord.getValue({
                    'fieldId': 'custpage_currency'
                });

                var vendorSearchObjcurrency = search.create({
                    type: "vendor",
                    filters: [
                        ["internalid", "anyof", currentVendor],
                        "AND",
                        ["vendorcurrencybalance.currency", "anyof", currentVendorCurrency]
                    ],
                    columns: [

                    ]
                });
                var searchResultCount = vendorSearchObjcurrency.runPaged().count;
                console.log("vendorSearchObjcurrency result count", searchResultCount);

                if (searchResultCount == 0) {
                    console.log("innnnn");
                    alert("This currency is not added to the customer. Please add and try again.");
                    currentRecord.setValue({
                        fieldId: 'custpage_currency',
                        value: ""
                    });
                    return false

                }
                return true

            }


        } catch (err) {
            console.log("err", err)
        }
    }




    return {

        fieldChanged: fieldChanged

    };

});

Leave a comment

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