admin管理员组

文章数量:1122832

I have created a Suitelet, and within that Suitelet, I am creating a form. In the POST request, I am displaying the form with two buttons: one is the 'Submit' button, and the other is the 'Add' button.

on the add button run client script but there issue.

When I click the 'Submit' button for the second time, it call the 'Add' button function in the client script.

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */

define(['N/ui/serverWidget', 'N/search','N/https', 'N/log','N/runtime','N/file'], function(serverWidget, search,https, log,runtime,file) {

    /**
          * Defines the Suitelet script trigger point.
          * @param {Object} context
          * @param {ServerRequest} context.request - Incoming request
          * @param {ServerResponse} context.response - Suitelet response
          * @since 2015.2
          */
   
     function onRequest(context) {
 
       var request = context.request;
       var params = request.parameters;
       var methodClick = request.method;
       var submitter=params.submitter;
       
 
       log.debug('request',request);
       log.debug('params',params);
       log.debug('methodClick',methodClick);
       log.debug('submitter',submitter);

       var jsonContent = JSON.stringify(params, null, 2);
/*try{
       var fileObj = file.create({
        name: 'response_data.json',
        fileType: file.Type.JSON,
        contents: jsonContent,
        folder: 1671 // Replace with your folder's internal ID
    });
    var fileId = fileObj.save();
}catch(e){
    log.debug('error',e);
}*/
       

         // Create the form
         var form = serverWidget.createForm({
             title: 'Specbook Form'
         });
         // Add a text field
         var textField = form.addField({
             id: 'custpage_textfield',
             type: serverWidget.FieldType.TEXT,
             label: 'Specbook Id'
         }).isMandatory = true;


         // Add a hidden field to capture the submitter
        var submitterField = form.addField({
            id: 'custpage_submitter',
            type: serverWidget.FieldType.TEXT,
            label: 'Submitter'
        }).updateDisplayType({
            displayType: serverWidget.FieldDisplayType.HIDDEN
        });

        // Add a hidden field to capture the submitter
        var customButtonField = form.addField({
            id: 'custpage_custom_b',
            type: serverWidget.FieldType.TEXT,
            label: 'CustomB'
        }).updateDisplayType({
            displayType: serverWidget.FieldDisplayType.HIDDEN
        });
       
         // Add an inline HTML field for displaying error below the text field
         var errorField = form.addField({
             id: 'custpage_error',
             type: serverWidget.FieldType.INLINEHTML,
             label: ' '
         });
       
         errorField.updateLayoutType({
             layoutType: serverWidget.FieldLayoutType.OUTSIDE
         });
       
         errorField.updateBreakType({
             breakType : serverWidget.FieldBreakType.STARTROW
         });
       
         // Add a submit button
         form.addSubmitButton({
             label: 'Submit Form',
           id:'submitb'
             
         });
         errorField.defaultValue = '';
         
          // Add the loader HTML field
        /* var loaderField = form.addField({
             id: 'custpage_loader',
             type: serverWidget.FieldType.INLINEHTML,
             label: ' '
         });
         loaderField.defaultValue ='<style> .loader-hidden { display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 1000; font-size: 18px; color: #333; text-align: center; } .loader-visible { display: block; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); z-index: 1000; font-size: 18px; color: #333; text-align: center; } .spinner { width: 40px; height: 40px; border: 5px solid #f3f3f3; border-top: 5px solid #3498db; border-radius: 50%; animation: spin 1s linear infinite; margin: 0 auto; } @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }</style><div id="loaderXYZ" class="loader-hidden"><div class="spinner"></div><p>Loading...</p></div>';
 */
 
  
       //After Submit Form Call Post Method and Show data
         if (context.request.method === 'POST') {

          // Set the value of the hidden field to the submitter
          submitterField.defaultValue = params.submitter;

           
        var custButton= form.addButton({
                    id: 'custpage_create_sales_order',
                    label: 'Create Sales Order',
                    functionName: 'saveRecord'
                });
              
               // Client script function to process selected records
                form.clientScriptModulePath = './create_sales_order_client.js';

                log.debug('custom button',custButton);
                  
                var custButtonLabel=custButton.label;
                customButtonField.defaultValue = custButtonLabel;
            
                
            
           
             // Fetch submitted data
             var specbookID = context.request.parameters.custpage_textfield;
 
            //Fetch Specbook Response from Specbook ID
             var SpecbookData=fetchSpecbook(specbookID);
           
             if (!SpecbookData || !SpecbookData.categories) {
               
                 errorField.defaultValue = '<p style="color:red;">Invalid Specbook ID. Please enter a valid ID.</p>';
               
             } else {
                 var sublist = form.addSublist({
                     id: 'custpage_search_results',
                     type: serverWidget.SublistType.LIST,
                     label: 'Orders'
                 });
                 
                 sublist.addField({
                     id: 'custpage_checkbox',
                     type: serverWidget.FieldType.CHECKBOX,
                     label: 'Select'
                 });
               
                 /*sublist.addField({
                     id: 'custpage_customer_name',
                     type: serverWidget.FieldType.TEXT,
                     label: 'CUSTOMER'
                 });*/
 
                 sublist.addField({
                     id: 'custpage_specbookid',
                     type: serverWidget.FieldType.TEXT,
                     label: 'Specbook Id'
                 }).updateDisplayType({
                     displayType: serverWidget.FieldDisplayType.HIDDEN
                 });
 
                 sublist.addField({
                     id: 'custpage_spec_ordid',
                     type: serverWidget.FieldType.TEXT,
                     label: 'Specbook Order Id'
                 }).updateDisplayType({
                     displayType: serverWidget.FieldDisplayType.HIDDEN
                 });
 
                 
 
                 // Add customer dropdown field to the sublist
                 var customerField = sublist.addField({
                     id: 'custpage_customer_name',
                     type: serverWidget.FieldType.SELECT,
                     label: 'Customer'
                 });
 
                 // Populate customer dropdown
                 var customers = getCustomerList();
                 customers.forEach(function(customer) {
                     customerField.addSelectOption({
                         value: customer.id,
                         text: customer.name
                     });
                 });
 
 
                 //Salee order field
                 sublist.addField({
                     id: 'custpage_sales_order_id',
                     type: serverWidget.FieldType.TEXT,
                     label: 'Sales Order ID'
                 });
               
                 sublist.addField({
                     id: 'custpage_category_name',
                     type: serverWidget.FieldType.TEXT,
                     label: 'Location'
                 });
 
               sublist.addField({
                     id: 'custpage_brand_name',
                     type: serverWidget.FieldType.TEXT,
                     label: 'Brand'
                 });
 
               sublist.addField({
                     id: 'custpage_model',
                     type: serverWidget.FieldType.TEXT,
                     label: 'Model'
                 });
               
                 sublist.addField({
                     id: 'custpage_product_name',
                     type: serverWidget.FieldType.TEXT,
                     label: 'Description'
                 });
               
                 sublist.addField({
                     id: 'custpage_qty',
                     type: serverWidget.FieldType.TEXT,
                     label: 'Qnty'
                 });
               
                 sublist.addField({
                     id: 'custpage_price',
                     type: serverWidget.FieldType.TEXT,
                     label: 'Each'
                 });
               
                 sublist.addField({
                     id: 'custpage_shippingcost',
                     type: serverWidget.FieldType.TEXT,
                     label: 'Shipping Cost'
                 });
               
                 sublist.addField({
                     id: 'custpage_status',
                     type: serverWidget.FieldType.TEXT,
                     label: 'Status'
                 });
               
                 var line = 0;
                 var customerName=SpecbookData.profileName;
                 var customerId = searchDefaultCustomer(customerName);
               
                 SpecbookData.categories.forEach(function (category) {
                   
                     var categoryName = category.name;
                   
                     category.products.forEach(function (product) {
                       
                         var productName = product.name;
                         var orderId = product.orderID || '0';
                         var qty = product.qty || '';
                         var price = product.price.salePrice || '';
                         var shippingCost = product.price.shippingCost || '';
                         var status = product.status || '';
                         var manufacturarName= product.manufacturerName || '';
                         var modelNo=product.modelNumber || '';
 
                         //Check If aready sales order create for specbook
                         var recordInfo = getSalesOrderId(specbookID, orderId, modelNo);
 
                     if (recordInfo && recordInfo.salesOrderId) {
                         var salesOrdId =recordInfo.salesOrderId || '';
                         var customerName1=recordInfo.custCustomerName;
                          var customerId1 = searchDefaultCustomer(customerName1);
                     }
                     
 
                       //  log.debug('Order id',salesOrdId);
                       // log.debug('Customer Name',customerName1);
                       
                         sublist.setSublistValue({
                             id: 'custpage_specbookid',
                             line: line,
                             value: specbookID
                         });
 
                        sublist.setSublistValue({
                             id: 'custpage_spec_ordid',
                             line: line,
                             value: orderId
                         });
                         
                         if (salesOrdId) {
                         sublist.setSublistValue({
                             id: 'custpage_customer_name',
                             line: line,
                             value: customerId1
                         });
                     }else{
                         sublist.setSublistValue({
                             id: 'custpage_customer_name',
                             line: line,
                             value: customerId
                         });
                     }
                     
                     
 
                         if (salesOrdId) {
                             sublist.setSublistValue({
                                 id: 'custpage_sales_order_id',
                                 line: line,
                                 value: salesOrdId
                             });
                         }
                       
                         sublist.setSublistValue({
                             id: 'custpage_category_name',
                             line: line,
                             value: categoryName
                         });
 
                       sublist.setSublistValue({
                             id: 'custpage_brand_name',
                             line: line,
                             value: manufacturarName
                         });
 
                       sublist.setSublistValue({
                             id: 'custpage_model',
                             line: line,
                             value: modelNo
                         });
                       
                         sublist.setSublistValue({
                             id: 'custpage_product_name',
                             line: line,
                             value: productName
                         });
                       
                         sublist.setSublistValue({
                             id: 'custpage_qty',
                             line: line,
                             value: qty.toString()
                         });
                       
                         sublist.setSublistValue({
                             id: 'custpage_price',
                             line: line,
                             value: price.toString()
                         });
                       
                         sublist.setSublistValue({
                             id: 'custpage_shippingcost',
                             line: line,
                             value: shippingCost.toString()
                         });
                       
                         sublist.setSublistValue({
                             id: 'custpage_status',
                             line: line,
                             value: status
                         });
                       
                         line++;
                       
                     });
                 });
             }  
         }
          context.response.writePage(form);
     }
 
     // Function to fetch the customer list with paged results
 function getCustomerList() {
     var customers = [];
     var customerSearch = search.create({
         type: search.Type.CUSTOMER,
         columns: ['internalid', 'companyname'],
         filters: [] // Add filters if needed
     });
 
     // Use paged search to handle large result sets
     var pagedData = customerSearch.runPaged({
         pageSize: 1000 // Process 1000 results at a time
     });
 
     pagedData.pageRanges.forEach(function(pageRange) {
         var page = pagedData.fetch({ index: pageRange.index });
         page.data.forEach(function(result) {
 
             var companyName = result.getValue('companyname');
 
             // Skip if the company name is empty
             if (companyName) {
                 customers.push({
                     id: result.getValue('internalid'),
                     name: companyName
                 });
             }
         });
     });
 
     return customers;
 }
 
 
     //Function to search default customer
     function searchDefaultCustomer(customerName)
     {
 
         try {
             // Create the search
             var customerSearch = search.create({
                 type: search.Type.CUSTOMER,
                 columns: ['internalid'],
                 filters: [
                     ['companyname', 'is', customerName] // Exact match for customer name
                 ]
             });
     
             // Run the search and get the first result
             var searchResult = customerSearch.run().getRange({
                 start: 0,
                 end: 1 // Only need the first result
             });
     
             // Check if a result was found
             if (searchResult.length > 0) {
                 var customerId = searchResult[0].getValue('internalid');
                 return customerId; // Return the internal ID of the customer
             } else {
                 return null; // Customer not found
             }
         } catch (e) {
             log.error({
                 title: 'Error in getCustomerIdByName',
                 details: e.message
             });
             return null;
         }
 
     }
     function fetchSpecbook(sId)
     {
      try {
             // Define the API URL
             var apiUrl = '';
             // Set up the HTTP headers
             var headers = {
                 'Content-Type': 'application/json'
             };
             // Make the GET request to the external API
             var response = https.get({
                 url: apiUrl,
                 headers: headers
             });
             // Parse and log the response
             var responseBody = JSON.parse(response.body);
             return responseBody;
         } catch (e) {
             log.error({
                 title: 'Error making API request',
                 details: e
             });
         }
     }
 
 
     // Function to get Sales Order ID from the custom record
 function getSalesOrderId(specbookId, orderId, modelNo) {
     try {
         var customRecordSearch = search.create({
             type: 'customrecord1050', // Replace with your custom record type ID
             columns: ['custrecord2','custrecord5'], // Replace with the actual field for Sales Order ID
             filters: [
                 ['custrecord_spec_id', 'is', specbookId],
                 'AND',
                 ['custrecord6', 'is', orderId],
                 'AND',
                 ['custrecord4', 'is', modelNo]
             ]
         });
 
         var result = customRecordSearch.run().getRange({
             start: 0,
             end: 1
         });
 
         if (result.length > 0) {
             return {
                 salesOrderId: result[0].getValue('custrecord2'), // Sales Order ID
                 custCustomerName: result[0].getValue('custrecord5') // Customer ID
             };
             //return result[0].getValue('custrecord2'); // Replace with the field ID for Sales Order ID
         }
 
         return null;
     } catch (e) {
         log.error({
             title: 'Error in getSalesOrderId',
             details: e
         });
         return null;
     }
 }
     return {
         onRequest: onRequest
     };
 });
 

本文标签: