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

Sangeetha_Jegan
Tera Expert

Hi @athavichith 

  If the field is a reference field, then using g_form.getValue(fieldName) returns the sys_id of the corresponding reference record. If you want to get the actual value displayed, then use g_form.getDisplayValue(fieldName).

 

So, if your problem is to get the actual displayed value of that particular field instead of getting their sys_id, then replace occurences of g_form.getValue(..) with g_form.getDisplayValue(..).

 

If you find my post resolving your issue or if you find my post in any way useful, please mark it as helpful.

 

Thanks,

Sangeetha

 

 

Shivalika
Mega Sage

Hello @athavichith 

 

I don't think there is any g_form.getDisplayValue() method that would work on normal records, it only works on "service portal". It doesn't works for normal records. 

 

You may need to use GetReference method of g_form. 

 

Additionally, you need to use g_form.getOption(fieldName, value).text method to get the display value of choice fields. Otherwise it will give you backend values. (If both the backend and frontend values are same that's fine) 

 

Here's the modified script - 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }

 

    var service = g_form.getValue('service'); // Reference field

    var category = g_form.getValue('category'); // Choice field

    var subcategory = g_form.getValue('subcategory'); // Choice field

 

    // Get display values for choice fields

    var categoryDisplay = category ? g_form.getOption('category', category).text : '';

    var subcategoryDisplay = subcategory ? g_form.getOption('subcategory', subcategory).text : '';

 

    function setShortDescription(serviceName) {

        var shortDescription = '';

 

        if (serviceName) {

            shortDescription += 'Service: ' + serviceName;

        }

        if (categoryDisplay) {

            shortDescription += (shortDescription ? ' / ' : '') + 'Category: ' + categoryDisplay;

        }

        if (subcategoryDisplay) {

            shortDescription += (shortDescription ? ' / ' : '') + 'Subcategory: ' + subcategoryDisplay;

        }

 

        g_form.setValue('short_description', shortDescription);

    }

 

    if (service) {

        g_form.getReference('service', function(ref) {

            setShortDescription(ref ? ref.name : service);

        });

    } else {

     

  setShortDescription('');

    }

}

Please let me know if you face any issues. 

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

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

 

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

athavichith_0-1742839768724.png



I cannot make the category, subcategory into Choice field...cause I have 300+ options. 

Hello @athavichith 

 

I was assuming both of them to be choice field hence I gave that code. This is how we have it in OOB. 

 

Let me give you a refreshed code, since now I have the clarity that all three are reference fields. 

 

Below is the updated code - 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }

 

    var service = g_form.getValue('service');

    var category = g_form.getValue('category');

    var subcategory = g_form.getValue('subcategory');

 

    var shortDescription = [];

 

    function fetchDisplayValue(field, callback) {

        if (field) {

            g_form.getReference(field, function(record) {

                if (record && record.name) {

                    callback(record.name);

                } else {

                    callback('');

                }

            });

        } else {

            callback('');

        }

    }

 

    fetchDisplayValue('service', function(serviceName) {

        if (serviceName) shortDescription.push('Service: ' + serviceName);

 

        fetchDisplayValue('category', function(categoryName) {

            if (categoryName) shortDescription.push('Category: ' + categoryName);

 

            fetchDisplayValue('subcategory', function(subcategoryName) {

                if (subcategoryName) shortDescription.push('Subcategory: ' + subcategoryName);

 

                g_form.setValue('short_description', shortDescription.join(' / '));

 

          });

        });

    });

}