Sending Surveys to individual users not working

MuhammadN
Tera Contributor
So I've created a record producer with two options:
1. Send survey to all users in the Survey Users list within the survey. (it is working as expected and code highlighted in blue.)
2. Option to add users in a list container from the same Survey Users list (if an admin wants to send survey to a number of users, and not all users.) This is not working. If we add two users in the list container and send the survey, the message I'd receive is 'Invited 2.0 users to take the survey.' which is expected, but the assessments were not created. 

I even tried to log the assessment created which gives me a sys_id but no record is found in the system.

var
surveySysId = 'survey sys id';

if(producer.send_survey_to_all_predefined_list_of_customer_contacts == 1 || producer.send_survey_to_all_predefined_list_of_customer_contacts == 'true'){
    sendInvitations(surveySysId);
}else if(producer.select_the_users_to_send_surveys_to){
    sendSelectedInvitations(surveySysId, producer.select_the_users_to_send_surveys_to);
}else{
    gs.addInfoMessage('No request was triggered.');
}

function sendInvitations(surveyID){
    new SNC.AssessmentCreation().createAssessments(surveyID);
    gs.addInfoMessage('Survey assessments have been created for all predefined list of customer contacts.');
}

function sendSelectedInvitations(surveyID, selectedUsers) {

    var selectedUsersString = String(selectedUsers);
    var usersArray = selectedUsersString.split(',');
    var usersCount = 0;
   
    for (var i = 0; i < usersArray.length; i++) {
        var userId = usersArray[i].trim();
        if (userId) {
            var result = (new SNC.AssessmentCreation()).createAssessments('survey sys id','',userId);
            if (result) {
                usersCount++;
            } else {
                gs.addErrorMessage('Failed to create assessment for user ' + userId);
            }
        }
    }

    if(usersCount > 0){
        gs.addInfoMessage(gs.getMessage('Invited {0} users to take the survey.', [usersCount]));
    } else {
        gs.addInfoMessage(gs.getMessage('No invitation sent.'));
    }
}

current.setAbortAction(true);
1 REPLY 1

afaqali112
Tera Contributor

It seems that the issue lies in how the `createAssessments` method is being called and the hardcoding of the survey sys_id. Here's how you can address the problem:

1. Avoid Hardcoding the Survey Sys ID: Instead of hardcoding `'survey sys id'`, pass the `surveyID` parameter to the `createAssessments` method.

2. Ensure Users Are in the Metric Category:  Before creating assessments, check if the users are in the relevant metric category and add them if necessary.

Here’s an updated version of your code:

 

 

var surveySysId = 'id'; // Replace with your survey sys_id

if(producer.send_survey_to_all_predefined_list_of_customer_contacts == 1 || producer.send_survey_to_all_predefined_list_of_customer_contacts == 'true'){
    sendInvitations(surveySysId);
} else if(producer.select_the_users_to_send_surveys_to) {
    sendSelectedInvitations(surveySysId, producer.select_the_users_to_send_surveys_to);
} else {
    gs.addInfoMessage('No request was triggered.');
}

function sendInvitations(surveyID){
    new SNC.AssessmentCreation().createAssessments(surveyID);
    gs.addInfoMessage('Survey assessments have been created for all predefined list of customer contacts.');
}

function sendSelectedInvitations(surveyID, selectedUsers) {

    var selectedUsersString = String(selectedUsers);
    var usersArray = selectedUsersString.split(',');
    var usersCount = 0;
   
    usersArray.forEach(function(userId) {
        userId = userId.trim();
        if (userId) {
            // Check if user is in the metric category
            var createUserInCategory = new GlideRecord('asmt_m2m_category_user');
            createUserInCategory.addQuery('metric_category', metricCategorySysId);
            createUserInCategory.addQuery('user', userId);
            createUserInCategory.query();

            if (!createUserInCategory.next()) {
                createUserInCategory.initialize();
                createUserInCategory.metric_category = metricCategorySysId;
                createUserInCategory.user = userId;
                createUserInCategory.insert();
            }

            // Create the assessment
            var result = new SNC.AssessmentCreation().createAssessments(surveyID, '', userId);
            if (result) {
                usersCount++;
                gs.addInfoMessage('Assessment created with ID: ' + result + ' for user: ' + userId);
            } else {
                gs.addErrorMessage('Failed to create assessment for user ' + userId);
            }
        }
    });

    if(usersCount > 0){
        gs.addInfoMessage(gs.getMessage('Invited {0} users to take the survey.', [usersCount]));
    } else {
        gs.addInfoMessage(gs.getMessage('No invitation sent.'));
    }
}

current.setAbortAction(true);