Client Script: Show Name Only from Reference Field

beycos
Tera Contributor

Hello,

Currently, in the CMDB table, the "Display Name" field is set to Display = true, and it shows the value in the format (name - company name). However, in table X, I want to use only the name field (not the full display name) in a reference field. As far as i know  whatever field is marked with Display=True it will show that value once reference value is selected Without touching the Display=True at dictionary I can't control what's seen there

I'm trying to find an alternative solution for this.

I created a new string field and I'm attempting to use a Client Script to populate it with only the part of the reference field's display value up to the dash (-).
After that, I plan to hide the reference field.

I wrote the following script, but it's not working yet.

 

function onLoad() {
    g_form.getReference('u_business_product_or_service_name', function(ref) {
        if (ref) {
            var displayValue = ref.getDisplayValue();
            if (displayValue) {
                var shortValue = displayValue.split('-')[0].trim();
                g_form.setValue('u_short', shortValue); // Show only the part before dash
            }
        }
});
}
Could I get your opinion or suggestions on this?

Thank you 

Beyza

2 ACCEPTED SOLUTIONS

Mohammad Danis1
Giga Guru

Hi @beycos ,

ref.getDisplayValue() is not available in client-side scripts. That method is server-side only. Instead, you should use g_form.getDisplayBox() or g_form.getDisplayValue() depending on your UI context.

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Mohammad Danish

View solution in original post

Mohammad Danis1
Giga Guru

Hi @beycos ,
I hope you are doing well !

Did my response help resolving your issue ?

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards,
Mohammad Danish

View solution in original post

5 REPLIES 5

Mohammad Danis1
Giga Guru

Hi @beycos ,

ref.getDisplayValue() is not available in client-side scripts. That method is server-side only. Instead, you should use g_form.getDisplayBox() or g_form.getDisplayValue() depending on your UI context.

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Mohammad Danish

Thank Mohammad . It worked now 

Rafael Batistot
Tera Sage

Hi @beycos 

 

The issue is here 

 

var displayValue = ref.getDisplayValue();

Try some like this and let me know if work

 

function onLoad() {
g_form.getReference('u_business_product_or_service_name', function(ref) {
if (ref) {
var nameOnly = ref.name; // Directly get the "name" field
g_form.setValue('u_short', nameOnly);
}
});
}

Mohammad Danis1
Giga Guru

Hi @beycos ,
I hope you are doing well !

Did my response help resolving your issue ?

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards,
Mohammad Danish