Copy and set fields from referenced record when creating a new record from related list
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2024 03:50 PM
Hello all thanks in advanced for any help.
As an example lets say I have a record on the contract table and a related list on that record to a custom table with all the records that reference that contract record based on that contract number. Currently when i click the New Ui action in the related list on the contract record I'm getting the contract number being pre-populated just fine but there are also 4 other fields on the contract record I want to auto populated as well from that referenced contract record to the new custom table record.
I've done some research and it appears the easiest way to handle this is with a script include that grabs the field values from the contract and have an onChange client script call that script include and set the values. I'm not sure if that's the easiest method but I tried it and I keep getting a null value back from the Ajax call from the script include. I've been spinning my wheels on this for a while now and any help would be appreciated.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2024 04:09 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2024 04:25 PM
Yeah.
Script include:
var ContractAdjustmentHelper = Class.create();
ContractAdjustmentHelper.prototype = {
initialize: function() {},
getData: function() {
var contractSysId = this.getParameter('sysparm_contract_id');
var result = {};
var gr = new GlideRecord('ast_contract');
if (gr.get(contractSysId)) {
result.field_hours = gr.getValue('u_hours_per_block');
// Additional fields can be fetched and added to the result object similarly
}
return JSON.stringify(result);
},
type: 'ContractAdjustmentHelper'
};
Client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading && !g_form.isNewRecord()) {
return;
}
if (newValue === '') {
return;
}
g_form.addInfoMessage("Fetching contract details...");
var ga = new GlideAjax('ContractAdjustmentHelper');
ga.addParam('sysparm_name', 'getData');
ga.addParam('sysparm_contract_id', newValue);
ga.getXML(parseResponse);
function parseResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var data = JSON.parse(answer);
g_form.addInfoMessage('Data: ' + JSON.stringify(data));
if (data) {
g_form.setValue('field_hours', data.field_hours);
// Additional fields can be updated similarly
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2024 04:45 PM
Hi @tgutierrez ,
What is the format of "u_hours_per_block" field value.
Can you refer to the below article:
If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!
Thanks & Regards,
Sumanth Meda
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-13-2024 05:50 PM
It's a decimal but I also have a String field and a currency field I'm trying to get and set values for.
Thank you, that looks like it could be exactly what I was looking for