autopopulate field

Shir Sharvit
Tera Contributor

I added to the incident table a field called call back (u_call_back) of string type.
I need to autopopulate this field with the caller's mobile phone as default value.

ShirSharvit_0-1701252231451.png

 

What is the problem with the code?

ShirSharvit_1-1701252355680.png

 

 

7 REPLIES 7

Community Alums
Not applicable

Hi @Shir Sharvit ,

 

Create onChange Client Script as below will populate the Call back base on the Caller field:

JackTKQ_0-1701255725242.png

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

    if (newValue != oldValue) {
        var caller = g_form.getReference('caller_id', popCallerInfo);
    }

    function popCallerInfo(caller) {
        var callBackVal = caller.u_call_back;
        if (callBackVal != '') {
            g_form.setValue('u_call_back', callBackVal);
        }
    }
}

It does not work

I made the following code:

 

function onChange() {
var callerId = g_form.getValue('caller_id');

if (callerId) {
// Fetch the caller's record
var caller = new GlideRecord('sys_user');
if (caller.get(callerId)) {
// Get the mobile phone from the caller's record
var mobilePhone = caller.mobile_phone;

// Set the u_call_back field in the incident form
g_form.setValue('u_call_back', mobilePhone);
}
}
}

But it doesn't work for me in SOW - service operation workspace

Community Alums
Not applicable

@Shir Sharvit , if this is onChange - Client script on the Caller field, you don't have to specify g_form.getValue('caller_id'), it is in the newValue

 

And should avoid using GlideRecord in Client side script.

 

I just change the code to take mobile_phone from user record:

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

    if (newValue != oldValue) {
        var caller = g_form.getReference('caller_id', popCallerInfo);
    }

    function popCallerInfo(caller) {
        var mobileVal = caller.mobile_phone;
        if (mobileVal != '') {
            g_form.setValue('u_call_back', mobileVal);
        }
    }
}

Please try again, and test it in Incident form first because workspace sometimes will acts differently.