Customer Item Detail record is a unique combination of Item and Customer

Jira Code: AD-21

Each ADV – Customer Item Detail record is a unique combination of Item and Customer. It would not allow the record to be saved if there is already another record with the same combination of Customer and Item Number.

Work Flow Action Script: AD-21 JJ WA Customer Item Combination

/** 
 * @NApiVersion 2.x
 * @NScriptType workflowactionscript
 * /*******************************************************************************
* CLIENTNAME:Advantus 
* AD-21
* Make it so ADV - Customer Item Detail records are unique
* **************************************************************************
* Date : 01-01-2019
*
* Author: Jobin & Jismi IT Services LLP
* Script Description : To Prevent saving of Custom record "ADV - Customer Item Detail records",
 * 				if there exists an Customer - Item Combination.
* Date created : 01-01-2019
*
* REVISION HISTORY
*
* Revision 1.0 ${01-01-2019} nd : created
*
*
******************************************************************************/
define(['N/record', 'N/search'],
/**
 * @param {record} record
 * @param {search} search
 */
function(record, search) {
   
    /**
     * Definition of the Suitelet script trigger point.
     *
     * @param {Object} scriptContext
     * @param {Record} scriptContext.newRecord - New record
     * @param {Record} scriptContext.oldRecord - Old record
     * @Since 2016.1
     */
    function onAction(scriptContext) {
    	
    	try{
    		var filterArray =[];
    		// to get the current record data
    		var currRec = scriptContext.newRecord;
    		var mode = scriptContext.type

    		var recordId = currRec.id
    		//to get the customer
    		var customer = currRec.getValue({
    			fieldId:'custrecord_adv_item_customer'
    		});
    		var item = currRec.getValue({
    			fieldId:'custrecord_adv_item'
    		});

    		// check if customer is empty
    		if(customer)
    			{
    			filterArray.push(["custrecord_adv_item_customer.internalidnumber","equalto",customer]);
    			}
    		else
    			{
    			filterArray.push(["custrecord_adv_item_customer.entityid","isempty",""]);
    			}
    		if(item)
    			{
    			filterArray.push("AND",["custrecord_adv_item.internalidnumber","equalto",item]);
    			}
    		else
    			{
    			filterArray.push("AND",["custrecord_adv_item","anyof","@NONE@"]);
    			}
    		if(recordId){
    			filterArray.push("AND", ["internalid","noneof",recordId]);
    		}
    		// to create search for finding the Customer-Item combination exists
    		var customrecord_adv_customer_item_detailSearchObj = search.create({
    			   type: "customrecord_adv_customer_item_detail",
    			   filters:filterArray
    			  /* [
    			      ["custrecord_adv_item_customer.internalidnumber","equalto",customer], 
    			      "AND", 
    			      ["custrecord_adv_item.internalidnumber","equalto",item]
    			   ]*/,
    			   columns:
    			   [
    			      search.createColumn({name: "custrecord_adv_item_customer", label: "Customer"}),
    			      search.createColumn({name: "custrecord_adv_item", label: "Item"}),
    			      search.createColumn({name: "custrecord_adv_custitem_number", label: "Customer Item Number"})
    			   ]
    			});
    			var searchResultCount = customrecord_adv_customer_item_detailSearchObj.runPaged().count;
    			
    			if(searchResultCount>0)
    				{
    				return false;
    				}
    			else{
    				return true;
    			}
    			
    	}catch(e)
    	{
    		log.error("Err@ FN onAction ",e.message);
    	}

    }

    return {
        onAction : onAction
    };
    
});

Leave a comment

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