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.

Could you correct my client script, it doesn't work in portal

abbassi
Tera Contributor
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
var userinfo = g_form.getReference ('user');
//g_form.setValue ('ldap', userinfo.user_name);
    // Fonction pour réinitialiser les champs
    function resetFields() {
        g_form.setValue('email', '');
        g_form.setValue('bu_or_external_supplier', '');
   
        g_form.setReadOnly('email', false);
        g_form.setReadOnly('bu_or_external_supplier', false);
      //  g_form.setReadOnly('ldap', false);
    }

    // Fonction pour définir les champs
    function setFields(email, company) {
        g_form.setValue('email', email);
        g_form.setValue('bu_or_external_supplier', company);
       // g_form.setValue('ldap', ldap);
        g_form.setReadOnly('email', true);
        g_form.setReadOnly('bu_or_external_supplier', true);
        //g_form.setReadOnly('ldap', true);
    }

    var userRecord = new GlideRecord("sys_user");
    if (!userRecord.get(newValue)) {
        resetFields();
        return;
    }

    var emailUser = userRecord.getValue('email');
    var companyID = userRecord.getValue('company');
    //var ldapUser = userRecord.getValue('ldap'); 

    if (!companyID) {
        resetFields();
        return;
    }

    var companyRecord = new GlideRecord('core_company');
    if (!companyRecord.get(companyID)) {
        resetFields();
        return;
    }

    var companyName = companyRecord.getValue('name');
    if (companyName === 'GROUPE') {
        setFields(emailUser, companyName);
    } else {
        resetFields();
    }
}



1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@abbassi There are two issues with your client scripts.

1.  g_form.getReference ('user');  getReference call without a callback function are not supported in Service Portal. For more information please refer to https://www.servicenow.com/community/developer-forum/g-form-getreference-is-not-working-in-service-p...

 

2. var userRecord = new GlideRecord("sys_user"); Synchronous GlideRecord queries in client script in Service portal are not supported. Please consider shifting your GlideRecord queries to a Script Include and call them via GlideAjax.

 

Hope this helps.

View solution in original post

6 REPLIES 6

Bert_c1
Kilo Patron

Hi,

 

Is the 'UI Type' field on the client script set to "All"?

abbassi
Tera Contributor

Yes , I cheked All 

Bert_c1
Kilo Patron

Try adding debug and test:

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
var userinfo = g_form.getReference ('user');
//g_form.setValue ('ldap', userinfo.user_name);
    // Fonction pour réinitialiser les champs
    function resetFields() {
		alert("client script: resetFields() called");
        g_form.setValue('email', '');
        g_form.setValue('bu_or_external_supplier', '');
   
        g_form.setReadOnly('email', false);
        g_form.setReadOnly('bu_or_external_supplier', false);
      //  g_form.setReadOnly('ldap', false);
    }

    // Fonction pour définir les champs
    function setFields(email, company) {
		alert("setFields called, email = " + email + "company = " + company);
        g_form.setValue('email', email);
        g_form.setValue('bu_or_external_supplier', company);
       // g_form.setValue('ldap', ldap);
        g_form.setReadOnly('email', true);
        g_form.setReadOnly('bu_or_external_supplier', true);
        //g_form.setReadOnly('ldap', true);
    }

    var userRecord = new GlideRecord("sys_user");
    if (!userRecord.get(newValue)) {
		alert("userRecord get failed");
        resetFields();
        return;
    }

    var emailUser = userRecord.getValue('email');
    var companyID = userRecord.getValue('company');
    //var ldapUser = userRecord.getValue('ldap');
	alert("setting emailUser = " + emailUser + ", companyID = " + companyID);

    if (!companyID) {
		alert("calling resetField() due to companyID");
        resetFields();
        return;
    }

    var companyRecord = new GlideRecord('core_company');
    if (!companyRecord.get(companyID)) {
		alert("calling resetField() due to companyID not being found");
        resetFields();
        return;
    }

    var companyName = companyRecord.getValue('name');
    if (companyName === 'GROUPE') {
		alert("calling setFields() due to companyName being GROUPE");
        setFields(emailUser, companyName);
    } else {
		alert("calling resetFields() due to companyName not being GROUPE");
        resetFields();
    }
}

 

And refer here:

 

service-portal client-script-reference

 

 

abbassi
Tera Contributor

Hello @Bert_c1 

It 'doesn't work