Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to format Phone Number variable

Teri Bobst
Mega Guru

I have a variable in our mobile service workflow for our MDM to record the number of the new service. I also have a business rule that will then populate the mobile number field on the user record when this task is closed. The problem is that it populates that field exactly as it is entered (even though the type on that field is phone number and if you just type it in there it will format correctly).

So, I need a way either in my variable, business rule, or elsewhere to maintain the phone number format (xxx) xxx-xxxx even when xxxxxxxxxx or xxx xxx xxxx is entered in the variable.

Does anyway know of a way to handle this? I will include my business rule below:

function onBefore(current, previous) {

    //This function will be automatically called when this rule is processed.

    var gr = new GlideRecord('sys_user');

  gr.addQuery('sys_id', current.request.requested_for);

  gr.query();

  if (gr.next()){

  gs.addInfoMessage("SUCCESS: Found User!");

  gs.addInfoMessage("Mobile # is: " + current.variable_pool.mobile_number);

  gr.mobile_phone = current.variable_pool.mobile_number;

  gr.update();

  } else {

  gs.addInfoMessage("ERROR: Could not find user record: " + current.request.requested_for.name);

  }

}

Thanks,

Teri

1 ACCEPTED SOLUTION

TJW2
Mega Guru

The following will format a string input as xxxxxxxxxx into (xxx)xxx-xxxx



var inputPhone = current.variables.mobile_number;


var newPhone = '('+inputPhone.substr(0,3)+')'+inputPhone.substr(3,3)+'-'+inputPhone.substr(6,4);



On the catalog item I would verify the input field is 10 long and numeric using an OnChange client script:



if( (newValue != "") && (/[0-9]{10}/i.exec(newValue) > -1) ) {


      alert("The phone number must be in this format: 1234567890.\n");


      g_form.setValue('field_name','');


}


View solution in original post

9 REPLIES 9

TJW2
Mega Guru

The following will format a string input as xxxxxxxxxx into (xxx)xxx-xxxx



var inputPhone = current.variables.mobile_number;


var newPhone = '('+inputPhone.substr(0,3)+')'+inputPhone.substr(3,3)+'-'+inputPhone.substr(6,4);



On the catalog item I would verify the input field is 10 long and numeric using an OnChange client script:



if( (newValue != "") && (/[0-9]{10}/i.exec(newValue) > -1) ) {


      alert("The phone number must be in this format: 1234567890.\n");


      g_form.setValue('field_name','');


}


Teri,



Did this approach work for you?   Please mark this post as 'Answered' , or supply additional information.



Thank You


Hi Terri, the script to set the format worked perfectly, thank you!



On the validation by client script, I have an issue. The variable is a workflow variable and therefore not available to select on the catalog client script item.



Do you know how I could get around this?



Thanks,


Teri


You should be able to use:     var input = workflow.variables.variableName



Where are you trying to validate?   On the Catalog Item?  


it may be better to start a new post since this one was marked as 'Answered'.   You will get more responses.