UI Macro to set Field Value in Catalog Item

EricG
Kilo Sage

I'm not real familiar with coding for UI Macros.

 

TASK:

I have a form for creating servers.  Need to populate the Server Name based on three fields and whether or not that name has been used before.  Fields are Company, Environment, and Application Short Name.

 

Result:

I'd like to variable to look like this

EricG_1-1714057154356.png

So when the user clicks on the I icon, the field populates with a value. 

The script would company the company name, value of the environment, the aplication short name and then add a 01 to the end.  it would also need to search the cmdb_ci_server class to verify the name and update the number should the name exist.
E.G. COMPANY-P-SMETHG01

 

I have a client script on change function that can do this.
However, the client would like this option better.

 

Here is my client script

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
	
    //Type appropriate comment here, and begin script below
	var cAbbrv = g_form.getValue('sc_sver_app_short');
    if (cAbbrv.length > 7 || cAbbrv.length == 0) {
        confirm("Your abbrevation can not be longer than 7 didgets.  Please reenter.");
        g_form.setValue('sc_sver_app_short', '');
        return;
    }

	var cEnty = g_form.getValue('sc_sver_entity');
	
	var cName = '';
	var cEnvn = g_form.getValue('sc_sver_environment');
	if(cEnty == '9987a66edb1363c0c3fbdd3b5e961946'){//Hilltop
		cName = 'XX1'+cEnvn;
   }
   else if(cEnty == '2239c306db709f047317323b7c9619b1'){//PCB
		cName = "XXX"+cEnvn;
   }
   else if(cEnty == '04490f06db709f047317323b7c961936'){//Securities
		cName = "XX2"+cEnvn;
   }
   else if(cEnty == 'fd290f06db709f047317323b7c961912'){//Prime
		cName = "XX3"+cEnvn;
   }
   else if(cEnty == '2695b2b1db340010c3fbdd3b5e9619e6'){//Diamond A
		cName = "XX4"+cEnvn;
   }
	
    var numb = 1;
    var newName = cName+ cAbbrv;
	newName = newName.toUpperCase();
    var svrChk = new GlideRecord('cmdb_ci_computer');
    //operational_status=1^sys_class_name=cmdb_ci_server^nameSTARTSWITHhth-vp-adfs
    svrChk.addQuery('name', 'STARTSWITH', newName);
    svrChk.query();

    while (svrChk.next()) {
        var existN = svrChk.name.replace(/[0-9]/g, "");
		existN = existN.toUpperCase();
        //console.log("The count is "+existN);
        if (existN == newName) {
            numb = numb + 1;
        }
    }
    if (numb <= 9) {
        g_form.setValue('sc_sver_name', cName.toUpperCase() + cAbbrv.toUpperCase() + "0" + numb);
    } else {
        g_form.setValue('sc_sver_name', cName.toUpperCase() + cAbbrv.toUpperCase() + numb);
    }
}

 

 

Any suggestions

 

0 REPLIES 0