client script

Vedavalli
Tera Expert

Hi

I have written this below client script . I have a checkbox field if that is checked a  reference field is becoming visible and mandatory so from that field record it should fetch title and detail and autopopulate in present form. but in title and detail I am getting value as undefined. Please correct my code.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    alert('hi');
    var pickSelected = g_form.getValue('pick_from_library');
    if (pickSelected == 'true') {
        alert('hlo');
        var toDo = g_form.getDisplayBox('to_do').value;
        alert(toDo);
        if (toDo) {
             alert(toDo.title);
            g_form.setValue('title', toDo.title);
           
            g_form.setValue('detail', toDo.detail);
        } else {
            alert('bye');
            g_form.clearValue('title');
            g_form.clearValue('detail');
        }
    }
    else{
        g_form.clearValue('title');
        g_form.clearValue('detail');
    }
    //Type appropriate comment here, and begin script below

}
2 REPLIES 2

Sandeep Rajput
Tera Patron
Tera Patron

@Vedavalli Please update your script as follows and see if it works.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    alert('hi');
    var pickSelected = g_form.getValue('pick_from_library');
    if (pickSelected == 'true') {
        alert('hlo');
        var toDo = g_form.getReference('to_do',callBack);
        
    }
    else{
        g_form.clearValue('title');
        g_form.clearValue('detail');
    }
    //Type appropriate comment here, and begin script below
	function callBack(toDo){
		alert(toDo);
        if (toDo) {
             alert(toDo.title);
            g_form.setValue('title', toDo.title);
           
            g_form.setValue('detail', toDo.detail);
        } else {
            alert('bye');
            g_form.clearValue('title');
            g_form.clearValue('detail');
        }
	}
}

Hope this helps.

Andrew_TND
Mega Sage
Mega Sage

Hi @Vedavalli 

Try the below, I just updated line 8 and replaced getValue() with getReference() due to the type of field. Let me know how you get on.

 

    if (isLoading || newValue === '') {
        return;
    }
    alert('hi');
    var pickSelected = g_form.getValue('pick_from_library');
    if (pickSelected == 'true') {
        alert('hlo');
        var toDo = g_form.getReference('to_do').value;
        alert(toDo);
        if (toDo) {
             alert(toDo.title);
            g_form.setValue('title', toDo.title);
           
            g_form.setValue('detail', toDo.detail);
        } else {
            alert('bye');
            g_form.clearValue('title');
            g_form.clearValue('detail');
        }
    }
    else{
        g_form.clearValue('title');
        g_form.clearValue('detail');
    }

 

Please mark as helpful or if its resolved the issue, CORRECT!