Variable inputs should be prepared by combinaton of other variable inputs on the catalog item form

ashwinipani
Tera Contributor

I have some variables on the form - 

1. Application - TESTxxxxx

2. Environment - Dev

3. AWS Account - XYZ -xxxxxxx

4. Region - east-1 - NV

5.HostName - Combination of above variable inputs

 

I want to prepare client script so that hostname input will be prepared based on the combination of other variables.

For ex. hostname - XYZANVTESD01
First 3 letter of AWS account
A is common for all

NV is coming from region

TES - first 3 letters of application variable

D - Environment

01 - this will be incremented each time when user submits this form.

 

Please guide to achive this.

 

Thanks

3 REPLIES 3

vaishali231
Tera Guru

hey @ashwinipani 

 

Try this :


Client Script (onChange)

Type: onChange
Applies to variables: Application, Environment, AWS Account, Region

Script -

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

    var application = g_form.getValue('application');
    var environment = g_form.getValue('environment');
    var awsAccount = g_form.getValue('aws_account');
    var region = g_form.getValue('region');

    if (!application || !environment || !awsAccount || !region) {
        return;
    }

    var awsCode = awsAccount.substring(0, 3).toUpperCase();
    var appCode = application.substring(0, 3).toUpperCase();

    var envMap = {
        'Dev': 'D',
        'Test': 'T',
        'Prod': 'P'
    };

    var regionMap = {
        'east-1 - NV': 'NV'
    };

    var envCode = envMap[environment] || '';
    var regionCode = regionMap[region] || '';

    var ga = new GlideAjax('HostnameSequenceAjax');
    ga.addParam('sysparm_name', 'getNextSequence');
    ga.getXMLAnswer(function(sequence) {
        if (!sequence) {
            return;
        }

        var hostname = awsCode + 'A' + regionCode + appCode + envCode + sequence;
        g_form.setValue('hostname', hostname);
    });
}

Script Include (Auto Increment Number)

Name: HostnameSequenceAjax
Client callable: true

Script -

var HostnameSequenceAjax = Class.create();
HostnameSequenceAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getNextSequence: function () {
        var gr = new GlideRecord('sys_properties');
        gr.addQuery('name', 'hostname.sequence.counter');
        gr.query();

        var counter = 1;

        if (gr.next()) {
            counter = parseInt(gr.getValue('value'), 10) + 1;
            gr.setValue('value', counter);
            gr.update();
        } else {
            gr.initialize();
            gr.setValue('name', 'hostname.sequence.counter');
            gr.setValue('value', counter);
            gr.insert();
        }

        return (counter < 10 ? '0' : '') + counter;
    }
});

 

*************************************************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.

Regards
Vaishali Singh

Ankur Bawiskar
Tera Patron

@ashwinipani 

whenever user raises request and you are running server side code to generate the host name, query earlier RITMs belonging to this catalog item, grab the variable values, then extract the number 01 from that variable, increment and then set

What did you start and where are you stuck?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@ashwinipani 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader