Passing the value of a field in a Table to Service Portal

Diogo Lemos
Tera Contributor

Hi guys,

 

I need a simple thing (it´s seems i guess) but right now is not working.

 

Basically i have a form that is part of a catalog item that contains different variables (see the image below).

 

DiogoLemos_0-1696005844725.png

In this form we have Agreed rate and i just want that this field to be fulfill like i have in the workspace (see the image below)

DiogoLemos_1-1696005988358.png


The client script that i did is like this: (the table that cointains the information is x_cioa2_hcx_t_engagement_hcx)
----------------------------------------------------------------------------------

function onChange(control, oldValue, newValue, isLoading) {
     if (isLoading || newValue == '') {
        return;
    }
    
    var hcxTable = new GlideRecord('x_cioa2_hcx_t_engagement_hcx');
    var agreedRate = hcxTable.addQuery('suggested_rate');
    hcxTable.query();
    var teste = g_form.getValue('final_travel_expenses');
    g_form.setValue('suggested_rate', agreedRate);
}
----------------------------------------------------------------------------------
Can someone help me with this?





 

5 REPLIES 5

johnfeist
Mega Sage
Mega Sage

Hi Diego,

 

Your immediate problem is that hcxTable.query();  doesn't position you on a record, it just gathers the data.  You need to use hcxTable.next() to move through the record(s).  Even more basic, unless you are passing a sys_id to a table, the query should have two parameters, field and value.  If you know that there can only be one value returned, you can also use hcxTable.get() which will return one record positioned.

 

Best practice is to do back end processing (which includes searching a table) in the back end via AJAX and then getting the relevant values from there.  The documentation for AJAX is pretty good and can be found here.

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Hi johnfeist,

I already change the code like you indicated

 

function onChange(control, oldValue, newValue, isLoading) {
     if (isLoading || newValue == '') {
        return;
    }
    
    var hcxTable = new GlideRecord('x_cioa2_hcx_t_engagement_hcx');
    hcxTable.addQuery('suggested_rate', '10');//add rate here
    hcxTable.query();
    if(hcxTable.next()){
		var agreedRate = hcxTable.getValue('suggested_rate');//Check if you have agreed rate field
		g_form.setValue('suggested_rate', agreedRate);
		g_form.addInfoMessage(agreedRate);
	}
	else{
		g_form.addInfoMessage('test');
	}
}

 

but i cannot go in the if condition and i dont know why the .next() is not working, do i need to do something more?

Can you help me?
 

Sandeep Rajput
Tera Patron
Tera Patron

@Diogo Lemos Try updating your code as follows.

 

 

function onChange(control, oldValue, newValue, isLoading) {
     if (isLoading || newValue == '') {
        return;
    }
    
    var hcxTable = new GlideRecord('x_cioa2_hcx_t_engagement_hcx');
    hcxTable.addQuery('suggested_rate','<provide rate here>');//add rate here
    hcxTable.query();
    if(hcxTable.next()){
    var agreedRate = hcxTable.getValue('agreed_rate');//Check if you have agreed rate field
    var teste = g_form.getValue('final_travel_expenses');
    var currency_code = 'USD';
    g_form.setValue('suggested_rate', currency_code + ';' + agreedRate);
}
}

 

Hope this helps.

Hi Sandeep Rajput,

I already change the code like you indicated

function onChange(control, oldValue, newValue, isLoading) {
     if (isLoading || newValue == '') {
        return;
    }
    
    var hcxTable = new GlideRecord('x_cioa2_hcx_t_engagement_hcx');
    hcxTable.addQuery('suggested_rate', '10');//add rate here
    hcxTable.query();
    if(hcxTable.next()){
		var agreedRate = hcxTable.getValue('suggested_rate');//Check if you have agreed rate field
		g_form.setValue('suggested_rate', agreedRate);
		g_form.addInfoMessage(agreedRate);
	}
	else{
		g_form.addInfoMessage('test');
	}
}
but i cannot go in the if condition and i dont know why the .next() is not working, do i need to do something more?

Can you help me?