Record Producer Will Not Update Existing Record - Only Create New Record

chrisinhoff
Tera Contributor

Hello,

 

I am trying to create a workflow/form in ServiceNow, where the form has about 6 questions.

What Configuration Item would you like to modify?

Then I have like 5 questions like Serial Number, Location, and MAC Address available to enter details.

I have tried using a Catalog Item to do this, but I have had no luck in trying to get the selected CI to be recognized by the flow.

I then tried a Record Producer, but it seems to only want to create a new record, and not modify an existing one, no matter what I do.

Is a Record Producer supposed to be able to modify existing records? This seems like it would be an easy thing to set up.

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@chrisinhoff 

you can use record producer to update as well.

Simply do this

1) in record producer script get the reference variable (pointing to existing record) and then update the record by using GlideRecord

2) then use current.setAbortAction(false) to stop the insertion

Using a record producer to update a record and not insert 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@chrisinhoff 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

chrisinhoff
Tera Contributor

Below is what I am using. Unfortunately, I am at the mercy of ChatGPT, and it hasn't been doing well with this particular task. I have asked it to incorporate what you have mentioned and also what was mentioned in the link you shared. I originally was using alm_asset, but it is not able to find the CI even though I can see the CI for my device in the alm_asset table. I have also tried using cmdb_ci_computer and all classes under it to no avail.

I am now using just cmdb_ci because I have been unable to modify anything, and I just want it to recognize the selected CI, but I get "No Configuration Item Selected" error.

 

(function executeAction(producer) {
    // Log for debugging
    gs.info("Producer Variables: " + JSON.stringify(producer));

    // Check if the CI sys_id exists (valid reference)
    var ciSysId = producer.ci_sys_id;
    if (!ciSysId) {
        gs.addErrorMessage("No Configuration Item selected.");
        return; // Abort the script
    }

    var grCI = new GlideRecord('cmdb_ci');
    if (!grCI.get(ciSysId)) {
        gs.addErrorMessage("Configuration Item not found.");
        return; // Abort the script if CI is not found
    }

    // Check for field values that shouldn't be blank and abort if any are
    if (producer.install_state === '' || producer.serial_number === '' || producer.assigned_to === '') {
        gs.addErrorMessage("Required fields are missing (Install State, Serial Number, Assigned To).");
        return; // Abort if any of these fields are left blank
    }

    // Ensure that the fields only get updated if new values are provided
    if (producer.install_state !== grCI.install_state) {
        grCI.install_state = producer.install_state;
        gs.info("Updating Install State: " + producer.install_state);
    }

    if (producer.serial_number !== grCI.serial_number) {
        grCI.serial_number = producer.serial_number;
        gs.info("Updating Serial Number: " + producer.serial_number);
    }

    if (producer.assigned_to !== grCI.assigned_to) {
        grCI.assigned_to = producer.assigned_to;
        gs.info("Updating Assigned To: " + producer.assigned_to);
    }

    // Commit the changes only if fields were updated
    if (grCI.update()) {
        gs.info("Asset " + grCI.name + " (" + ciSysId + ") updated successfully.");
    } else {
        gs.addErrorMessage("No changes were made, or no valid updates occurred.");
    }

})(producer);

@chrisinhoff 

Don't rely on ChatGPT. It sometimes give weird results.

are you using the correct variables from your record producer and correct field on table?

the link I shared has solution.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader