update CI when related asset's model is modified

dev_K
Tera Contributor

Hi all,

 

 

I need a solution for the following scenario:

 

 

1. Asset display name is a combo of a asset tag and model.

2. Related CI's name is the same as the asset

 

dev_K_0-1716903323808.png

 

 

3. I need a business rule that will adapt the CI's display name when user changes asset's model.

 

 

As you can see below; I have change the model and so asset's display name changed but CI remains the same.

 

 

dev_K_1-1716903606059.png

 

 

 

 

 

 

I also want to be sure that this name will be updated only when the asset's display name will be the same as the CI.

If it is different nothing should happen.

 

 

Thanks for all the tips!

 

 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @dev_K ,

This is doable, please find the below BR-

(function executeRule(current, previous /*null when async*/) {
    // display name combining asset and model
    var DisplayName = current.asset_tag + ' - ' + current.model_id.getDisplayValue();

    // Update the display name
    if (current.display_name != DisplayName) {
        current.display_name = DisplayName;
        current.update();
    }
    if (current.model_id != previous.model_id || current.asset_tag != previous.asset_tag) {
        var ciGr = new GlideRecord('cmdb_ci');
        ciGr.addQuery('asset', current.sys_id);
        ciGr.query();
        while (ciGr.next()) {
            if (ciGr.name == previous.display_name || ciGr.name == current.display_name) {
                ciGr.name = DisplayName;
                ciGr.update();
            }
        }
    }
})(current, previous);

 

Modify the above based on other validations as per your requirement.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

Hi @dev_K ,

This is doable, please find the below BR-

(function executeRule(current, previous /*null when async*/) {
    // display name combining asset and model
    var DisplayName = current.asset_tag + ' - ' + current.model_id.getDisplayValue();

    // Update the display name
    if (current.display_name != DisplayName) {
        current.display_name = DisplayName;
        current.update();
    }
    if (current.model_id != previous.model_id || current.asset_tag != previous.asset_tag) {
        var ciGr = new GlideRecord('cmdb_ci');
        ciGr.addQuery('asset', current.sys_id);
        ciGr.query();
        while (ciGr.next()) {
            if (ciGr.name == previous.display_name || ciGr.name == current.display_name) {
                ciGr.name = DisplayName;
                ciGr.update();
            }
        }
    }
})(current, previous);

 

Modify the above based on other validations as per your requirement.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

(function executeRule(current, previous /*null when async*/) {

    // Function to normalize display name and CI formats for comparison
    function normalize(str) {
        return str.replace(' - ', ',').replace(' ', '');
    }

    // Get current display name and CI values
    var displayName = current.display_name;
    var ci = current.ci;

    // Normalize values to compare
    var normalizedDisplayName = normalize(displayName);
    var normalizedCi = normalize(ci);

    // Check if the normalized display name equals the normalized CI
    if (normalizedDisplayName === normalizedCi) {

        // Extract parts of CI
        var ciParts = ci.split(',');

        // Replace the 'name2' part with the new model name
        ciParts[1] = current.model_id.getDisplayValue();

        // Join the parts to form the new CI
        var newCi = ciParts.join(',');

        // Update the CI field with the new value
        current.ci = newCi;
    }

})(current, previous);
 
 
how about this one? I tested it and i cant make 'configuration item' field change 

Kris Moncada
Tera Guru

I think you'll need a business rule that is triggered when an Asset's Model changes and updates the corresponding CI's model.

(function executeRule(current, previous /*null when async*/) {

	var gr_ci = new GlideRecord('cmdb_ci');
	if(gr_ci.get(current.getValue('ci'))){
		gr_ci.setWorkflow(false);
		gr_ci.setValue('model_id', current.getValue('model'));
		gr_ci.update();
	}

})(current, previous);

 

KristofferMonc_2-1716932856418.png

 

Hope this helps.