A working example of a Custom Script that retrieves CI data from the CMDB

A working example of a Custom Script that retrieves CI data from the CMDB

Hi Everyone,

I've been designing a Normal Change Request template. One of the features of the template is to provide the Service Owner (the "Owned by" field in the CMDB) given a selected Configuration Item (CI) from the CMDB. When the Change Request is created, the Change Requester selects the CI to be changed (this is a Lookup field). When this happens, the Service Owner user field will be automatically populated and displayed.

I could not find a way to get a Service Owner Lookup field to retrieve the Owned by field (it is not one of the 2 dynamic fields provided), so I decided to use a Custom Script that calls an API to read the CMDB. Since there is very little information on how to do this, I'm sharing it here so others might benefit.

// Custom script to get the selected configuration item.Owned by and use it to set the Service Owner field                                                      

try {

        const peer = await CS.getValue('udf_fields.udf_ref2');
    console.log(peer);
        // Get the lookup field value (returns object with id and name)
        const CI = await CS.getValue('udf_fields.udf_ref1');
        if (CI) {
            // Extract the text/name from the lookup field
            var CIname = CI.name;
        } else {
//            await CS.alert('No CI selected for this change');
        }
    var searchCriteria = {
            "field": "name",
            "condition": "is",
            "value": CIname
    };

    console.log("searchCriteria: " + JSON.stringify(searchCriteria, null, 2));
    // Set parameters for the API call (include pagination to get total count)
    // parameters = {"list_info":{"row_count":"1","start_index":"0","search_criteria":{}}};
    parameters = {"list_info":{"row_count":"10","start_index":"0","search_criteria":searchCriteria}};
    const response = await CS.API.get('/api/v3/cmdb', parameters);
  
    const data = JSON.parse(response);
console.log("Requester Object Data: " + JSON.stringify(data, null, 2));
    const respCode = data.response_status[0].status_code;
    const respTxt = data.response_status[0].status;   

const cmdb = data.cmdb;
console.log("Requester CMDB: " + JSON.stringify(data.cmdb, null, 2));
const ci_type = data.cmdb[0].ci_type;
console.log("Requester CI type: " + JSON.stringify(data.cmdb[0].ci_type, null, 2));
const ci_att = data.cmdb[0].ci_attributes;
console.log("Requester CI attributes: " + JSON.stringify(data.cmdb[0].ci_attributes, null, 2));
const so = data.cmdb[0].ci_attributes.ref_owned_by;
console.log("Requester Owned by: " + JSON.stringify(data.cmdb[0].ci_attributes.ref_owned_by, null, 2));
const name = data.cmdb[0].ci_attributes.ref_owned_by.name;
const nameID = data.cmdb[0].ci_attributes.ref_owned_by.id;

const rows = data.list_info.row_count;
    // await CS.alert(`CI is Owned by: ${name}`);  

    // Define the user object payload
    var targetUser = {
      "id": nameID,           
      "name": name       
    };

    // Set the lookup field Service Owner
    await CS.setValue('udf_fields.udf_ref5', targetUser);
    // await CS.disableField("udf_fields.udf_ref5"); //Doesnt work, add to form rule
console.log("Response is " + respTxt + ", status code is " + respCode + ", row count is " + rows + "Owned by " + name + "Owned by ID" + nameID);
    CS.end();
} catch (error) {
    await CS.alert('<B>Error:</B> Failed to fetch CI');
    CS.abort('Unable to retrieve CMDB data');
}
CS.end();



Regards,
Robert

                        New to ADSelfService Plus?