Add State Name in Address Record

Jira Code: ME-174

Add State Name in address Record. When saving the customer record or vendor record we need to update the full state name in the address record as the state name only shows some code. For that first add a custom field in address record and customize the corresponding address form to include that field. Then a user event script is run in the background to update the field according to the corresponding state.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/*******************************************************************************
 * CLIENTNAME:Mangalam
 * ME-174
 * State in Address
 *************************************************************************
 * Date : 29-04-2019
 *
 * Author:Jobin and Jismi IT services LLP
 * Script Description : State in address
 * Date created : 29-04-2019
 *
 * REVISION HISTORY
 *
 * Revision 1.0 ${29-04-2019} nd :marg created
 * 
 * 
 *
 ******************************************************************************/
define(['N/record', 'N/search','N/error'],

    function(record, search,error) {

        function afterSubmit(scriptContext) {

            try {

           var customerRec = scriptContext.newRecord.id;
           var recType = scriptContext.newRecord.type;

           log.debug('customerRec',customerRec)

         	log.debug('recType',recType)
         	 var loadRec = record.load({
          	 type:recType, 
          	 id: customerRec,
         	 isDynamic: true
        	 });

         log.debug('loadRec',loadRec)


		var numLines = loadRec.getLineCount({
  		 sublistId: 'addressbook'
			});
		log.debug('numLines',numLines)


		for(i=0;i<numLines; i++){
var subrec = loadRec.selectLine({
    sublistId: 'addressbook',
    line: i
});
log.debug('subrec',subrec)
			  
            	var objSubrecord = loadRec.getCurrentSublistSubrecord({
    sublistId: 'addressbook',
    fieldId: 'addressbookaddress'
});
log.debug('objSubrecord',objSubrecord)
            	var state = objSubrecord.getValue('state');
			
				log.debug('state',state)

				statename = { }

				statename.35 = "Andaman & Nicobar Islands"
				statename.37 = "Andhra Pradesh"
				statename.12 = "Arunachal Pradesh"
				statename.18 = "Assam"
				statename.10 = "Bihar"
				statename.4 = "Chandigarh"
				statename.22 = "Chhattisgarh"
				statename.26 = "Dadra & Nagar Haveli"
				statename.25 = "Daman & Diu"
				statename.7 = "Delhi"
				statename.30 = "Goa"
				statename.24 = "Gujarat"
				statename.6 = "Haryana"
				statename.2 = "Himachal Pradesh"
				statename.1 = "Jammu & Kashmir"

					statename.20 = "Jharkhand"
					statename.29 = "Karnataka"
					statename.32 = "Kerala"
					statename.31 = "Lakshdweep"
					statename.23 = "Madhya Pradesh"
					statename.27 = "Maharashtra"

							statename.14 = "Manipur"
							statename.17 = "Meghalaya"
							statename.15 = "Mizoram"
							statename.13 = "Nagaland"
							statename.21 = "Odisha"
							statename.99 = "Other Countries"
					statename.97 = "Other Territory"
					statename.34 = "Puducherry"
					statename.3 = "Punjab"
					statename.8 = "Rajasthan"
					statename.11 = "Sikkim"
					statename.33 = "Tamil Nadu"

							statename.36 = "Telangana"
							statename.16 = "Tripura"
							statename.9 = "Uttar Pradesh"
							statename.5 = "Uttarakhand"
							statename.19 = "West Bengal"
							




var stateValue = state.split("-")

 
stateValue = stateValue[0]
log.debug('stateValue',stateValue)



	
		var stateText = objSubrecord.setValue({
                    fieldId: 'custrecord_jj_state_full_name',
                    value: statename.stateValue
                });
		
				log.debug('stateText',stateText)



//subrec.commitSubrecord('addressbook');
              loadRec.commitLine({
    sublistId: 'addressbook'
});
                }
                var recordID = loadRec.save({
                    enableSourcing: true,
                    ignoreMandatoryFields: true
                });
            }
               


             catch (e) {

                log.debug('error', e.message)


            }



        }


        return {

            afterSubmit: afterSubmit

        };

    });

Leave a comment

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