Variable (new_user) need to be setted to true or false if user exits in the Table of catalog item

Pushpanjali3
Tera Contributor

When the form in the portal is opened, this variable need to be setted to true or false based on this logic:

If the logged user has no records for table ALM Software List, set the variable to true
If the logged user has 1 or more records for table ALM Software List, set the variable to false

I tries with below script but it is always returning true value. Please help on this


Script Include:

var CheckALMSoftwareList = Class.create();
CheckALMSoftwareList.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    hasRecordsForUser: function() {
        var userId = this.getParameter('userId');

        var gr = new GlideRecord('x_stmin_alm_applic_alm_software_list');
        gr.addQuery('sys_created_by', userId);  
        gr.query();

        return gr.hasNext() ? 'true' : 'false';  
    },

    type: 'CheckALMSoftwareList'
});

Client script:

function onLoad() {
    var userName = g_user.userID; 

    var gaMatchingRecord = new GlideAjax('CheckALMSoftwareList');
    gaMatchingRecord.addParam('sysparm_name', 'hasRecordsForUser');
    gaMatchingRecord.addParam('userId', userName);
    gaMatchingRecord.getXMLAnswer(_handleResponse);
}

function _handleResponse(response) {
    var answer = response;
    if (answer === 'true') {
        return;
    } else if (answer === 'false') {
        g_form.setValue('new_user', true);
        g_form.save();
    }
}



1 ACCEPTED SOLUTION

SANDEEP28
Mega Sage

@Pushpanjali3 Issue is with the "userName" parameter which you are passing from client script.  You are querying "x_stmin_alm_applic_alm_software_list" table by putting sys_id of user in "sys_created_by".  

 

System stores "User ID" field of user table in  "sys_created_by" field not the user's sys_id. Because of this invalid query it's ignoring the query and returning all record from "x_stmin_alm_applic_alm_software_list" table.  Hence it is returning "true" everytime from your script include.

 

You should use "g_user.userName" method is client script instead of g_user.userID.

 

var userName = g_user.userName;

 

User below client script-

 

function onLoad() {
    var userName = g_user.userName;   //this will return value present in "User ID" field of User table

    var gaMatchingRecord = new GlideAjax('CheckALMSoftwareList');
    gaMatchingRecord.addParam('sysparm_name', 'hasRecordsForUser');
    gaMatchingRecord.addParam('userId', userName);
    gaMatchingRecord.getXMLAnswer(_handleResponse);
}

function _handleResponse(response) {
    var answer = response;
    if (answer === 'true') {
        return;
    } else if (answer === 'false') {
        g_form.setValue('new_user', true);
        g_form.save();
    }
}

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

 

View solution in original post

3 REPLIES 3

Kartik Magadum
Kilo Sage

Hello

It seems like there might be a slight issue in your client script logic. Let's correct it. Since you want to set the variable new_user to true if the user has no records and false if the user has one or more records, you should handle the true case in the if block and the false case in the else block. Here's the corrected client script:

 

function _handleResponse(response) {
    var answer = response;
    if (answer === 'true') {
        g_form.setValue('new_user', true);
        g_form.save();
    } else if (answer === 'false') {
        g_form.setValue('new_user', false);
        g_form.save();
    }
}

 

With this correction, if the response is 'true', meaning the user has no records, it will set new_user to true. Otherwise, if the response is 'false', indicating that the user has one or more records, it will set new_user to false.

 

Thank You

saurabh_dubey
Kilo Sage

Use this, it will work

function _handleResponse(response) {
    var answer = response;
    if (answer === 'true') {
        return;
    } else {
        g_form.setValue('new_user', true);
        g_form.save();
    }
}

 

If my answer helps you, make sure to hit the solution and helpful button.

 

Thanks and regards,

Saurabh Dubey.

SANDEEP28
Mega Sage

@Pushpanjali3 Issue is with the "userName" parameter which you are passing from client script.  You are querying "x_stmin_alm_applic_alm_software_list" table by putting sys_id of user in "sys_created_by".  

 

System stores "User ID" field of user table in  "sys_created_by" field not the user's sys_id. Because of this invalid query it's ignoring the query and returning all record from "x_stmin_alm_applic_alm_software_list" table.  Hence it is returning "true" everytime from your script include.

 

You should use "g_user.userName" method is client script instead of g_user.userID.

 

var userName = g_user.userName;

 

User below client script-

 

function onLoad() {
    var userName = g_user.userName;   //this will return value present in "User ID" field of User table

    var gaMatchingRecord = new GlideAjax('CheckALMSoftwareList');
    gaMatchingRecord.addParam('sysparm_name', 'hasRecordsForUser');
    gaMatchingRecord.addParam('userId', userName);
    gaMatchingRecord.getXMLAnswer(_handleResponse);
}

function _handleResponse(response) {
    var answer = response;
    if (answer === 'true') {
        return;
    } else if (answer === 'false') {
        g_form.setValue('new_user', true);
        g_form.save();
    }
}

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!