Updating records on custom table

cre1014
Kilo Expert

In creating a new service catalog item, I created a custom table that is populated with all of the variable information to make for easier reporting. (The Variable Ownership table for us is rather huge.) I have the business rule that successfully captures all of the entered data into the variables and writes them to the respective columns in the new custom table, using the RITM number as the link.

The issue that I'm having is with updating these records. There are instances where our support teams will update the variable information at the TASK level, and these changes need to be reflected in the new custom table that I initially created. Since the variables on the TASK form are the same as the ones on the RITM, I was hoping this would be fairly straight forward. However, this is the part that isn't quite working correctly - my logs state that the update() that I have at the end of the script has been reached, but nothing about the record in the table actually updates.

Below is the code for the attempted updating of the record via the TASK. For the sake of brevity, I only left two variables as an example.Is this even possible to do? What have I not set up correctly in order to get this to work?

/**

* Expense Authorization Request

**/

if (current.request_item.cat_item.getDisplayValue() == "Expense Authorization Request"){

  var gr = new GlideRecord("u_expense_authorization_reques");

        var updateRecord = false;

  gr.addQuery('u_request_item', current.request_item.sys_id);

  gr.query();

  if (gr.next()) {

  //set flag to indicate record already exists

  updateRecord = true;

  }

  gr.initialize();

//Copy all variable values to the record

gr.urequest_item = current.request_item.sys_id;

  gr.u_additional_info = current.request_item.variable_pool.add_info;

  gr.u_additional_review_required = current.request_item.variable_pool.add_review_req;

  gr.u_approval_to_move_forward = current.request_item.variable_pool.app_move_forward;

 

if (updateRecord == true) {

        // Update

gs.log("I've reached the update step");

        gr.update()

  } else {

    //Insert the record

          gr.insert();

    }

}

5 REPLIES 5

ahaz86
Mega Guru

have you tried logging the values you are trying to set to see if they are coming through?


gs.log(current.request_item.variable_pool.add_info)



also, is line 19 correct or should there be an underscore after   the u


gr.urequest_item = current.request_item.sys_id;     // current


gr.u_request_item = current.request_item.sys_id;     // modified


I've logged the variables like you suggested, and found something interesting; I'm getting previous data that I've entered into those fields, not what I'm trying to change them to. Do you have any idea why that might be happening?


sappidi
Kilo Contributor

Hi,


I have same requirement, If possible can u give me the script that updating custom table via variables data entered by user. it will be very helpful for me.


Greg42
Mega Guru

Hi Caitlyn,



In line 16 you initialize completely new object that doesn't exist anywhere. In line 13 you set a flag that always go to if statement in lines 25-29 but because you act on an object that is not yet in the database your update doesn't go anywhere and is lost after this script is finished. Remove line 16 and you should be fine.



Edit: Ooops such a necro post, just noticed



Cheers



Greg