Suitelet Script For Warranty Popup

define([‘N/ui/serverWidget’, ‘N/file’, ‘N/log’, ‘N/search’], function(serverWidget, file, log, search) {

    function onRequest(context) {

        try {

          log.debug(“hi”);

            if (context.request.method === ‘GET’) {

                // Load the HTML file

                var htmlFile = file.load({

                    id: ‘194644’

                });

                if (htmlFile) {

                    var customerId = context.request.parameters.customerId;

                log.debug(‘Customer ID:’, customerId);

                var items = JSON.parse(context.request.parameters.items);

                    log.debug(“itemdetails”, items);

                   

                    var internalIds = searchTransactionsByCustomerId(customerId);

                   

                       

   

                log.debug(“searhchresult”,internalIds);

                    // Read the content of the HTML file

                    var htmlContent = htmlFile.getContents();

                    htmlContent= htmlContent.replace(“REPLACE_SUBJOBS_LINES_HERE”, ` <select id=”salesorder” onchange=”myFunction()”>${internalIds.join()}</select>`)

                    var a=;

                    for(var i=0;i<items.length;i++){

                        a+=`<tr>`

                        a+=`<td><input type=”checkbox” name=”selectedItem” id=”checkbox${i}“></td>`

                        a+=`<td>${items[i][‘itemName’]}</td>`

                        a+=`<td>${items[i][‘quantity’]}</td>`

                        a+=`<td>${items[i][‘lineNumber’]}</td>`

                        a+=`</tr>`

                    }

                    htmlContent= htmlContent.replace(“TABLE_RESULT”,a)

                    log.debug(“htmlcontent”,htmlContent);

   

                    // Serve the HTML content as a response

                    context.response.write(htmlContent);

                } else {

                    log.error({

                        title: ‘HTML File Not Found’,

                        details: ‘The HTML file could not be found.’

                    });

                }

               

        }

     

        else {

            log.debug(“sending post request working”);

       

           

                let body = JSON.parse(context.request.body);

               

               

                if(body?.requestType){

                log.debug(“body”, body);

               

                let salesDet = body.salesOrder;

                log.debug(“salesorderid”, salesDet);

                let salesItems = salesOrder(salesDet);

                log.debug(“salesItems”, salesItems);

       

               

                    var b = ;

                    for (var i = 0; i < salesItems.length; i++) {

                        b += `<tr>`;

                        b += `<td><input type=”checkbox” name=”selectedItem” id=”checkbox${i}“></td>`;

                        b += `<td>${salesItems[i][‘item’]}</td>`;

                        b += `<td>${salesItems[i][‘quantity’]}</td>`;

                        b += `<td>${salesItems[i][‘lineId’]}</td>`;

                        b += `</tr>`;

                    }

                   

                   

                    context.response.write({

                        output: b,

                        contentType: ‘HTML’,

                       

                    });

                }

                else{

                    log.debug(“worked”,body)

                         

                    var dataToSend = {

                        salesOrder: body.salesOrder,

                        selectedItem: body.selectedItem

                    };

           

                    // Write the response with the data

                    context.response.write({

                        output: JSON.stringify(dataToSend), // Convert data to JSON string

            contentType: ‘JSON’ // Include dataToSend in the response

                    });

               

                }

                   

                   

                }

           

        }

       

       

         catch (e) {

            log.error({

                title: ‘Error Serving HTML Page’,

                details: e

            });

        }

    }

   

    function searchTransactionsByCustomerId(customerId) {

        var transactionSearchObj = search.create({

            type: “transaction”,

            filters: [

                [“customermain.internalid”, “anyof”, customerId],

                “AND”,

                [“mainline”, “is”, “T”]

            ],

            columns: [

                search.createColumn({ name: “internalid”, label: “Internal ID” }),

                search.createColumn({ name: “tranid”, label: “Document Number” })

            ]

        });

   

        var options = transactionSearchObj.run().getRange({ start: 0, end: 100 });

        var optionElements = options.map((result) => {

            var customerId = result.getValue({ name: “internalid”, label: “Internal ID” });

            var customerName = result.getValue({ name: “tranid”, label: “Document Number” });

            return `<option value=”${customerId}“>${customerName}</option>`;

        });

   

        log.debug(“option”, optionElements);

        return optionElements;

    }

    function testingFunction(customerId){

        try{

            log.debug(“working Properly”,customerId)

        }

        catch(e){

            log.error(“error@testingFunction”,e)

        }

    }

    function populateSalesOrdersDropdown(internalIds) {

        var salesOrderDropdown = document.getElementById(‘salesOrder’); // Select the dropdown element

   

        // Clear existing options

        salesOrderDropdown.innerHTML = ;

   

        // Populate dropdown with internal IDs

        internalIds.forEach(function(internalId) {

            var option = document.createElement(‘option’); // Create new option element

            option.value = internalId; // Set value attribute

            option.text = internalId; // Set text content

            salesOrderDropdown.appendChild(option); // Append option to select element

        });

    }

    function salesOrder(salesDet)

    {

    var salesorderSearchObj = search.create({

        type: “salesorder”,

        filters: [

           [“taxline”, “is”, “F”],

           “AND”,

           [“type”, “anyof”, “SalesOrd”],

           “AND”,

           [“internalid”, “anyof”, salesDet],

           “AND”,

           [“cogs”, “is”, “F”],

           “AND”,

           [“shipping”, “is”, “F”],

           ‘AND’,

           [“mainline”, “is”, “F”]

        ],

        columns: [

           search.createColumn({name: “item”, label: “Item”}),

           search.createColumn({name: “quantity”, label: “Quantity”}),

           search.createColumn({name: “line”, label: “Line ID”})

        ]

     });

     

     var searchResults = [];

     var searchResultSet = salesorderSearchObj.run();

     searchResultSet.each(function(result){

        // Create an object for each result

        var item = result.getText({name: “item”});

        var quantity = result.getValue({name: “quantity”});

        var lineId = result.getValue({name: “line”});

        // var item = result.getText({name: “item”}) ? result.getText({name: “item”}) : ” “;

        // var quantity = result.getValue({name: “quantity”}) ? result.getValue({name: “quantity”}) : ” “;

        // var lineId = result.getValue({name: “line”}) ? result.getValue({name: “line”}) : ” “;

       

       

        var resultObject = {

           item: item,

           quantity: quantity,

           lineId: lineId

        };

       

        // Push the object to the array

        searchResults.push(resultObject);

       

        // Continue processing additional results

        return true;

     });

     

     // Return the array of objects

     return searchResults;

    }

    return {

        onRequest: onRequest,

        populateSalesOrdersDropdown: populateSalesOrdersDropdown

    };

});

Leave a comment

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