Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Checking conditions before record addition though declarative UI action

MaharshiC
Tera Contributor

Hi,

 

I have 2 tables and I want to create bulk request from 'x_iem_tqa_work_request_items'

to 'x_iem_tqa_work_order' though declarative UI action. So I have written the below script and its creating records in '

x_iem_tqa_work_order' as expected. Now 'x_iem_tqa_work_request_items' has 2 additional fields - parent and state and now there is an additional condition that I need to apply - If there is any record in ''x_iem_tqa_work_request_items' that we select have a different parent or state than the others then we have to stop the process all together. What should be my approach in that case? Will I be able to add conditions to stop the entire insert process?
var requests='';
var items= new GlideRecord('x_iem_tqa_work_request_items');
items.addEncodedQuery('selected_work_items=true^associated_work_order_numberISEMPTY');
items.query();
while(items.next())
{
  requests=requests+','+items.sys_id;

}
	var order = new GlideRecord('x_iem_tqa_work_order');
	order.initialize();
	order.assocaited_work_request_number=items.assocaited_work_request_number;
	//order.associated_work_request_item_number=items.sys_id;
	order.associated_work_request_item_number=requests;
	order.insert();

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@MaharshiC 

what type of UI action it is? Related list?

Share screenshots.

try this updated script

var requests = '';
var items = new GlideRecord('x_iem_tqa_work_request_items');
items.addEncodedQuery('selected_work_items=true^associated_work_order_numberISEMPTY');
items.query();

var parent = null;
var state = null;
var isValid = true;

while (items.next()) {
    if (parent === null && state === null) {
        parent = items.parent;
        state = items.state;
    } else if (items.parent != parent || items.state != state) {
        isValid = false;
        break;
    }
    requests = requests + ',' + items.sys_id;
}

if (isValid) {
    var order = new GlideRecord('x_iem_tqa_work_order');
    order.initialize();
    order.assocaited_work_request_number = items.assocaited_work_request_number;
    order.associated_work_request_item_number = requests;
    order.insert();
} else {
    gs.addErrorMessage('Selected work request items have different parent or state. Process stopped.');
}

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

View solution in original post

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@MaharshiC 

what type of UI action it is? Related list?

Share screenshots.

try this updated script

var requests = '';
var items = new GlideRecord('x_iem_tqa_work_request_items');
items.addEncodedQuery('selected_work_items=true^associated_work_order_numberISEMPTY');
items.query();

var parent = null;
var state = null;
var isValid = true;

while (items.next()) {
    if (parent === null && state === null) {
        parent = items.parent;
        state = items.state;
    } else if (items.parent != parent || items.state != state) {
        isValid = false;
        break;
    }
    requests = requests + ',' + items.sys_id;
}

if (isValid) {
    var order = new GlideRecord('x_iem_tqa_work_order');
    order.initialize();
    order.assocaited_work_request_number = items.assocaited_work_request_number;
    order.associated_work_request_item_number = requests;
    order.insert();
} else {
    gs.addErrorMessage('Selected work request items have different parent or state. Process stopped.');
}

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