How to Parent field value based on Child records field value

Sagar S
Tera Contributor

Hi ,

 

We have a requirement to set the parent field 'service cat' (choice field - choices are 'support' and 'business'). The Related list has a Services which can have multiple records. Both the parent and child are on the same table(cmdb) with the difference is parent and child has different views.

  Now the child records has one of the  field on the form called 'service type' which is reference field referred to service types table. The service type field has a column 'service cat' which has either support or business. If child record contains multiple records and it can have mix of both support and business values as their values. If all the records has support values , then need to set the parent field 'service cat' to 'support', if all the records has business values,  then need to set the parent field 'service cat' to 'business', if all the records has a mix of support and business values, then need to set the parent field 'service cat' to 'business.

Please let me know how to achieve this.

 

Thanks,

 

1 ACCEPTED SOLUTION

// Get the parent record
var parentGR = new GlideRecord('cmdb_ci');
if (parentGR.get(current.parent)) { // assuming 'parent' is the field pointing to the parent record
    var serviceCatSupport = true;
    var serviceCatBusiness = true;

    // Query related child records
    var childGR = new GlideRecord('cmdb_ci');
    childGR.addQuery('parent', current.parent); // query based on parent field
    childGR.query();

    while (childGR.next()) {
        var serviceTypeGR = new GlideRecord('service_type_table'); // replace with actual table name
        if (serviceTypeGR.get(childGR.service_type)) { // assuming 'service_type' is the reference field
            var serviceCatValue = serviceTypeGR.service_cat; // assuming 'service_cat' is the field in service type table
            
            if (serviceCatValue == 'support') {
                serviceCatBusiness = false; // There are support records
            } else if (serviceCatValue == 'business') {
                serviceCatSupport = false; // There are business records
            }
        }
    }

    // Determine the parent's service cat value
    if (serviceCatSupport && !serviceCatBusiness) {
        parentGR.service_cat = 'support';
    } else if (!serviceCatSupport && serviceCatBusiness) {
        parentGR.service_cat = 'business';
    } else {
        parentGR.service_cat = 'business'; // Default to business if mixed
    }

    // Update the parent record
    parentGR.update();
}

View solution in original post

4 REPLIES 4

Rajesh Chopade1
Mega Sage

Hi @Sagar S 

 

To achieve the requirement of setting the parent field 'service cat' based on the values of the child records in the same table (CMDB), you can create a Business Rule on the CMDB table that triggers after a child record is inserted, updated, or deleted. This Business Rule will check the 'service type' field in the related child records and update the parent’s 'service cat'  accordingly.

 

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

Rajesh

Thanks @Rajesh Chopade1  for your reply.

 

Could you please share the same code snippet. , the child records can have more than one record. the parent field need to set depending on the child records one of the field value. if all records has mix of Support and business value as their column value, then need to set the parent field to business . if all child records has only support values , then need to set the parent field to support and vice versa.

 

Thanks,

// Get the parent record
var parentGR = new GlideRecord('cmdb_ci');
if (parentGR.get(current.parent)) { // assuming 'parent' is the field pointing to the parent record
    var serviceCatSupport = true;
    var serviceCatBusiness = true;

    // Query related child records
    var childGR = new GlideRecord('cmdb_ci');
    childGR.addQuery('parent', current.parent); // query based on parent field
    childGR.query();

    while (childGR.next()) {
        var serviceTypeGR = new GlideRecord('service_type_table'); // replace with actual table name
        if (serviceTypeGR.get(childGR.service_type)) { // assuming 'service_type' is the reference field
            var serviceCatValue = serviceTypeGR.service_cat; // assuming 'service_cat' is the field in service type table
            
            if (serviceCatValue == 'support') {
                serviceCatBusiness = false; // There are support records
            } else if (serviceCatValue == 'business') {
                serviceCatSupport = false; // There are business records
            }
        }
    }

    // Determine the parent's service cat value
    if (serviceCatSupport && !serviceCatBusiness) {
        parentGR.service_cat = 'support';
    } else if (!serviceCatSupport && serviceCatBusiness) {
        parentGR.service_cat = 'business';
    } else {
        parentGR.service_cat = 'business'; // Default to business if mixed
    }

    // Update the parent record
    parentGR.update();
}

Thanks @Rajesh Chopade1  - The above code snippet is working