How to get the location from the asset table using asynchronous script?

Luis Roman1
Tera Contributor

I have a catalog form with label named "assigned_asset" when the submitter select an asset id in the catalog item, I need to have an OnChange script for the "assigned_asset" and pull the location from the asset table. I need to have a script asynchronous so that it does not impact the user experience and it is "best practice". I have the script below but not able to get anything back. Any help appreciated. Or should I try to do a Glide Ajax to pull the information? Please advise or let me know what need to do to fix the script my experience scripting is very limited.  Thank you for advise or guidance. 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    try {
       g_form.getReference('asset', callback);            /* asset table */    
function callback(ret) {
          g_form.setValue('location', ret.location);
          }
    } catch (ex) {
        g_form.addErrorMessage(" TYPE [on Asset Change] encountered the error: " + ex.message);
        g_form.addInfoMessage("TYPE [on Asset Change] encountered the error:" + ex.message);
    }
}

1 ACCEPTED SOLUTION

As far as I know, getReference() only works with reference fields/variables, so it will not work with Lookup SelectBox. I would recommend you to use GlideAjax for this requirement.

View solution in original post

9 REPLIES 9

Muhammad Khan
Mega Sage
Mega Sage

Hi Luis,

 

Can you please use alert('Testing: ' + ret.location); as a first statement in callback function. Just trying to debug your script.

A couple of things I want to know;

  1. Are you sure that you have used the correct variable name in g_form.getReference() ?
  2. ret.location will return the sys_id of location, so if you are setting this value in a reference variable then it is fine , otherwise you will have to use GlideAjax to get the display value of location.

 

For learning purpose, getReference() only supports one-level of dot walking.

 

Hopefully, we will resolve this query together.

 

Khan I put the alert as you indicated. Get the message "Testing:undefined". What I know is that the 

newValue has the value of the selected asset id.  Any other suggestion, please. 

Luis 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    try {
       g_form.getReference('assigned_asset', callback);            

        function callback(ret) {
    alert('Testing: ' + ret.location);
          g_form.setValue('location', ret.location);
    alert('Testing after:' + ret.location);
          }
    } catch (ex) {
        g_form.addErrorMessage(" TYPE [on assigned_asset Change] encountered the error: " + ex.message);
        g_form.addInfoMessage("TYPE [on assigned_asset Change] encountered the error:" + ex.message);
    }
}

Can you share screenshot of your variable details on which you have implemented this client script.

Khan, I attached pdf with the information that you requested. Really hope this help. Appreciate your patience and assistance. Hopefully the attached document will help. 

Luis