Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Insertion of survey response in custom table

mahiakash
Tera Contributor

Hello 

I have a requirement where i need to insert survey responses in custom table when survey is submitted. So i have created after business rule on "asmt_assessment_instance" table and condition is state changes to completed. i am writing below script. For now i have 5 fields in survey so when survey is submitted it will 5 records in metric result table. i am gliding metric result table and getting all the survey answers and stored in varibles and inserting new record in custom table by giving values. I just want to know is there any better way to do this. If survey fields are more then i may need to declare more varibles.

var assetname ;
var stakeholder ;
var payable ;
var waiver ;
var recovery ;
var gemetric = new GlideRecord('asmt_metric_result');
gemetric.addQuery('instance', current.sys_id);
gemetric.query();
while (gemetric.next()) {
if (gemetric.metric.name == 'Asset Name') {
assetname = gemetric.string_value;

} else if (gemetric.metric.name == 'Stakeholder Name') {
stakeholder = gemetric.string_value;

} else if (gemetric.metric.name == 'Payable Amount') {
payable = gemetric.string_value;

} else if (gemetric.metric.name == 'Waived Amount') {
waiver = gemetric.string_value;

} else if (gemetric.metric.name == 'Recovery Amount') {
recovery = gemetric.string_value;

}

}

var grseparation = new GlideRecord('sn_hr_core_ele_separation_metrics');
grseparation.initialize();
grseparation.u_stakeholder_name = stakeholder;
grseparation.u_asset_name = assetname;
grseparation.u_waived_amount = waiver;
grseparation.u_recovery_amount = recovery;
grseparation.u_payable_amount = payable;
grseparation.insert();

2 REPLIES 2

Rao Vamshi
Kilo Guru

Hi @mahiakash 

Looks like, you are trying to insert survey responses into a custom table in ServiceNow using a GlideRecord script. This approach can work, but it may become burdensome if you have a large number of survey fields, as you would need to declare a variable for each field.

One alternative approach you could consider is using the ServiceNow Survey application, it allows you to create surveys on custom tables and does automatically stores the responses in the corresponding survey response table.

This can simplify the process of collecting and storing survey responses, as you do not need to write custom scripts to handle the data.

Let me know if your require any additional help!



Thanks,
Vamshi

Hello

I need to insert all the responses as single record. But as per OOB it will insert each question as separate record.

 

Thanks