I need to truncate a single line text variable to 35 characters

WarrenB
Tera Expert

I have created a Single Line Text variable called "Street".  It has a max_length=35.

If I type (or paste) a value that is longer that 35 characters, it will truncate (as I expect) the string.

I have a catalog client script that is setting the value by getting the street value from the sys_user form and setting it here.  The problem is that it seems to ignore the max length attribute of the field.  Not only does it display the entire value, but on submit, it saves the over length value. 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('atfRequestUtils');
    ga.addParam('sysparm_name', 'requestorInfo');
    ga.addParam('sysparm_requested_for', g_form.getValue('requestor'));
    ga.getXML(atfRequestUtilsParse);

    function atfRequestUtilsParse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var userInfo = answer.split("||");
        g_form.setValue('street', userInfo[5]);
        g_form.setValue('city', userInfo[6]);
        g_form.setValue('state', userInfo[7]);
        g_form.setValue('zip', userInfo[8]);
        g_form.setValue('country', userInfo[11]);
        g_form.setValue('phone', userInfo[3]);

    }
}

How do I get my script to trim off the addl characters?

Thank you for any suggestions!  Scripting is not my strong suit.

1 ACCEPTED SOLUTION

Mahendra RC
Mega Sage

Hello @WarrenB 

Could you please check with below script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('atfRequestUtils');
    ga.addParam('sysparm_name', 'requestorInfo');
    ga.addParam('sysparm_requested_for', g_form.getValue('requestor'));
    ga.getXML(atfRequestUtilsParse);

    function atfRequestUtilsParse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var userInfo = answer.split("||");
        g_form.setValue('street', userInfo[5].substring(0,35));
        g_form.setValue('city', userInfo[6]);
        g_form.setValue('state', userInfo[7]);
        g_form.setValue('zip', userInfo[8]);
        g_form.setValue('country', userInfo[11]);
        g_form.setValue('phone', userInfo[3]);

    }
}

If my answer helped you in any way then please do mark it as helpful. If it answered your question then please mark it correct and helpful. This will help others with similar issue to find the correct answer.

Thanks

View solution in original post

4 REPLIES 4

Zach Koch
Giga Sage
Giga Sage

The following post may help you, it has a script to limit the characters in your field

Post about character limits

If this solved your issue, please mark this response correct and helpful, thank you!

 

If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!

Thanks Zach.  Interesting stuff, but what I really need is to make sure that when I set the value, it only uses the first 35 characters (lopping off anything more).

The problem I'm trying to solve is that we built a FedEx integration to create shipping labels.  FedEx uses multiple "streetlines" for the address.  These lines cannot be longer then 35 char.  But ServiceNow has used a larger format on the sys_user form for street.  So, we have to chop it off and prompt the user to populate 2 streetline variable (street and street2).

It's a quandry!

Mahendra RC
Mega Sage

Hello @WarrenB 

Could you please check with below script:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var ga = new GlideAjax('atfRequestUtils');
    ga.addParam('sysparm_name', 'requestorInfo');
    ga.addParam('sysparm_requested_for', g_form.getValue('requestor'));
    ga.getXML(atfRequestUtilsParse);

    function atfRequestUtilsParse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        var userInfo = answer.split("||");
        g_form.setValue('street', userInfo[5].substring(0,35));
        g_form.setValue('city', userInfo[6]);
        g_form.setValue('state', userInfo[7]);
        g_form.setValue('zip', userInfo[8]);
        g_form.setValue('country', userInfo[11]);
        g_form.setValue('phone', userInfo[3]);

    }
}

If my answer helped you in any way then please do mark it as helpful. If it answered your question then please mark it correct and helpful. This will help others with similar issue to find the correct answer.

Thanks

Thanks Mahendra!  That did the trick.