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

How to auto populate location in REQ

SN2024
Tera Contributor

Hi, 

Can someone please advise how I can configure REQ form to populate location based on the "Requested for" user?

 

I have been able to create business rule to populate the location for the new requests but for the existing ones I can't figure out what I need to select in Form Builder under location to make it work.

I have tried dot walking in location, dependent field to the "Requested for" location but that didn't work.

 

Thank you for your help.

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@SN2024 

you can use a fix script to update it for older REQs

Something like this

updateLocation();

function updateLocation() {
    try {
        var req = new GlideRecord('sc_request');
        req.addEncodedQuery('locationISEMPTY');
        req.query();
        while (req.next()) {
            req.location = req.requested_for.location;
            req.setWorkflow(false);
            req.update();
        }
    } catch (ex) {
        gs.info(ex);
    }
}

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

View solution in original post

5 REPLIES 5

Rafael Batistot
Kilo Patron

Hi @SN2024 

 

Consider create an OnChange Client Script

  • Table: sc_request
  • Type: onChange
  • Field name: requested_for
  • script

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

// Get the location of the selected requested_for user
var ga = new GlideAjax('GetUserLocation');
ga.addParam('sysparm_name', 'getLocation');
ga.addParam('sysparm_user', newValue);
ga.getXMLAnswer(function(response) {
if (response) {
g_form.setValue('location', response);
}
});
}

 

Now Create the Script Include:

Name: GetUserLocation

Client Callable: (Check this!)

Script:

 

var GetUserLocation = Class.create();
GetUserLocation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getLocation: function() {
var userID = this.getParameter('sysparm_user');
var userGR = new GlideRecord('sys_user');
if (userGR.get(userID)) {
return userGR.location.toString();
}
return '';
}
});

 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

mattmm
Kilo Sage

Have you considered running a simple background script 

var gr = new GlideRecord('sc_request');
gr.addNullQuery('location')       //Only where locaiton is currently empty
gr.query();

var count = 0;

while(gr.next()) {
    var user = new GlideRecrod('sys_user');
    if (user.get(gr.requested_for)) {
        if (user.location) {
            gr.location = user.location;
            gr.update();
            count++;
        }
    }
}

gs.print('Updated ' + count + ' sc_request records with location from requested_for user.');

Prasanna_Patil
Tera Guru
Tera Guru

Hi @SN2024 Try using onchange client script and script include for it..

Please hit like and Mark Helpful if you liked it
Regards,
Prasanna

Shubham_Jain
Mega Sage
Mega Sage

@SN2024 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.getReference('requested_for', function(user) {
g_form.setValue('location', user.location); // use .location for reference field
});
}

✔️ If this solves your issue, please mark it as Correct.


✔️ If you found it helpful, please mark it as Helpful.



Shubham Jain