appending country code to phone numbers based on location

Sript 2.0 Code used to obtain the values from contact record and update the phone number field by appending the country code at beginning based on the location then add it in to the contact record. Using this field we also sending email

/** 

 * @NApiVersion 2.x 

 * @NScriptType workflowactionscript 

 */ 

/******************************************************************************* 

 * CLIENTNAME:Thermogard 

 *  

 *  

 * WORK FLOW ACTION SCRITP FOR Special Priced Item setting 

 * 

 * 

 * ************************************************************************** 

 * Date : 29-03-2022 

 * 

 * Author: JOBIN AND JISMI IT SERVICES LLP 

 *  

 *  

 * Script Description : 

 *  

 * Date created :29-03-2022 

 * 

 * REVISION HISTORY 

 * 

 * 

 *  

 ******************************************************************************/ 

 
 

 define(['N/record', 'N/search', 'N/ui/serverWidget', 'N/url', 'N/runtime', 'N/email', 'N/render'], 

 
 

 function (record, search, serverWidget, url, runtime, email, render) { 

 
 
 
 

     /***************************************************************************************** 

     Main Function  

     *****************************************************************************************/ 

     var main = { 

         /***************************************************************************************** 

         Entry Point 

         *****************************************************************************************/ 

         onAction: function (scriptContext) { 

             log.debug('scriptContext', scriptContext) 

             try { 

 
 
 

                 var newRecId = scriptContext.newRecord.id; 

                 log.debug("newRecId", newRecId) 

                 var coRec = record.load({ 

                     type: "contact", 

                     id: newRecId, 

                     isDynamic: false 

                 }) 

                 var region = coRec.getValue({ 

                     fieldId: "custentity5" 

                 }) 

                 log.debug("region", region) 

                 var Name = coRec.getValue({ 

                     fieldId: "nameorig" 

                 }) 

                 var Email = coRec.getValue({ 

                     fieldId: "email" 

                 }) 

                 var Phone = coRec.getText({ 

                     fieldId: "phone" 

                 }) 

                 var location = coRec.getText({ 

                     fieldId: "custentity5" 

                 }) 

                 //[phone] 

                 var flag = false; 

                 if (Phone[0] == "+") { 

                     if (Phone[1] == "6") { 

                         if (Phone[2] == "4") { 

                             flag = true; 

                         } 

                     } 

                 } 

                 function truncate(Phone){ 

                     var r = ""; 

                     for(var i = 3; i < Phone.length; i++){ 

                       r += Phone[i]; 

                     } 

                     return r; 

                   } 

                 if (region == "1") { 

                     coRec.setValue({ 

                         fieldId: 'subsidiary', 

                         value: 4 

                     }); 

                 } 

                 if (region == "2") { 

                     coRec.setValue({ 

                         fieldId: 'subsidiary', 

                         value: 1 

                     }); 

                     if (flag == true){ 

                     Phone = "+61"+ truncate(Phone); 

                     coRec.setValue({ 

                         fieldId: 'phone', 

                         value: Phone 

                     }); 

                     } 
                 } 

                 if (region == "3") { 

                     coRec.setValue({ 

                         fieldId: 'subsidiary', 

                         value: 7 

                     }); 

                     if (flag == true){   

                        Phone = "+1"+ truncate(Phone); 

                         coRec.setValue({ 

                             fieldId: 'phone', 

                             value: Phone 

                         }); 

                         } 
                 } 
                 coRec.save(); 

                 //to get the value of industry 

                 var Companyid = coRec.getValue({ 

                     fieldId: "company" 

                 }) 

                 var custrec = record.load({ 

                     type: "customer", 

                     id: Companyid, 

                     isDynamic: false 

                 }) 

                 log.debug("newcustomer", custrec) 

                 var Industry = custrec.getText({ 

                     fieldId: "custentity_industry" 

                 }) 
                 if (region == "1") { 

                     custrec.setValue({ 

                         fieldId: 'subsidiary', 

                         value: 4, 

                     }); 
                 } 

                 if (region == "2") { 

                     custrec.setValue({ 

                         fieldId: 'subsidiary', 

                         value: 1, 
                     }); 

                     if (flag == true){ 

                         custrec.setValue({ 

                             fieldId: 'phone', 

                             value: Phone 

                         }); 

                        } 
                 } 

                 if (region == "3") { 

                     custrec.setValue({ 

                         fieldId: 'subsidiary', 

                         value: 7, 
                     }); 

                     if (flag == true){ 

                         custrec.setValue({ 

                             fieldId: 'phone', 

                             value: Phone 
                         }); 

                         } 
                 } 

                 custrec.save(); 

                 var html = "<p>Hi,</p><p>A Contact record has been created through online custom form.</p><p>Name:" + Name + "</p><p>Email:" + Email + "</p><p>Phone:" + Phone + "</p><p>Location:" + location + "</p><p>Thank you</p>" 

                 region = region ? region[0] : null; 

                 log.debug("region", region) 

                 if (region == "1") { 

                     log.debug("emailsent123") 

                     email.send({ 

                         author: 6304, 

                         recipients: 'sales.nz@thermogard.com', 

                         subject: "Contact record is created", 

                         body: html, 

                     }) 

                     log.debug("emailsent") 

                 } 

                 if (region == '2') { 

                     email.send({ 

                         author: 6304, 

                         recipients: 'sales.au@thermogard.com', 

                         subject: "Contact record is created", 

                         body: html, 

                     }) 

                 } 

                 if (region == '3') { 

                     email.send({ 

                         author: 6304, 

                         recipients: 'sales.us@thermogard.com', 

                         subject: "Contact record is created", 

                         body: html, 

                     }) 

                 } 

             } catch (e) { 

                 log.debug('error@onAction', e) 

                 log.error('error@onAction', e) 

             } 
         } 

     } 

     for (var key in main) { 

         if (typeof main[key] === 'function') { 

             main[key] = trycatch(main[key], key); 

         } 

     }; 

     function trycatch(myfunction, key) { 

         return function () { 

             try { 

                 return myfunction.apply(this, arguments); 

             } catch (e) { 

                 log.debug("e in  " + key, e); 

             } 

         } 

     }; 

     return main; 

 }); 

 

Leave a comment

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