Update a record using record producer

TejasviR
Tera Contributor

Hello Everyone!

I am looking to updates an existing record on a table rather than inserting a new entry using record producer.

The record producer contains one reference variable, once a selection is chosen, I have a script that populates  the relevant information from the table.

These variables are editable and a user can update these as well.

i tried the given solution as well but no luck.

Solution: https://community.servicenow.com/community?id=community_question&sys_id=e77abb30db4473480be6a345ca961964

----------------------- My Script--------------

if (producer.type_of_request == 2) { // Select a choice from the menu
    var record = producer.ad_group_name;
    var view_gr = new GlideRecord('software_groups');
    gs.addInfoMessage("AD Group" + record + review.sys_id);
    view_gr.addEncodedQuery('sys_id=+record' );
    view_gr.query();
    gs.addInfoMessage("Its working!");

    //if we find a record update it

    if (view_gr.next()) {
        
        gs.addInfoMessage("Its working till here!");
        view_gr.u_description = producer.description;
        view_gr.u_publisher = producer.u_publisher.getDisplayValue();
        view_gr.u_product = producer.u_product.getDisplayValue();
        view_gr.u_discovery_model = producer.discovery_model;
        view_gr.u_approval_process = producer.approval_process;
        view_gr.u_dedicated_approver = producer.dedicated_approver.getDisplayValue();
        view_gr.update();
        gs.addInfoMessage("The record has now been updated");
        current.setAbortAction(true);

        }
}

 

Thanks in advance

4 REPLIES 4

Maik Skoddow
Tera Patron
Tera Patron

Hi

a Record Producer is only for creating records.

Why don't you use a common catalog item?

Maik

Hello @Maik Skoddow ,

Actually using the same record producer for another flow, that's why i can't.

Mahendra RC
Mega Sage

Hello,

I am not sure if this is your old code, but this code will not work. The below code line is incorrect.

view_gr.addEncodedQuery('sys_id=+record' ); // Entire query is inside '' 

Please check below code once:

if (producer.type_of_request == 2) { // Select a choice from the menu
    var record = producer.ad_group_name;
    var view_gr = new GlideRecord('software_groups');
    gs.addInfoMessage("AD Group" + record + review.sys_id);
    //view_gr.addEncodedQuery('sys_id=+record' ); this was your code line
    view_gr.addEncodedQuery('sys_id=' + record);
    view_gr.query();
    gs.addInfoMessage("Its working!");

    //if we find a record update it

    if (view_gr.next()) {
        
        gs.addInfoMessage("Its working till here!");
        view_gr.u_description = producer.description;
        view_gr.u_publisher = producer.u_publisher.getDisplayValue();
        view_gr.u_product = producer.u_product.getDisplayValue();
        view_gr.u_discovery_model = producer.discovery_model;
        view_gr.u_approval_process = producer.approval_process;
        view_gr.u_dedicated_approver = producer.dedicated_approver.getDisplayValue();
        view_gr.update();
        gs.addInfoMessage("The record has now been updated");
        current.setAbortAction(true);

        }
}

Please mark this as helpful/correct, if it answer your question.

Thanks

Hello Mahendra,

It's still creating a new record.