Change location, based on customer selection in the invoice and opportunity record.

ACLW-31:: Location script on additional record.

Need to populate the location from entity record, while selecting a customer in the transaction body field (invoice, opportunity). And location will be auto-populated, according to the customer location. We achieve this by using a client script.

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
define(['N/search'],
    function(search) {
        function postSourcing(context) {
            try {
                if (context.fieldId == "entity") { //update location
                    var customer = context.currentRecord.getValue({ fieldId: "entity" });
                    if (customer) {
                        var location = search.lookupFields({
                            id: customer,
                            type: "entity",
                            columns: ["custentity22"]
                        }).custentity22[0].value;
                        if (location)
                            context.currentRecord.setValue({ fieldId: "location", value: location });
                    }
                } else if (context.fieldId == "item" && context.sublistId == "item") //update item location
                    context.currentRecord.setCurrentSublistValue({ sublistId: 'item', fieldId: 'location', value: context.currentRecord.getValue({ fieldId: "location" }), ignoreFieldChange: true });
            } catch (err) {
                console.log(err);
            }
        }

        function fieldChanged(context) {
            try {
                if (context.fieldId == "location" && context.sublistId != "item") { //on body line location change	
                    //	console.log("TEST");
                    var location = context.currentRecord.getValue({ fieldId: "location" });
                    var numLines = context.currentRecord.getLineCount({ sublistId: 'item' });
                    //     try{
                    //    context.currentRecord.setCurrentSublistValue({sublistId:'item',fieldId:'location',value:context.currentRecord.getValue({fieldId:"location"}),ignoreFieldChange:true});            
                    // }catch(err){}

                    for (var i = 0; i < numLines && location; i++) {
                        context.currentRecord.selectLine({ sublistId: 'item', line: i });
                        if (context.currentRecord.getCurrentSublistValue({ sublistId: 'item', fieldId: 'item' })) {
                            context.currentRecord.setCurrentSublistValue({ sublistId: 'item', fieldId: 'location', value: location, ignoreFieldChange: true });
                            context.currentRecord.commitLine({ sublistId: 'item' });
                        }
                    }
                }
            } catch (err) { console.log(err); }
        }


        function pageInit(context) {
            try {
                if (context.mode == 'create') {
                    var customer = context.currentRecord.getValue({ fieldId: "entity" });
                    if (customer) {
                        var location = search.lookupFields({ id: customer, type: "entity", columns: ["custentity22"] }).custentity22[0].value;
                        if (location) context.currentRecord.setValue({ fieldId: "location", value: location });
                    }
                }
            } catch (err) {
                console.log(err);
            }

        }

        return { postSourcing: postSourcing, fieldChanged: fieldChanged, pageInit: pageInit }


    });

Leave a comment

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