Will Update work in the Insert Scripted Rest API

Ksnow
Tera Contributor

Hello all, @shloke04 @Ankur Bawiskar 

I have used scripted rest API (Insert), POST method to Insert the data, It worked fine.

There are few records inserted, all good here but later I have added 1 or 2 fields in that table and updated the scripted accordingly, just to update those 2 fields without disturbing the earlier records but the problem here is now records or inserting again as duplicates even though we have check point to stop duplicates.

Update function will not work in the Insert scripted rest API?

find_real_file.png

Please help!

Thanks,

Ksnow

17 REPLIES 17

Hi,

if record is found then it would update

are you talking about this part?

this looks good to me

var vnetSysId;
        var grVnet = new GlideRecord("u_cmdb_ci_azure_virtual_network");
        grVnet.addQuery("name", vnet.name);
        grVnet.addQuery("u_resource_group", rgSysId);
        grVnet.query();
        // If there is not already a Virtual Network with this name, insert it
        if (!grVnet.next()) {
            grVnet.initialize();
            grVnet.u_resource_group = rgSysId;
            grVnet.u_azure_location = locSysid;
            grVnet.name = vnet.name;
            grVnet.u_azure_subscription = subscriptionSysId;
            grVnet.u_active = vnet.u_active;
            vnetSysId = grVnet.insert();
        } else {
            grVnet.u_azure_location = locSysid;
            grVnet.u_azure_subscription = subscriptionSysId;
            grVnet.u_active = vnet.u_active;
            grVnet.setWorkflow(false);
            grVnet.autoSysFields(false);
            grVnet.update();
            while (grVnet.next()) {
                vnetSysId = grVnet.sys_id.toString();
            }
        }

Regards
Ankur

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

Yes, it's not updating and creating a duplicates.

find_real_file.png

Hi,

it means your query is not working fine

        var grVnet = new GlideRecord("u_cmdb_ci_azure_virtual_network");
        grVnet.addQuery("name", vnet.name);
        grVnet.addQuery("u_resource_group", rgSysId);

Regards
Ankur

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

Could you please let me know how can I make it work?

And not all records having duplicates, but not getting updated, example as below

find_real_file.png

Thanks

Ksnow
Tera Contributor

Just to confirm, will update work in API when we have given it for Insert?