CapaJC
ServiceNow Employee
ServiceNow Employee

In another post, someone just asked how to copy a survey and all its questions and their choices. Doing that manually can be a big hassle depending on the size of the survey.

If you create a form button UI Action with the following, you'll have a quick and easy way to copy a Survey Master part and parcel.


Name: Copy Survey Master
Table: Survey (survey_master)
Form button: checked
Hint: Create a copy of this survey, including its Questions and their Choices
Script: as follows



copySurvey();

function copySurvey() {
var oldID = current.sys_id.toString();
var newName = "Copy of " + current.name;
current.name = newName;
var newID = current.insert();

var question = new GlideRecord("survey_question_new");
question.addQuery("master", oldID);
question.query();
while (question.next()) {
var oldQID = question.sys_id.toString();
question.master = newID;
var newQID = question.insert();

var choice = new GlideRecord("question_choice");
choice.addQuery("question", oldQID);
choice.query();
while (choice.next()) {
choice.question = newQID;
choice.insert();
}
}
gs.addInfoMessage("Survey created: " + newName);
}