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

Copy and set fields from referenced record when creating a new record from related list

tgutierrez
Tera Contributor

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. 

6 REPLIES 6

Hi @tgutierrez ,

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

@tgutierrez 

Few things to check:

1. Check if ContractSysId is Correctly Passed: In your client script, you are passing the sysparm_contract_id parameter to the GlideAjax. Ensure that newValue is indeed the correct ContractSysId.

 

2. Apply few logs :

 In Script Include:

  gs.info("Contract Sys ID: " + contractSysId);

  In client script: 

  g_form.addInfoMessage("Response: " + response.responseText);

 

3. Ensure Correct Field Names: Double-check that the field names you are using in g_form.setValue correspond to the actual field names on your form.

 

4. Synchronous GlideAjax: For simplicity, you might consider using synchronous GlideAjax in this scenario, as it ensures that the response is received before moving to the next line of code.

 

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);
    var response = ga.getXMLWait();
    parseResponse(response);

    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
        }
    }
}

 

Please mark my response correct/helpful, if it helps you..

Thanks