UI Action on Table A to update a record on Table B

Somujit1
Tera Contributor

Hello Experts,

 

I have a custom table A where we create records to allocate multiple assets to different locations. On the Hardware table we have a reference field to table A record. I need to create a UI action on table A which on click should update the reference field on Hardware with the table A record name.

The purpose of the UI Action is to allow multiple asset record update based on a query criteria.

I wrote the below client callable UI Action on the custom table but it doesn't seem to update the Hardware record.

Any suggestion on what i am doing wrong would be great.

 

 

Table - Table A

Form Button - true
Client - true
OnClick - addAsset()
Condition - (state=='new'||state=='open')
Script -
function addAsset(){
var answer=confirm("Are you sure you want add hardware assets to this deployment record?");
    if (answer==false)
    {
     return false;
    }
if(typeof window == 'undefined')
    assetReturn();
function assetReturn(){
var updateAsset = new GlideRecord ("alm_hardware");
updateAsset.addEncodedQuery('stockroom=<location_name>^install_statusIN6,1^substatus=available');
updateAsset.query();
while (updateAsset.next()){
updateAsset.reference_field_name = current.record_name;
updateAsset.update();
 
}
}
}
4 REPLIES 4

Voona Rohila
Kilo Patron
Kilo Patron

Hi @Somujit1 

check below comments

 

function addAsset() {
    var answer = confirm("Are you sure you want add hardware assets to this deployment record?");
    if (answer == false) {
        return false;
    }
    if (typeof window == 'undefined')
        assetReturn();

    function assetReturn() {
        var updateAsset = new GlideRecord("alm_hardware");
        updateAsset.addEncodedQuery('stockroom=<location_name>^install_statusIN6,1^substatus=available'); //please provide proper sysid of stockroom here
        updateAsset.query();
        while (updateAsset.next()) {
            updateAsset.reference_field_name = current.sys_id; // modify this and use proper field name.
            updateAsset.update();
        }
    }
}

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Thanks for your reply @Voona Rohila. Updated the script as suggested, but it doesnt seem to updated the hardware record

Karan Chhabra6
Mega Sage
Mega Sage

Hi @Somujit1 ,

 

In order to use the server script in the UI action, you need to trigger it using gsftSubmit().

Please add an action name as well example : 'update_asset'

AND, the condition should be (current.state=='new'||current.state=='open')

 

Refer to this script below:

function addAsset() {
    var answer = confirm("Are you sure you want add hardware assets to this deployment record?");
    if (answer == false) {
        return false;
    }
else{
gsftSubmit(null, g_form.getFormElement(), "update_asset")

}
    if (typeof window == 'undefined')
        assetReturn();

    function assetReturn() {
        var updateAsset = new GlideRecord("alm_hardware");
        updateAsset.addEncodedQuery('stockroom=<location_name>^install_statusIN6,1^substatus=available'); //please provide proper sysid of stockroom here
        updateAsset.query();
        while (updateAsset.next()) {
            updateAsset.reference_field_name = current.sys_id; // modify this and use proper field name.
            updateAsset.update();
        }
    }
}

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks!

 

Hi @Karan Chhabra6 ,

 

Thanks for your reply.

I updated the script with the above changes suggested by you and as below, but it still doesnt seem to update the hardware record. Any suggestions to make it work?

 

Updated UI action script, action name and condition on custom table A -

function addAsset() {
    var answer = confirm("Are you sure you want add hardware assets to this deployment record?");
    if (answer == false) {
        return false;
    }
else{
gsftSubmit(null, g_form.getFormElement(), "update_asset")
}
    if (typeof window == 'undefined')
        assetReturn();

    function assetReturn() {
    var updateAsset = new GlideRecord("alm_hardware");
    updateAsset.addEncodedQuery
('warranty_expirationONThis month@javascript&colon;gs.beginningOfThisMonth()@javascript&colon;gs.endOfThisMonth()^install_statusIN6,1^substatus=available')
    updateAsset.query();
    while (updateAsset.next()){
        updateAsset.u_project_deployment = current.sys_id;
        updateAsset.update();
    }
    }
    }