Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.8 kB
4
Indexable
Never
/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/currentRecord', 'N/record', 'N/runtime', 'N/url', 'N/search'],
/**
 * @param{currentRecord} currentRecord
 * @param{record} record
 * @param{runtime} runtime
 * @param{url} url
 */
function(currentRecord, record, runtime, url, search) {

    function pageInit(scriptContext) {

    }

    function send(scriptContext) {
        try {
            var recType = 'customrecord_aw_3pl_inv_transfer';
            var folderId;
            var content = new Array();
            var csvColumns = new Array();
            var lineOne = '';
            var rec = currentRecord.get();
            var recId = rec.id;
            var recUrl = url.resolveRecord({
                recordType: recType,
                recordId: recId,
                isEditMode: false
            });
            var custRec = record.load({
                type: recType,
                id: recId
            });
            var shipTo = custRec.getValue({
                fieldId: 'custrecord_ship_to_loc'
            });
            var shipFrom = custRec.getValue({
                fieldId: 'custrecord_ship_from_loc'
            });

            if (shipTo.indexOf('3PL') != -1) {
                folderId = 727300;
                log.debug('index', shipTo.indexOf("3PL"))
            } else if (shipFrom.indexOf("3PL") != -1) {
                folderId = 727299;
            }
            log.debug('folder id', folderId)

            var custRecSearch = search.create({
                type: "customrecord_aw_3pl_inv_transfer",
                filters:
                    [
                        ["internalid","anyof",recId]
                    ],
                columns:
                    [
                        search.createColumn({name: "custrecord_document_number", label: "Document Number"}),
                        search.createColumn({
                            name: "custrecord_line_number",
                            join: "CUSTRECORD_3PL_INV_TRANSFER_ID",
                            sort: search.Sort.ASC,
                            label: "Line Number"
                        }),
                        search.createColumn({
                            name: "custrecord_market_part_num",
                            join: "CUSTRECORD_3PL_INV_TRANSFER_ID",
                            label: "Marketing Part Number"
                        }),
                        search.createColumn({
                            name: "custrecord_ops_part_num",
                            join: "CUSTRECORD_3PL_INV_TRANSFER_ID",
                            label: "OPS Part Number"
                        }),
                        search.createColumn({
                            name: "custrecord_quantity",
                            join: "CUSTRECORD_3PL_INV_TRANSFER_ID",
                            label: "Quantity"
                        }),
                        search.createColumn({name: "custrecord_ship_date", label: "Ship Date"}),
                        search.createColumn({name: "custrecord_contact_name", label: "Contact Name"}),
                        search.createColumn({name: "custrecord_requester", label: "Requester"}),
                        search.createColumn({name: "custrecord_purpose", label: "Purpose"}),
                        search.createColumn({name: "custrecord_track_number", label: "Tracking Number"}),
                        search.createColumn({name: "custrecord_ship_to_loc", label: "Ship to Location"}),
                        search.createColumn({name: "custrecord_ship_from_loc", label: "Ship from Location"}),
                        search.createColumn({name: "custrecord_ship_to_address", label: "Ship to Address"})
                    ]
            });
            var resultSet = custRecSearch.run();

            resultSet.each(function (result) {
                var temp = '';
                for (var i = 0; i < custRecSearch.columns.length; i++) {
                    var searchResult = result.getValue({
                        name: custRecSearch.columns[i].name
                    });
                    temp += searchResult + ',';
                }
                content.push(temp);
                return true;
            });

            resultSet.columns.forEach(function (col) {
                csvColumns.push(col.label);
            });

            for (var i = 0; i < csvColumns.length; i++) {
                lineOne += csvColumns[i] + ',';
            }

            // Looping through the content array and assigning it to the contents string variable.
            lineOne = lineOne + '\n';
            for (var j = 0; j < content.length; j++) {
                lineOne += content[j].toString() + '\n';
            }

            log.debug({ title: 'Contents from Line One', details: JSON.stringify(lineOne) });

            // Creating a csv file and passing the contents string variable.
            var csvFile = file.create({
                name: 'search_csv_file.csv',
                fivarype: file.Type.CSV,
                contents: lineOne,
                folder: folderId // Folder ID where the file should be saved in the File Cabinet
            });

            log.debug('csv File', csvFile)

            /*csvFile.save();

            //3PL status changing to Sent
            record.submitFields({
                type: recType,
                id: recId,
                values: {
                    'custrecord_3pl_status': 2
                }
            });*/
            window.open(recUrl,"_self")
        } catch (e) {
            log.debug('ERROR', e)
        }
    }

    return {
        pageInit: pageInit,
        send: send
    };
    
});