Set values on a record using business rule

Hari1
Mega Sage

Hi,

I have created a business rule to set values on the "assessment instance question" table. I am not able to set the values. I can see the values in the logs. But still i am not able to set those values.
Below is my business rule.

 

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

    var arrQust = [];

    var obj = {};

    var recordSysId = current.getValue('sys_id');

    var wfd = new GlideRecord("u_wfd_assessment"); // custom table
    wfd.addQuery('u_assessment_number', recordSysId);
    wfd.query();
    if (wfd.next()) {
        var wfdQust = new GlideRecord('asmt_assessment_instance_question');
        wfdQust.addQuery('instance', recordSysId);
        wfdQust.query();
        while (wfdQust.next()) {
            obj = {
                metric: wfdQust.getDisplayValue('metric'),
                value: wfdQust.getValue('value'),
                stringValue: wfdQust.getValue('string_value')
            };

            arrQust.push(obj);
        }
        gs.info("WFD : Business Rule: arrQust: " + arrQust);
        gs.info("WFD : Business Rule: JSON.stringify(arrQust): " + JSON.stringify(arrQust));

        for (var i = 0; i < arrQust.length; i++) {
            gs.info("WFD : Business Rule: Assessment Number: " +wfd.getValue("u_assessment_number") + ",arrQust[i].metric: " + arrQust[i].metric + " ,arrQust[i].value: " + arrQust[i].value + " ,arrQust[i].stringValue: " + arrQust[i].stringValue);
            if (arrQust[i].metric == "Product") {
                gs.info("WFD : Business Rule: Inside if block " + arrQust[i].metric);

                wfd.product = arrQust[i].stringValue;
                //wfd.update();
				
            } else if (arrQust[i].metric == "ID") {
                gs.info("WFD : Business Rule: Inside if block " + arrQust[i].metric);

                wfd.u_id = arrQust[i].stringValue;
                //wfd.update();
				
            } else if (arrQust[i].metric == "Application Type") {
                gs.info("WFD : Business Rule: Inside if block " + arrQust[i].metric);

                wfd.u_application_type = arrQust[i].stringValue;
                //wfd.update();
				
            } else if (arrQust[i].metric == "Application") {
                gs.info("WFD : Business Rule: Inside if block " + arrQust[i].metric);

                wfd.u_application = arrQust[i].stringValue;
                //wfd.update();
				
            } 
        }
		wfd.update();
    }

})(current, previous);

 

 c1.JPG

9 REPLIES 9

@Hari1 were you able to achieve this?


Raghav
MVP 2023
LinkedIn

Pavankumar_1
Mega Patron

Hi @Hari1 ,

on this table u_wfd_assessment you are setting and updating the values.

if you want to update values in asmt_assessment_instance_question table. you need to use like below.

Example:

wfdQust.product = arrQust[i].stringValue;

wfdQust.update();

My understanding: But from you are script getting values from the asmt_assessment_instance_question table and based on certain conditions you are  updating u_wfd_assessment this table.

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Pavankumar_1
Mega Patron

Hi @Hari1 ,

Try this script

(function executeRule(current, previous /*null when async*/ ) {
    var arrQust = [];
    var obj = {};
    var wfdQust = new GlideRecord('asmt_assessment_instance_question');
    wfdQust.addQuery('instance', current.getValue('sys_id')); //current record sysid should match with instance sysid
    wfdQust.query();
    while (wfdQust.next()) {
        obj = {
            metric: wfdQust.getDisplayValue('metric'),
            value: wfdQust.getValue('value'),
            stringValue: wfdQust.getValue('string_value')
        };
        arrQust.push(obj);
    }
    var arrQustJSON = JSON.parse(arrQust);
    for (var i = 0; i < arrQustJSON.length; i++) {
        if (arrQustJSON[i].metric == "Product") {
            current.product = arrQustJSON[i].stringValue;
        } else if (arrQustJSON[i].metric == "ID") {
            current.u_id = arrQustJSON[i].stringValue;
        } else if (arrQustJSON[i].metric == "Application Type") {
            current.u_application_type = arrQustJSON[i].stringValue;
        } else if (arrQustJSON[i].metric == "Application") {
            current.u_application = arrQustJSON[i].stringValue;
        }
    }
    current.update();
})(current, previous);
If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Pavankumar_1
Mega Patron

Hi @Hari1 ,

If your issue got resolved close the question by Accepting solution and hit thumb icon.
If not reply and share details.

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Pavankumar_1
Mega Patron

Hi @Hari1 ,

If your issue got resolved close the question by Accepting solution and hit thumb icon.
If still not resolved reply and share details will try to resolve your issue.

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar