Auto-populate PO fields from Drop Ship SOs

Jira Code : AD-24

When a drop ship Purchase Oder (PO) is created from a Sales Order (SO), the following info needs to populate on the PO:

  1. SO Customer Service Rep -> PO Requester
  2. SO Planned Ship Date -> PO Due Date
  3. SO Incoterm -> PO Incoterm



User Event : AD-24 JJ UE SO to PO

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
* CLIENTNAME:Advantus 
* AD-24
* Auto Populate PO fields from SO
* **************************************************************************
* Date : 03-01-2019
*
* Author: Jobin & Jismi IT Services LLP
* Script Description : This script is to autopopulate the following:
*   1. SO Customer Service Rep -> PO Requester
*   2. SO Planned Ship Date -> PO Due Date
*   3. SO Incoterm -> PO Incoterm
* Date created : 03-01-2019
*
* REVISION HISTORY
*
* Revision 1.0 ${03-01-2019} nd : created
*
*
******************************************************************************/
define(['N/record','N/search'],

function(record,search) {

    /**
     * Function definition to be triggered before record is loaded.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.newRecord - New record
     * @param {Record} scriptContext.oldRecord - Old record
     * @param {string} scriptContext.type - Trigger type
     * @Since 2015.2
     */
    function afterSubmit(scriptContext) {


    	try{
    		var purchaseOrder =  scriptContext.form;
    		var idPurchase = scriptContext.newRecord
    		var id_PO = idPurchase.id 

		var purchaseorderSearchObj = search.create({
			type: "purchaseorder",
			filters:
				[
					["type","anyof","PurchOrd"], 
					"AND", 
					["internalid","anyof",id_PO], 
					"AND", 
					["mainline","is","T"]
					],
					columns:
						[
							search.createColumn({name: "createdfrom", label: "Created From"}),
							search.createColumn({name: "internalid", label: "Internal ID"}),
							search.createColumn({
								name: "custbody_adv_cust_service_rep",
								join: "createdFrom",
								label: "Customer Service Rep"
							}),
							search.createColumn({
								name: "custbody_adv_planned_shipdate",
								join: "createdFrom",
								label: "Planned Ship Date"
							}),
							search.createColumn({
								name: "custbody_adv_so_incoterm",
								join: "createdFrom",
								label: "Sales Order Incoterm"
							})
							]
			});
            var searchResult = purchaseorderSearchObj.run().getRange({
					start: 0,
					end: 100
				});

             for (var j = 0; j<searchResult.length; j++) {
                  var cusmtrService_rep = searchResult[j].getValue({
					  name: "custbody_adv_cust_service_rep",
                        join: "createdFrom"
					});

				 var shipDate = searchResult[j].getValue({
					  name: "custbody_adv_planned_shipdate",
                        join: "createdFrom"
					});

                 var incoTerm = searchResult[j].getValue({
					  name: "custbody_adv_so_incoterm",
                     join: "createdFrom"
					});
                
             }
    		var isd = record.submitFields({
                type: record.Type.PURCHASE_ORDER,
                    id: id_PO,
                     values: {
                         incoterm: incoTerm,
                         duedate:shipDate,
                         custbody_nsts_gaw_tran_requestor:cusmtrService_rep
                     },
                     options: {
                            ignoreMandatoryFields : true
                    }
    		});

    	}catch(err){
    		log.error('error',err)
    	}
    	

    

    
    }

    return {
        afterSubmit: afterSubmit
    };
    
});

Leave a comment

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