Convert SPR record(custom record) to Quote

USEREVENT SCRIPT


define(["N/ui/serverWidget", "N/record", "N/xml", "N/runtime","N/ui/message"],

    (serverWidget, record, xml, runtime,message) => {

        const beforeLoad = (scriptContext) => {
            try {
                //remove unwanted links, buttons and create quote button on view mode
                if (scriptContext.type == "view") {
                    scriptContext.form.clientScriptModulePath = 'SuiteScripts/Jobin & Jismi IT Services LLP/NTIN-95/JJ CS SPR to Quote Conversion NTIN-97.js';

                    var inlineField = scriptContext.form.addField({
                        id: 'custpage_inline_field1',
                        type: 'INLINEHTML',
                        label: 'Button1'
                    });

                     var scr = '<script>jQuery( document ).ready(function(){jQuery("#recmachcustrecordjj_parent_main_form").hide();jQuery("#tbl_newrecrecmachcustrecordjj_parent.uir-button").hide();jQuery("#tbl_customize.uir-button").hide();jQuery("#customize.rndbuttoninpt.bntBgT.__web-inspector-hide-shortcut__").hide();jQuery("#a.dottedlink").hide();jQuery("#td.listtext.uir-list-row-cell.__web-inspector-hide-shortcut__").hide();jQuery("#recmachcustrecordjj_parentdir0.listheadersort").hide();jQuery("#td.listtext.uir-list-row-cell").hide();jQuery("#a.dottedlink.__web-inspector-hide-shortcut__").hide();jQuery("#customize.rndbuttoninpt.bntBgT").hide();jQuery("#tdbody_newrecrecmachcustrecordjj_parent.bntBgT").hide();jQuery("#newrecrecmachcustrecordjj_parent.rndbuttoninpt.bntBgT").hide();jQuery("#rndbuttoninpt bntBgT").hide();jQuery("#td.__web-inspector-hide-shortcut__").hide();jQuery("#tr_attach.tabBnt").hide();jQuery("#tdbody_attach.bntBgB").hide();jQuery("#attach.rndbuttoninpt.bntBgT").hide();{if(jQuery("#recmachcustrecordjj_parent__tab").length){jQuery("#recmachcustrecordjj_parent__tab tbody tr td:last-child").css("display", "none");jQuery("#recmachcustrecordjj_parent__tab thead tr td:first-child").css("display","none");jQuery("#recmachcustrecordjj_parent__tab tbody tr td:first-child").css("display", "none");jQuery("#recmachcustrecordjj_parent__tab thead tr td:nth-last-child(2)").css("display", "none");}};})</script>';

                    inlineField.defaultValue = scr

                    // quote button
                    scriptContext.form.addButton({
                        id: 'custpage_quotebutton',
                        label: 'Quote',
                        functionName: 'converttoQuote'
                    });
                    //delete button
                    scriptContext.form.addButton({
                        id: 'custpage_delete',
                        label: 'Delete',
                        functionName: 'deleteSPR'
                    });

                }
                //set default values for expires and sales rep
                if (scriptContext.type == "create") {

                    var UserObj = runtime.getCurrentUser();
                    var salesRep = UserObj.id
                    var currentRecord = scriptContext.newRecord;
                    currentRecord.setValue({
                        fieldId: 'custrecord_jj_spr_salesrep',
                        value: salesRep
                    });


                }
                //hide new item button on edit mode
                if (scriptContext.type == "edit") {
                    var inlineField2 = scriptContext.form.addField({
                        id: 'custpage_inline_field2',
                        type: 'INLINEHTML',
                        label: 'Button2'
                    });
                    var scr = "<script>jQuery( document ).ready(function(){jQuery('#tbl_newrec419').hide()});</script>";
                    inlineField2.defaultValue = scr;
                }

            } catch (e) {
                log.debug("error@beforeLoad", e)
            }
        }

        return {beforeLoad}

    });

CLIENT SCRIPT



define(['N/record','N/currentRecord','N/search', 'N/ui/message'],

function(record,currentRecord,search,message) {

    function pageInit(scriptContext) {

    }

    /*********************************************************************
     * Function to create quote from SPR record
     *********************************************************************/
    function converttoQuote() {
        try {
                jQuery('#custpage_quotebutton').prop('disabled', true);
            var currentSPR= currentRecord.get().id
            var objRecord = record.load({
                type: 'customrecord_jj_spr_custom',
                id: currentSPR,
                isDynamic: true,
            });

            //get details from SPR Record
            var customer=objRecord.getValue({
                fieldId:'custrecord_jj_cust_name'
            });
            var expDate=objRecord.getValue({
                fieldId:'custrecord_jj_expdate'
            });
            var docNum=objRecord.getValue({
                fieldId:'name'
            });

            var line_count = objRecord.getLineCount({
                sublistId: 'recmachcustrecordjj_parent'
            });

            var itemArray=[];

            // get sublist values from spr
            for(var i=0; i<line_count; i++){
                var itemObj={}
                var lineNum=objRecord.selectLine({
                    sublistId:'recmachcustrecordjj_parent',
                    line:i
                });
                itemObj.itemName=objRecord.getCurrentSublistValue({
                    sublistId: 'recmachcustrecordjj_parent',
                    fieldId: 'custrecordjj_item',
                });

                itemObj.itemQty=objRecord.getCurrentSublistValue({
                    sublistId: 'recmachcustrecordjj_parent',
                    fieldId: 'custrecord_jj_qty',
                });

                itemObj.sprCost=objRecord.getCurrentSublistValue({
                    sublistId: 'recmachcustrecordjj_parent',
                    fieldId: 'custrecordjj_spr_cost',
                });
                itemObj.itemUnitPrice=objRecord.getCurrentSublistValue({
                    sublistId: 'recmachcustrecordjj_parent',
                    fieldId: 'custrecord_jj_price_level',
                });

                itemArray.push(itemObj);
            }
           // create quote record from SPR
            var quoteRecord = record.create({
                type: record.Type.ESTIMATE,
                isDynamic: true,
                defaultValues: {
                    entity:customer
                }
            });
            quoteRecord.setValue({
                fieldId:'duedate',
                value:expDate
            });

            //set item sublist values
            for(var j=0; j<itemArray.length;j++){
                quoteRecord.selectNewLine({
                    sublistId: 'item'
                });
                quoteRecord.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'item',
                    value: itemArray[j].itemName
                });
                quoteRecord.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'quantity',
                    value: itemArray[j].itemQty
                });
                // set custom price level
                quoteRecord.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'price',
                    value: -1
                });
                quoteRecord.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'custcol_jj_spr_cost',
                    value: itemArray[j].sprCost
                });
                quoteRecord.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'rate',
                    value: itemArray[j].itemUnitPrice
                });
                quoteRecord.setCurrentSublistValue({
                    sublistId: 'item',
                    fieldId: 'custcol_jj_spr_hash',
                    value:docNum
                });
                quoteRecord.commitLine({
                    sublistId: 'item'
                });
            }

            quoteRecord.setValue({
                fieldId:'custbody_jj_spr_reference',
                value:currentSPR
            });

                var recId=quoteRecord.save({
                    enableSourcing: true,
                    ignoreMandatoryFields: true
                });

            if(recId)
            {
                jQuery('#custpage_quotebutton').prop('enabled', true);
                var fieldLookUp = search.lookupFields({
                    type: search.Type.ESTIMATE,
                    id: recId,
                    columns: ['tranid']
                });
               var quoteDocNum= fieldLookUp.tranid
                alert("Quote record " +quoteDocNum+ " created successfully")

                // var confMsg = message.create({
                //     title: "Congratulations",
                //     message: "Quote record " +quoteDocNum+ " created successfully",
                //     type: message.Type.CONFIRMATION
                // });
                //
                // confMsg.show();
                // setTimeout(confMsg.hide, 700000);

                var existValues= objRecord.getValue({
                    fieldId:'custrecord_jj_createdfrom_quote'
                })
                // setting the related transactions on the multiselect field
                var recIdArray=[]
                for(var i=0; i<existValues.length; i++){
                    recIdArray.push(existValues[i])
                }
                recIdArray.push(JSON.stringify(recId))
                objRecord.setValue({
                    fieldId: 'custrecord_jj_createdfrom_quote',
                    value:recIdArray
                });
                objRecord.save({
                    enableSourcing: true,
                    ignoreMandatoryFields: true
                });

                window.onbeforeunload = function () { };
                location.reload();
                // navigate to created Quote
                //window.open('https://448505.app.netsuite.com/app/accounting/transactions/estimate.nl?id='+recId)

            }
        }
        catch (e)
        {
            console.log("error@converttoQuote",e)
            alert("Quote record is not created due to the following error",e)
        }
    }


    /******************************************************************************
     * Function to create delete record from SPR record
     ******************************************************************************/
    function deleteSPR() {
        try {

            var currentSPR = currentRecord.get().id

            var objRecord = record.load({
                type: 'customrecord_jj_spr_custom',
                id: currentSPR,
                isDynamic: true,
            });
            var relatedTransaction=objRecord.getValue({
                fieldId: 'custrecord_jj_createdfrom_quote',
            });
            if(relatedTransaction.length>0) {
                for (var i = 0; i < relatedTransaction.length; i++) {
                    var relateRecId = relatedTransaction[i]
                    var recordType = search.lookupFields({
                        type: search.Type.TRANSACTION,
                        id: relateRecId,
                        columns: 'type'
                    });

                    var recTypeIs=recordType.type
                    var type= recTypeIs[0].value
                    var typeIS= type.toLowerCase()
                    if(type=="SalesOrd"){
                        typeIS='salesorder'
                    }
                    record.submitFields({
                        type:typeIS,
                        id: relateRecId,
                        values: {
                            custbody_jj_spr_reference: ''
                        }
                    });
                }
            }
            var line_count = objRecord.getLineCount({
                sublistId: 'recmachcustrecordjj_parent'
            });
            console.log("line_count", line_count)
            for(var j=0; j< line_count;j++){
                console.log("remove")
                objRecord.selectLine({
                    sublistId: 'recmachcustrecordjj_parent',
                    line: j
                });
                var childRecId=objRecord.getCurrentSublistValue({
                    sublistId: 'recmachcustrecordjj_parent',
                    fieldId: 'id'
                });
                console.log("childRecId",childRecId)
                if(childRecId){
                    record.delete({
                        type: 'customrecord_jj_item_record',
                        id: childRecId,
                    });
                }

                objRecord.removeLine({
                    sublistId: 'recmachcustrecordjj_parent',
                    line: j
                });
            }
            record.delete({
                type: 'customrecord_jj_spr_custom',
                id:currentSPR ,
            });
            var url='https://448505.app.netsuite.com/app/center/card.nl?sc=-29&whence='
            window.open(url,"_self")

            var deleteMsg = message.create({
                title: "Congratulations",
                message: "SPR Record deleted successfully",
                type: message.Type.CONFIRMATION
            });

            deleteMsg.show({
                duration: 900000
            });

        }catch (e) {
            console.log("error@deleteSPR :"+e)
        }
    }

    return {
        converttoQuote:converttoQuote,
        deleteSPR:deleteSPR,
        pageInit: pageInit,

    };
    
});

Leave a comment

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