How can I Set Value to snRecordPicker if the rowCount is 1

sureshhegde
Kilo Contributor

Is there a way that I can set a default value or auto-populate a value into snRecordPicker widget in service-portal page, only if the table has only one record in it (if getRowCount is 1).

Looking for some good suggestions.

Thanks,

Ashwath 

1 ACCEPTED SOLUTION

Josh Virelli
Tera Guru

Hi Ashwath,

Without seeing any of your code, let me give a possible solution using an example.

Let's say this is my record picker:

<sn-record-picker field="location" table="'cmn_location'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" ></sn-record-picker>

Whatever value is set in the "field" parameter is what we can change via the client controller.

So to communicate to the client controller from the server script we have to use the data object. We'll use data.defaultValue. Let's say this is my Server script and if there is one location in Zimbabwe, then set data.defaultValue to that GlideRecord:

data.defaultValue;

var loc = new GlideRecord('cmn_location');
loc.addQuery('country','Zimbabwe');
loc.query();
if(loc.getRowCount() == 1){
data.defaultValue = loc;
}

 

Then in the client controller, if data.defaultValue exists, set the sn-record-picker to data.defaultValue;

if(c.data.defaultValue){
$scope.location = {
	displayValue: c.data.defaultValue.name,
	value: c.data.defaultValue.sys_id,
	name: 'location'
};
}

Let me know if that helps, and please mark my answer as correct if so. Otherwise I'll continue to help.

Thanks,

Josh

 

View solution in original post

3 REPLIES 3

Josh Virelli
Tera Guru

Hi Ashwath,

Without seeing any of your code, let me give a possible solution using an example.

Let's say this is my record picker:

<sn-record-picker field="location" table="'cmn_location'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" ></sn-record-picker>

Whatever value is set in the "field" parameter is what we can change via the client controller.

So to communicate to the client controller from the server script we have to use the data object. We'll use data.defaultValue. Let's say this is my Server script and if there is one location in Zimbabwe, then set data.defaultValue to that GlideRecord:

data.defaultValue;

var loc = new GlideRecord('cmn_location');
loc.addQuery('country','Zimbabwe');
loc.query();
if(loc.getRowCount() == 1){
data.defaultValue = loc;
}

 

Then in the client controller, if data.defaultValue exists, set the sn-record-picker to data.defaultValue;

if(c.data.defaultValue){
$scope.location = {
	displayValue: c.data.defaultValue.name,
	value: c.data.defaultValue.sys_id,
	name: 'location'
};
}

Let me know if that helps, and please mark my answer as correct if so. Otherwise I'll continue to help.

Thanks,

Josh

 

sureshhegde
Kilo Contributor

Hi Josh, 

Yup, that's the solution I have implemented right now. But I was looking, if I can achieve the same thing without querying the table again in server script just to check row count. Will I be able to do anything using the snRecordPicker properties or fields available and Client Script?

Sorry for not clearing this in my initial question.

Thank you very much for the response though.

Thanks,

Ashwath

Hmm.. I'm not aware of any way to do this without the server script portion. Sorry!