Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

BR not working

karthik65
Tera Guru
br in dmn_demand table. all the error message are printing but state value is not changing in the demand task,
 
 
(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var task = new GlideRecord('dmn_demand_task');
    task.addQuery("parent=878babf793245210a25172918bba10fc^");
    task.query();
    while(task.next()) {
        gs.addInfoMessage(current.getValue('number')+task.number);
        task.setValue('state', 3);
        gs.addInfoMessage("Project created Successfully. All the Associated Demand Task are closed.");
    }

})(current, previous);
1 ACCEPTED SOLUTION

Badrinarayan
Tera Guru

 

 

Hi @karthik65,

If you look at the code, you're currently setting the value of a field, but you're not actually updating the record in the database.

To update a field's value in the database:

  • If you're working with the current record, you should use current.update().

  • If you're using a GlideRecord variable (in your case, the variable is task), you need to update the record with task.update().

Example:

After setting the value of a field using task.setValue(), you must call task.update() to save the changes to the database.

 

task.setValue('field_name', 'value'); // Set the value for the field
task.update(); // Update the record in the database

 

 

for your reference attaching the full Code 

 

(function executeRule(current, previous /*null when async*/) {
    var task = new GlideRecord('dmn_demand_task');
    task.addQuery("parent", "878babf793245210a25172918bba10fc"); // Add your query
    task.query();
    
    while (task.next()) {
        gs.addInfoMessage(current.getValue('number') + " " + task.number);
      
        task.setValue('state', 3);
        
        task.update();
     
        gs.addInfoMessage("Project created successfully. All associated Demand Tasks are closed.");
    }

})(current, previous);

 

 

Please Mark it as Helpful and Accept the Solution , If the Solution is Resolved.

 

Thanks Regards , 

Badrinarayan

View solution in original post

4 REPLIES 4

Bhavya11
Kilo Patron
Kilo Patron

Hi @karthik65 ,

What type of BR is this ?.

or can you update script like below

 

var task = new GlideRecord('dmn_demand_task');
    task.addQuery('parent', current.sys_id); // Adjust the query as per your needs 
    task.query();

    while (task.next()) {
        gs.addInfoMessage(current.getValue('number') + ' - Task: ' + task.number);

        task.setValue('state', 3); // Set the state to '3' (closed)
        task.update(); // Save the changes to the database

        gs.addInfoMessage("Project created successfully. Demand Task " + task.number + " is closed.");
    }

 

 

Please mark helpful & correct answer if it's really worthy for you.

 Thanks,

BK

I missed it. Update operation thanks.

Hi @karthik65 ,

Please mark helpful & correct answer if it's really worthy for you.

 

Thanks,

Bk

Badrinarayan
Tera Guru

 

 

Hi @karthik65,

If you look at the code, you're currently setting the value of a field, but you're not actually updating the record in the database.

To update a field's value in the database:

  • If you're working with the current record, you should use current.update().

  • If you're using a GlideRecord variable (in your case, the variable is task), you need to update the record with task.update().

Example:

After setting the value of a field using task.setValue(), you must call task.update() to save the changes to the database.

 

task.setValue('field_name', 'value'); // Set the value for the field
task.update(); // Update the record in the database

 

 

for your reference attaching the full Code 

 

(function executeRule(current, previous /*null when async*/) {
    var task = new GlideRecord('dmn_demand_task');
    task.addQuery("parent", "878babf793245210a25172918bba10fc"); // Add your query
    task.query();
    
    while (task.next()) {
        gs.addInfoMessage(current.getValue('number') + " " + task.number);
      
        task.setValue('state', 3);
        
        task.update();
     
        gs.addInfoMessage("Project created successfully. All associated Demand Tasks are closed.");
    }

})(current, previous);

 

 

Please Mark it as Helpful and Accept the Solution , If the Solution is Resolved.

 

Thanks Regards , 

Badrinarayan