UI Page: How to pre-populate reference fields?

straightforward
Kilo Explorer

Dear all,

 

I have been cracking my head for hours, looked through various discussion threads and am still getting absolutely nowhere with this - thus, any help on is more than appreciated

 

I'm currently working on a UI page which essentially resembles a form to edit a given record. It works like a charm except for the reference fields which I just can't get to pre-populate based on a given record.

 

The UI page's xml includes several reference fields, e.g. :

 

<g:ui_reference name="approved_by" id="approved_by" table="sys_user" show_lookup="true"/>

 

How would I go about populating this field from the client script, given that I have the reference record's sys_id? I have explored various options (just to give you some keywords of what I've looked into: g_form, setValue, setDisplayValue, getElementsByName, getElementById)

 

Many thanks in advance,

straightforward

1 ACCEPTED SOLUTION

Kalaiarasan Pus
Giga Sage

try this ...



gel('approved_by').value = sysID;


gel('sys_display.approved_by').value = displayValue;


View solution in original post

3 REPLIES 3

Kalaiarasan Pus
Giga Sage

try this ...



gel('approved_by').value = sysID;


gel('sys_display.approved_by').value = displayValue;


Thanks, you just made my day!


Jared Healy2
Kilo Expert

Hey Christoph,



One method I've used to avoid the UI page complexities for these types of things is to use a Record Producer. Normally a record producer is used to insert a new record, but you can simply add the below line to the producer script and it won't insert anything if you set the abort action:



// Query for the record you want to update


var gr = new GlideRecord('table_to_update');


gr.get(producer.selectedRecord);



// Set values based on the producer variables


gr.comments = producer.comments;


gr.update();



// Override the normal producer behavior to insert a record


current.setAbortAction(true);



The benefit of this is the following:


  1. You gain access to standard g_form client side functions
  2. Utilizes variables that are easy to work with
  3. You can still control basic form layout, look and feel
  4. You can have multiple client scripts named by functionality just like a catalog item
  5. You have access to UI Policies as well


Hope that helps!



Jared Healy