Script is not working

akin9
Tera Contributor

Hello exports,

We have "sn_ind_tmt_orm_order_task" table and "u_site_name" field and related list "sn_ind_tmt_orm_order_characteristic_value" table and "characterstic" is "sitename" this have charactestic_value will be updated through the payload .

 

Requirement is .

We want to update characterstic value in  "u_site_name" field . below script is working but issue is its

updating same value for all the task. please support on this.

 

   var characteristicGR = new GlideRecord('sn_ind_tmt_orm_order_characteristic_value');
    characteristicGR.addQuery('sn_ind_tmt_orm_order_task', current.sys_id); // Filter by the current order task
    characteristicGR.addQuery('characteristic.name', 'sitename'); // Filter by the characteristic name
    characteristicGR.query();

    if (characteristicGR.next()) {
        // Retrieve the characteristic_value (or characteristic_option_value) from the record
        var siteNameValue = characteristicGR.characteristic_option_value; // Replace this with the correct field name if different

        // Ensure we have a valid value before updating the u_site_name field
        if (siteNameValue) {
            current.u_site_name = siteNameValue;
        }
    }
 
Pls support thank you
5 REPLIES 5

Tushar
Kilo Sage
Kilo Sage

Hi @akin9 

 

can you please try with below code -

 

var characteristicGR = new GlideRecord('sn_ind_tmt_orm_order_characteristic_value');
characteristicGR.addQuery('sn_ind_tmt_orm_order_task', current.sys_id); // Filter by the current order task
characteristicGR.addQuery('characteristic.name', 'sitename'); // Filter by the characteristic name
characteristicGR.query();

var siteNameValue = '';  // Initialize variable to hold the site name value

// Loop through all the records for the current task
while (characteristicGR.next()) {
    // Retrieve the characteristic_value (or characteristic_option_value) from the record
    var tempSiteNameValue = characteristicGR.characteristic_option_value; // Replace with the correct field name if different
    
    // If we find a valid site name, update the siteNameValue variable (you can adjust logic here if needed)
    if (tempSiteNameValue) {
        siteNameValue = tempSiteNameValue;
    }
}

// If a valid siteNameValue was found, update the u_site_name field
if (siteNameValue) {
    current.u_site_name = siteNameValue;
}

 

Thanks

akin9
Tera Contributor

Hi @Tushar  

Thank you for the quick reply will check and update.

 

akin9
Tera Contributor

Hi @Tushar  

I have tried the same  above script still we are facing the same issue.

from payload we posted characterstic value is ,madurai,pune and chennai still its updating pune only

pls check and support this.

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

var characteristicGR = new GlideRecord('sn_ind_tmt_orm_order_characteristic_value');
characteristicGR.addQuery('sn_ind_tmt_orm_order_task', current.sys_id); // Filter by the current order task
characteristicGR.addQuery('characteristic.name', 'sitename'); // Filter by the characteristic name
characteristicGR.query();

var siteNameValue = '';  


while (characteristicGR.next()) {
    // Retrieve the characteristic_value (or characteristic_option_value) from the record
    var tempSiteNameValue = characteristicGR.characteristic_option_value; // Replace with the correct field name if different
   
    // If we find a valid site name, update the siteNameValue variable (you can adjust logic here if needed)
    if (tempSiteNameValue) {
        siteNameValue = tempSiteNameValue;
    }
}


if (siteNameValue) {
    current.u_site_name = siteNameValue;
}

})(current, previous);

Tushar
Kilo Sage
Kilo Sage

I think siteNameValue is being overwritten with the last characteristic_option_value in the loop (which happens to be pune), causing only that value to be assigned to u_site_name.

 

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

    var characteristicGR = new GlideRecord('sn_ind_tmt_orm_order_characteristic_value');
    characteristicGR.addQuery('sn_ind_tmt_orm_order_task', current.sys_id); // Filter by the current order task
    characteristicGR.addQuery('characteristic.name', 'sitename'); // Filter by the characteristic name
    characteristicGR.query();

    var siteNameValue = '';   // Initialize an empty string to store the concatenated values

    // Loop through all the characteristics and concatenate their values
    while (characteristicGR.next()) {
        var tempSiteNameValue = characteristicGR.characteristic_option_value; // Fetch the characteristic value
        if (tempSiteNameValue) {
            // concatenate with a comma (or any other delimiter) if it's not the first value
            if (siteNameValue != '') {
                siteNameValue += ','; // Add a comma separator
            }
            siteNameValue += tempSiteNameValue; // Append the value
        }
    }

    // If we have any valid site name values, update the u_site_name field
    if (siteNameValue) {
        current.u_site_name = siteNameValue;
    }

})(current, previous);