Copy a field value from one record to another reference field new record onLoad

Shiladitya Das
Tera Contributor

Hi All,

 

We've a requirement, let's a there is an alm_asset record opened in form view. This has two reference fields 'Account' (customer_account) and Location (cmn_location). Now, if I open the look up of location field and click on 'New' to create a new location record, I need 'Account' field (reference) in that new location to be auto field with the Asset's location.

 

Is there a way to achieve this?

1 ACCEPTED SOLUTION
7 REPLIES 7

johnfeist
Mega Sage
Mega Sage

Hi Shiladitya,

 

Once you go to the cmn_location you may be too late.  What you can do is try using a UI Action (Add Location or something like that).  In the scripting, you can create the new record, populate the account and then open that record via setRedirectURL.   Here's an example of what the code might look like:

function doCreateProblem() {
	var mySysID = current.update;
	var location = new GlideRecord("cmn_location");
	var sysID;
	location.setValue("u_account", current.customer_account.getDisplayValue());
//     add any other values that can be preset
	sysID = location.insert();
	mySysID = current.update();

	action.setRedirectURL(location);
	action.setReturnURL(current);
}

This is a simplified version of a UI Action that I have running on our instanaces.  Please let me know if you have any further questions.

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Amit Gujarathi
Giga Sage
Giga Sage

HI @Shiladitya Das ,
I trust you are doing fine.

You can use a UI policy to automatically set the value of the 'Account' field in the Location record when a new Location record is created from the Asset's form view.

To implement this, you need to follow these steps:

  1. Navigate to the 'Location' table and create a new UI policy.
  2. Set the 'Action' field of the policy to 'Run script'.
  3. In the 'Advanced' section of the policy, add the following script:
  4. Set the 'Applies on' field of the policy to 'Insert'.
  5. Set the 'Run scripts' field of the policy to 'Client'.
  // get the current asset record
  var asset = g_form.getUniqueValue();

  // get the value of the account field in the asset record
  var account = g_form.getValue('customer_account');

  // set the value of the account field in the location record
  g_form.setValue('account', account);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hi @Amit Gujarathi,

 

Thanks for the responding. I've tried the following but still not working.

Am I missing something here?

UI Policy:

Table = cmn_location

Global = True

Onload = True

Run script = True

Script: Execute if true - 

function onCondition() {
// get the current asset record
var asset = g_form.getUniqueValue();

// get the value of the account field in the asset record
var account = g_form.getValue('customer_account');

// set the value of the account field in the location record
g_form.s

etValue('account', account);

}

Screenshots:

 

Ui Policy.PNG

Ui Policy_Script.PNG

 

Slava Savitsky
Giga Sage

The parent window object can be accessed via this.opener from the reference popup. Create an onLoad client script for Location table with the following code:

 

if (this.opener && this.opener.g_form)
	g_form.setValue('account', this.opener.g_form.getValue('customer_account'));

 

 

You could also add another condition to make sure it only works if the popup was opened from an alm_asset form:

if (this.opener && this.opener.g_form && this.opener.g_form.getTableName() == 'alm_asset')
	g_form.setValue('account', this.opener.g_form.getValue('customer_account'));