JSON query and return to populate CI data

Alex307
Kilo Guru

Hey folks,

I'm working on a client script/AJAX script include that retrieves data from an existing CI record, builds an object with relevant data, then populates the catalog request form. I'm just learning how to do this so I've got it written the long way to make sure I understand what I'm doing before cleaning up the script.

I'm not sure how to define key/value pairs WITHIN the created object so that I can dot-walk to the data in my client script field setters. My question concerns the last function within the script include '_getApplData'. I do not have anything in the client script yet to use the data held within that function.

I'm also having difficulties clearing list collectors onChange within my existing script. I don't have anything that clears the selection at the moment, but I would appreciate any suggestions on that. I've tried Moving List Collector Options but I can't figure out how/when to apply the script so it only triggers with a new value, then runs the rest of the retrieval script.

 

Most of the comments are for my own follow-up.

 

Thank you in advance for any suggestions!

 

Script Include

// Script Include 

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

    //functionName: function() {

    //},

	/**
	 * Return all details from the CI record in a JSON format.
	 * 
	 * Used for item 'CMDB/SACM - APPLICATION Configuration Item (CI) Request'
     * When a user selects a CI to UPDATE, build a JSON object to hold relevant CI data
     * so that we can parse it via the client script to populate the form.
	 catalog item/when its used/what we're returning and how we're using it
	 * 
	 * @return {string} JSON string
	 */
    getAllCIDetails: function () {
        // Return Object
        var result = {};

        // Get passed in parameters
        var ciID = this.getParameter('sysparm_ci_id');
        var GRU = new GlideRecordUtil();

        // Get the CI gliderecord
        var grCI = GRU.getCIGR(ciID);

        // Only proceed if we have a valid glideRecord
        if (grCI.isValidRecord()) {

            // Get CI field values


			/*	var ciFields = [
				"name",
				"location"
			];

			// Loop through the defined fields
			for (var i = 0; i < ciFields.length; i++) {
				// Return the values to the result object
				result[ciFields[i]] = grCI.getValue(ciFields[i]) || '';
			} 
			*/

            // objectName.keyName = glideRecord.getValue(GRVariable)
            result.vs_ci_support_lvl = grCI.getValue('u_support_level') || '';
            result.vs_ci_criticality = grCI.getValue('u_ci_criticality') || '';
            result.vs_ci_env_class = grCI.getValue('u_env_class') || '';
            result.vs_ci_env_subclass = grCI.getValue('u_env_subclass') || '';
            result.vs_ci_name_full = grCI.getValue('u_name_full') || '';
            result.vs_ci_location = grCI.getValue('location') || '';
            result.vs_ci_change_control = grCI.getValue('u_control_change') || '';
            result.vs_ci_bus_unit = grCI.getValue('u_business_unit') || '';
            result.vs_ci_desc = grCI.getValue('short_description') || '';
            result.vs_ci_subtype = grCI.getValue('u_ci_subtype') || '';
            result.vs_ci_subclass = grCI.getValue('u_ci_subclass') || '';
            result.vs_ci_type = grCI.getValue('u_ci_type') || '';
            result.vs_ci_schedule = grCI.getValue('maintenance_schedule') || '';
            result.vs_ci_sensitive_data = grCI.getValue('u_sensitive_data') || '';
            result.vs_ci_owned_by = grCI.getValue('owned_by') || '';
            result.vs_ci_default_group = grCI.getValue('assignment_group') || '';
            result.vs_ci_managed_by = grCI.getValue('managed_by') || '';
            result.vs_ci_disruptive_chg_grp = grCI.getValue('u_disruptive_change_notification_group') || '';

            /*                  
                                CI FIELDS TO CLARIFY

			result.vs_ci_abbr = grCI.getValue('CI ABBREVIATION FIELD') || '';
			result.vs_ci_bus_mgr = grCI.getValue('CI BUS REL MANAGER FIELD') || '';
			result.vs_support_group_t1 = grCI.getValue('CI T1 SUPPORT GROUP FIELD') || '';
			result.vs_ci_bus_svc_list = grCI.getValue('CI BUS SVCS') || '';
			result.vs_ppsid = grCI.getValue('CI PPSID') || '';
			result.vs_ppsid_ci = grCI.getValue('sys_id') || '';
			result.vs_ci_group_t2_t3 = grCI.getValue('CI T2/T3 SUPPORT GROUP FIELD') || '';
			result.vs_cust_appr = grCI.getValue('CI CUSTOMER APPROVERS') || '';
			result.vs_qa_contact = grCI.getValue('CI QA CONTACTS') || '';
			result.vs_add_chg_appr = grCI.getValue('CI ADD REQ CHG APPROVERS') || '';
			result.vs_sme = grCI.getValue('CI TECHNICAL SME') || '';
            */


            // Get business services to populate list collector vs_ci_bus_svc_list
            result.vs_ci_bus_svc_list = this._getBusSvcs(ciID);
            result.ci_application_data = this._getApplData(ciID);
        }

        // Return JSON string
        davLog(result, "Alex");
        return JSON.stringify(result);
    },

    _getBusSvcs: function (ciID) {
        // This will get the business services where the selected CI to update is the child and add it to the result object
        var busServiceList = [];

        var grBS = new GlideRecord('cmdb_rel_ci');
        grBS.addEncodedQuery('type.nameSTARTSWITHdepends on^child.sys_class_name=u_cmdb_ci_appl_master_inst^child.install_status=1^child=' + ciID);
        grBS.query();

        while (grBS.next()) {
            var parentBS = busServiceList.push(grBS.parent.sys_id.toString());
        }

        return busServiceList;
    },

    _getApplData: function (ciID) {
        // This will get child application data and add it to the result object

        result.ci_application_data = {
            vs_ci_abbr : '',
            vs_support_group_t1 : '',
            vs_ci_group_t2_t3 : ''
        };
       // .vs_ci_abbr = '';
       // result.ci_application_data.vs_support_group_t1 = '';
       // result.ci_application_data.vs_ci_group_t2_t3 = '';


        var grAppl = new GlideRecord('cmdb_ci_appl');
        grAppl.addEncodedQuery('sys_id=' + ciID);
        grAppl.query();

        if (grAppl.next()) {
            result.ci_application_data.vs_ci_abbr = grAppl.getValue('u_abbreviated_name') || '';
            result.ci_application_data.vs_support_group_t1 = grAppl.getValue('u_support_group_tier1') || '';
            result.ci_application_data.vs_ci_group_t2_t3 = grAppl.getValue('support_group') || '';
        }

        return result.ci_application_data;
    },


    type: 'u_ApplicationCIRequest'
});

 

Client Script

// Client Script

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

    // Make sure this is to UPDATE the CI
    if (g_form.getValue('vs_request_type') == 'update') {
        console.log('*aw Request Type: ' + g_form.getValue('vs_request_type'));
        console.log('*aw About to call AJAX');

        var ga = new GlideAjax('u_ApplicationCIRequest');
        ga.addParam('sysparm_name', 'getAllCIDetails');
        ga.addParam('sysparm_ci_id', newValue);
        ga.getXMLAnswer(function (answer) {

            // Parse JSON response
            var ciData = JSON.parse(answer);
            console.log('*aw');
            console.log(ciData);

            // Set the regular field values on the form
            // g_form.setValue('varName', objectName.keyName);
            g_form.setValue('vs_ci_location', ciData.vs_ci_location);
            g_form.setValue('vs_ci_support_lvl', ciData.vs_ci_support_lvl);
            g_form.setValue('vs_ci_criticality', ciData.vs_ci_criticality);
            g_form.setValue('vs_ci_env_class', ciData.u_env_class);
            g_form.setValue('vs_ci_env_subclass', ciData.vs_ci_env_subclass);
            g_form.setValue('vs_ci_name_full', ciData.vs_ci_name_full);
            g_form.setValue('vs_ci_change_control', ciData.vs_ci_change_control);
            g_form.setValue('vs_ci_bus_unit', ciData.vs_ci_bus_unit);
            g_form.setValue('vs_ci_desc', ciData.vs_ci_desc);
            g_form.setValue('vs_ci_subtype', ciData.vs_ci_subtype);
            g_form.setValue('vs_ci_subclass', ciData.u_ci_subclass);
            g_form.setValue('vs_ci_type', ciData.vs_ci_type);
            g_form.setValue('vs_ci_schedule', ciData.vs_ci_schedule);
            g_form.setValue('vs_ci_sensitive_data', ciData.vs_ci_sensitive_data);
            g_form.setValue('vs_ci_owned_by', ciData.vs_ci_owned_by);
            g_form.setValue('vs_ci_default_group', ciData.vs_ci_default_group);
            g_form.setValue('vs_ci_managed_by', ciData.vs_ci_managed_by);
            g_form.setValue('vs_ci_disruptive_chg_grp', ciData.vs_ci_disruptive_chg_grp);

            
            // Set the Business Services list collector
            // Name of variable to move options from
            var varName = 'vs_ci_bus_svc_list';
            var leftBucket = gel(varName + '_select_0');
            var rightBucket = gel(varName + '_select_1');
            //var selectedOptions = leftBucket.options;
            var selectedOptions = ciData.vs_ci_bus_svc_list;
            // Get an array of all option IDs to move
            var selectedIDs = new Array();
            var index = 0;
            for (var i = 0; i < selectedOptions.length; i++) {
                selectedIDs[index] = i;
                index++;
            }

            // Move all returned options from left to right bucket and sort the results
            // Switch 'rightBucket' and 'leftBucket' to move right to left
            moveSelectedOptions(selectedIDs, leftBucket, rightBucket, '--None--');
            // Sort the resultant options in the right bucket
            sortSelect(rightBucket);


            // Add other setters for the fields I'm not sure of (abbreviation/BRM/PPSID, etc.)

            // FOR ME -Use the script to move options from left to right for list collectors
        });
    }
}

 

 

1 ACCEPTED SOLUTION

vkachineni
Kilo Sage
Kilo Sage

You will have to pass result to the function getApplData as result is local to getAllCIDetails

result.ci_application_data = this._getApplData(ciID, result);

 

update the function to receive second parameter

_getApplData: function (ciID, result) {

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

View solution in original post

2 REPLIES 2

vkachineni
Kilo Sage
Kilo Sage

You will have to pass result to the function getApplData as result is local to getAllCIDetails

result.ci_application_data = this._getApplData(ciID, result);

 

update the function to receive second parameter

_getApplData: function (ciID, result) {

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Thank you, I knew I was missing something simple! That did the trick.