Eliminate Leading and Trailing Spaces in Fields - Catalog Client Script

alexcharleswort
Tera Expert

Hi all,

I have a need to eliminate leading and following spaces in a field on a catalog item. I can get this to work nicely as a business rule for a table, but I would like to utilize this as a catalog client script on a form.

My string variable name is thirdparty_whofirst

I have tried an onSubmit catalog client script like this and it doesn't seem to work:

function onSubmit() {

    //Type appropriate comment here, and begin script below

  var first = g_form.getValue('thirdparty_whofirst');

  g_form.setValue(first, first.toString().trim());

   

}

Let me know if you guys know where I'm and going wrong.

As always, thanks in advance for your help.

1 ACCEPTED SOLUTION

Whoops, you don't have your field name as the first parameter of setValue...Should have caught that...



Try



g_form.setValue('thirdparty_whofirst', first.trim());  


View solution in original post

12 REPLIES 12

The g_form.setValue function has 2 parameters, the first of which is the field you are modifying and the second is the value.



On the server, if your are doing something like current.field_name.setValue('value') that setValue function on GlideElement objects related to a GlideRecord only has 1 parameter because the field is implied.



Hope that makes sense.


right. so I set: var first = g_form.getValue('thirdparty_whofirst)



so in the g_form.setValue I put 'first' because that is the field I am setting and then I am setting it with first.trim() which is a modified version of thirdparty_whofirst.


is it just a general rule that you can't assign a value to a variable and then reference that variable in the first parameter when it requires two?


Not entirely sure I follow.   Do you have a Catalog Item variable named 'first' (distinct/different from a client script variable)?



The first parameter needs to be a catalog form field (catalog item variable) with that name, not a client side javascript variable (defined with the var keyword in script).


Hi Joe,



I'm trying the same in a OnChange Catalog Client Script:



  var mobile = g_form.getValue('mobile_phone');


  g_form.setValue(mobile, mobile.trim());



But it's not working, what am I doing wrong?



Im trying to remove "spaces" in the mobile number. So if it's like "+45 66 00 000 000" it will be "+456600000000".



Best regards,


The first parameter in the line :




g_form.setValue(mobile, mobile.trim());




should be a string identifying the name of the field to set, i.e.:



g_form.setValue('mobile_phone', mobile.trim());



Whereas you are trying to find a field with a name equal to the value of the variable mobile.