Client script to get reference value

athavichith
Kilo Sage

I created custom app off the Task table. For short description, I want to set the field to the value of 'Service', 'Category', and 'Subcategory'. I have created client script to capture that, but it is getting the sys_id of the reference field. Here is my script:

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var service = g_form.getValue('service');
    var category = g_form.getValue('category');
    var subcategory = g_form.getValue('subcategory');

    if (service || category || subcategory) {
        var shortDescription = '';


        if (service) {
            shortDescription += 'Service: ' + service;
        }


        if (category) {
            shortDescription += (shortDescription ? ' / ' : '') + 'Category: ' + category;
        }


        if (subcategory) {
            shortDescription += (shortDescription ? ' / ' : '') + 'Subcategory: ' + subcategory;
        }


        g_form.setValue('short_description', shortDescription);
    }
}
2 ACCEPTED SOLUTIONS

Sangeetha_Jegan
Tera Expert

Alternative to getting display value instead of sys_id for a reference field in core UI could be to use getDisplayBox() function like so.

var category = g_form.getDispayBox('category').value

 

But as @Chaitanya ILCR mentioned, the better approach is to go with Before Business Rule if you want the change in Short Description to reflect on all 3 field's change of value.

 

Regards,

Sangeetha

View solution in original post

Amit Verma
Kilo Patron
Kilo Patron

Hi @athavichith 

 

You can make use of g_form.getDisplayBox('your_field_name').value method to get reference field value. Refer below example where I tried getting the value of the Caller field and pushing it to the Description Field.

 

AmitVerma_0-1742870725402.png

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var caller = g_form.getDisplayBox('caller_id').value;
    g_form.setValue('description', caller);
}

AmitVerma_1-1742870779234.png

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

View solution in original post

8 REPLIES 8

Chaitanya ILCR
Kilo Patron

Hi @athavichith ,

better convert this to a before update BR as you would have to create 3 onchange client script each one for service,category and subcategory for this to work 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var service = current.service.getDisplayValue();
    var category = current.category.getDisplayValue();
    var subcategory = current.subcategory.getDisplayValue();

    if (service || category || subcategory) {
        var shortDescription = '';


        if (service) {
            shortDescription += 'Service: ' + service;
        }


        if (category) {
            shortDescription += (shortDescription ? ' / ' : '') + 'Category: ' + category;
        }


        if (subcategory) {
            shortDescription += (shortDescription ? ' / ' : '') + 'Subcategory: ' + subcategory;
        }


        current.setValue('short_description', shortDescription);
    }

})(current, previous);

if you want this in client side 

create a client callable client script include with client scripts

 

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

Yes, I was thinking of the same. But may be he can make use of OnLoad client script, script remains the same. Just that it won't show the update immediately. 

 

however yes before update BR would also work, but it is incidents and it will trigger tooo many times and may not be best for performance impact reasons.

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

Sangeetha_Jegan
Tera Expert

Alternative to getting display value instead of sys_id for a reference field in core UI could be to use getDisplayBox() function like so.

var category = g_form.getDispayBox('category').value

 

But as @Chaitanya ILCR mentioned, the better approach is to go with Before Business Rule if you want the change in Short Description to reflect on all 3 field's change of value.

 

Regards,

Sangeetha

Amit Verma
Kilo Patron
Kilo Patron

Hi @athavichith 

 

You can make use of g_form.getDisplayBox('your_field_name').value method to get reference field value. Refer below example where I tried getting the value of the Caller field and pushing it to the Description Field.

 

AmitVerma_0-1742870725402.png

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    //Type appropriate comment here, and begin script below
    var caller = g_form.getDisplayBox('caller_id').value;
    g_form.setValue('description', caller);
}

AmitVerma_1-1742870779234.png

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.