Now Experience Catalog Item Set Focus to specific variable on Change, I can't get it to work

litchick10
Tera Guru

I am populating a list collector base on the value of another list collector.  This works great but the focus shifts to the second list collector and users might be confused, I'd like to refocus on the original list collector after populating the second field. 

 

I've tried the following that didn't refocus

g_form.getControl('field_name').focus();

g_form.getElement('field_name').focus();

g_form.getFormElement('field_name').focus();

g_form.getControl('field_name').scrollIntoView();

g_form.getElement('field_name').parentElement.lastElementChild.firstChild.focus();

 

Here's my current script without a focus:

Isolate Script = false (also tried with true)

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) {
        return;
    }
    g_form.hideFieldMsg('technology_cmdb_ci_business_app');
    if (newValue == '') {
        g_form.clearValue('technology_customer_facing');
        g_form.showFieldMsg('technology_cmdb_ci_business_app', 'You cannot select Customer Facing Business Applications with this field empty');
    } else {
        var cmdb = new GlideAjax('getTableDataUtils');
        cmdb.addParam('sysparm_name', 'getList');
        cmdb.addParam('sysparm_query_table', 'cmdb_ci_business_app');
        cmdb.addParam('sysparm_query_fields', 'sys_id');
        cmdb.addParam('sysparm_query_encodedQuery', 'sys_idIN' + newValue + '^u_customer_facing=Yes');
        cmdb.addParam('sysparm_query_order', 'sys_id');
        cmdb.getXML(getInfo);
    }
    function getInfo(response) {
        var custFace = [];
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer) {
            answer = answer.replace(/},{/g, '}},{{');
            var answerArr = answer.split('},{');
            for (var i = 0; i < answerArr.length; i++) {
                var answerObj = JSON.parse(answerArr[i]);
                custFace.push(answerObj.sys_id.toString());
            }
            g_form.clearValue('technology_customer_facing_business_app');
            if (g_form.getValue('technology_customer_facing') != 'Yes') {
                g_form.setValue('technology_customer_facing', 'Yes');
            }
            g_form.setValue('technology_customer_facing_business_app', custFace); //Here is where I need to refocus on technology_cmdb_ci_business_app
        } else {
            if (g_form.getValue('technology_customer_facing') != 'No') {
                g_form.setValue('technology_customer_facing', 'No');
            }
        }
    }
}

Any help is appreciated 

2 REPLIES 2

M Iftikhar
Kilo Sage

Hi @litchick10 ,

Can you try setTimeout - as it waits a short moment so ServiceNow can finish rendering the target list collector.

setTimeout(function() {
var ctrl = g_form.getControl('technology_cmdb_ci_business_app');
ctrl.focus();
}, 200);

Also change your "UI Type"  to "All" .

 

Thanks & Regards, 
Muhammad Iftikhar

 

If my response helped, please mark it as the accepted solution so others can benefit as well. 

Ankur Bawiskar
Tera Patron
Tera Patron

@litchick10 

this is for native view/service catalog or portal?

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