Populate variables from sys user table based on 'Requested for'

rin1
Kilo Guru

Hi all,

Iam working on a catalog item. The requirement is to autopopulate 6 fields (Fname, Lname, Title, Department, Email,Phone,) based on Requested For field.

In the catalog item variables, I have set the default value for all the 6 variables like below, so when the form loads all these 6 fields are populated. So no issues on that part.

javascript:(gs.getUser().getRecord().getValue("first_name"));

javascript:(gs.getUser().getRecord().getValue("last_name"));

javascript:(gs.getUser().getRecord().getValue("title")); .......etc

 

If the 'requested_for' value is changed then all these values should be populated based on the user data. I have written an onchange client script but that doesnt seems working. Below is the script. Any guidance would be much appreciated.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    var gr = g_form.getReference('requested_for', getUser);
}
function getUser(gr){
    
    g_form.setValue('dccr_forename', gr.first_name);
    g_form.setValue('dccr_surname', gr.last_name);
    g_form.setValue('dccr_work_email', gr.email);
    g_form.setValue('dccr_jobtitle', gr.title);
    g_form.setValue('dccr_service_dpt', gr.department);

g_form.setValue('dccr_contact_number', gr.phone);

}

 

Thanks,

Rini

 

11 REPLIES 11

Hi Jaspal,

Followed this post https://community.servicenow.com/community?id=community_question&sys_id=196b8b21db9cdbc01dcaf3231f96... by Chuck Tomasi, to create a script inlude and client script. But it doesn't work , and it throws an error 'There is a javascript error in your browser console'.

Script Include:

find_real_file.png

Client Script:

find_real_file.png

Thanks,

Rini

Its working now, Dont ask me how....I have no idea..I have deleted all the scripts and started fresh and tada It worked!

SumanthDosapati
Mega Sage
Mega Sage

Hi,

Your scripts is correct. No issue in that.

May be you can put some alerts and see where it is getting broken.

 

Regards,
Sumanth

Hi Sumanth,

I was using a variable set, and now I have removed that and created new variables for Requester/COmpany/Requested For and deactivated all the client scripts and script include. Just wrote a basic onload client script and fields are still not populating. Its totally weird and I dunno why? Any thoughts on this?

function onLoad() {
    //Type appropriate comment here, and begin script below
    var user = g_form.getReference('dccr_requested_for', populateInfo);

    function populateInfo(user) {
        g_form.setValue('dccr_forename', user.first_name);
        g_form.setValue('dccr_surname', user.last_name);
    }
}

 

Thanks,

Rini

Aman Kumar S
Kilo Patron

Your script looks alright, can you confirm if your onCHange script is even working or not?

If it is set for requested for, add some alerts to verify if the CS is triggering also verify the name of fields

Best Regards
Aman Kumar