The CreatorCon Call for Content is officially open! Get started here.

Compare Login User with the Assigned To reference field in the UI Action button

BKSharma
Tera Contributor

Hi,

I'm working on some UI Action. In the RITM form I've a UI Action button.

My requirement is to when a user click on that button, I need to validate, if the logged in user is the user mentioned in the assigned_to field or anyother user.

How I can validate this?

1 ACCEPTED SOLUTION

YaswanthKurre
Tera Guru

Hi @BKSharma ,

 

You can use this script in UI action field.

 

var assignedTo = g_form.getValue('assigned_to');
var loggedInUser = g_user.userID;

if (assignedTo === loggedInUser) {
    alert("You are the assigned user.");
} else {
    alert("You are not the assigned user.");
}

 

Mark this as helpful and correct if this solves your question.

 

Thanks,

Yaswanth.

View solution in original post

12 REPLIES 12

Hi @Ankur Bawiskar ,

I just tried below but it's not getting validated -

 

function checkMandatory() {
    if (g_form.getValue('state') == '5' && (current.assigned_to == gs.getUserID())) {

        if (g_form.getValue('comments') == '') {
            g_form.hideFieldMsg('comments');
            g_form.setMandatory('comments', true);
            alert('Additional Comments are mandatory when placing the requested item into Work in Progress.');
            return false;
        }
    } else
    if ((g_form.getValue('work_notes') == '') && (g_form.getValue('comments') == '')) {
        g_form.hideFieldMsg('work_notes');
        g_form.hideFieldMsg('comments');
        g_form.setMandatory('work_notes', true);
        g_form.setMandatory('comments', true);
        alert('Work Notes and Additional Comments are mandatory when placing the requested item into Work in Progress.');
        return false;
    }


    g_form.setValue('state', 2);
    gsftSubmit(null, g_form.getFormElement(), 'requested_item_work_in_progress');
}


if (typeof window == 'undefined')
    serverWIP();

function serverWIP() {
    current.state = 2;
    current.update();
    action.setRedirectURL(current);
}
 
Can you please let me know, where I'm doing wrong?

My requirement is, if the logged in user is the Assigned To user, then Additional Comments (comments) is mandatory else, both Worknote (work_note) and Additional Comments (comments) are mandatory. 
This needs to be done through UI Action only. As there is a dedicated UI action button for the same. 

@BKSharma 

update as this

function checkMandatory() {
    if (g_form.getValue('state') == '5' && (g_form.getValue('assigned_to') == g_user.userID)) {
        if (g_form.getValue('comments') == '') {
            g_form.hideFieldMsg('comments');
            g_form.setMandatory('comments', true);
            alert('Additional Comments are mandatory when placing the requested item into Work in Progress.');
            return false;
        }
    } else
    if ((g_form.getValue('work_notes') == '') && (g_form.getValue('comments') == '')) {
        g_form.hideFieldMsg('work_notes');
        g_form.hideFieldMsg('comments');
        g_form.setMandatory('work_notes', true);
        g_form.setMandatory('comments', true);
        alert('Work Notes and Additional Comments are mandatory when placing the requested item into Work in Progress.');
        return false;
    }

    gsftSubmit(null, g_form.getFormElement(), 'requested_item_work_in_progress');
}


if (typeof window == 'undefined')
    serverWIP();

function serverWIP() {
    current.state = 2;
    current.update();
    action.setRedirectURL(current);
}

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

@BKSharma 

Thank you for marking my response as helpful.

Please mark my response as correct if that worked for you.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar 

I just need one more validation here in the else part of the code - How I can check the logged In User is a part of same assignment group or not in the same UI action button?

@BKSharma 

you can use display business rule and store g_scratchpad variable

that variable will hold and check if logged in user is part of same group or not

then access it in client side code of UI action.

I hope you are aware on how to create display BR and use scratchpad variable.

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