UI Action Button generating duplicate surveys

Igor Corr_a
Tera Expert

Hello,

 

I have a UI Action to generate surveys within the Projects form.

 

The problem is that, when generating the survey, it generates in duplicate, one for each user of the additional_assignee_list field with the u_project_object field filled in and the other two not.

 

What is expected: to generate a survey for each user of the additional_assignee_list field (field within the projects form) and with the field filled in.

 

Below is my script:

 

function checkAddicionalAssigneeList(){
    g_form.setMandatory("additional_assignee_list", true);
    gsftSubmit(null, g_form.getFormElement(), 'send_survey_for_project');
}

if (typeof window == 'undefined')
    sendSurveyAndReload();

function sendSurveyAndReload() {
    // Updates the registry before creating assessments
    current.update();

    // Gets the list of users from the additional_assignee_list field
    var assigneeList = current.additional_assignee_list.split(',');

    // Iterates on each user and creates an assessment for each one
    for (var i = 0; i < assigneeList.length; i++) {
        var assignee = assigneeList[i].trim();

        // Fetches the user's full name from sys_user
        var userGR = new GlideRecord('sys_user');
        userGR.get(assignee);
        var assigneeName = userGR.getDisplayValue('name');  // Gets the user's full name

        // Creates the assessment for the current user
        var assessmentCreation = new sn_assessment_core.AssessmentCreation();
        assessmentCreation.setUser(assignee);  // Defines the recipient user
        assessmentCreation.conditionTrigger(current, '0cf155cd1b60f11060d9426fe54bcb81');

        // Updates the u_project_object field in the assessment record
        var projectDisplayValue = current.sys_id;  // Obtém o display value do projeto

        // Searches for the last assessment created for the current record
        var assessmentGR = new GlideRecord('asmt_assessment_instance');
        assessmentGR.addQuery('trigger_table', current.getTableName());  // Filter by record table
        assessmentGR.addQuery('trigger_id', current.sys_id);  // Filter by the current project record
        assessmentGR.addQuery('trigger_condition', '0cf155cd1b60f11060d9426fe54bcb81');  // Filter by trigger
        assessmentGR.addQuery('user', assignee);  // Filter by recipient user
        assessmentGR.orderByDesc('sys_created_on');  // Sorts by creation date to pick the last one created
        assessmentGR.query();

        if (assessmentGR.next()) {
            // Fills the u_project_object field with the project's display value
            assessmentGR.u_project_object = projectDisplayValue;
            assessmentGR.update();  // Updates the assessment log

            // Displays a message informing you of the search number generated for each user with their full name
            gs.addInfoMessage("Pesquisa " + assessmentGR.number + " gerada com sucesso para o usuário " + assigneeName + ".");
        }
    }

    // Redirects to the updated record
    action.setRedirectURL(current);
}

Here's the problem. When generating the search, the following occurs:

IgorCorr_a_0-1728052197513.png

 

The columns with a blank value I have colored to hide sensitive data.


Definitions of my survey:

IgorCorr_a_1-1728052256884.png

IgorCorr_a_2-1728052291323.png

 

My survey trigger:

IgorCorr_a_3-1728052387110.png

 

I'm having trouble understanding what's causing the duplication.

 

I suspect it's the method used to fire the trigger.

 

Can anyone help me?

 

Best regards,

1 REPLY 1

Mani A
Tera Guru

function sendSurveyAndReload() {

    // Updates the registry before creating assessments

    current.update();

 

    // Gets the list of users from the additional_assignee_list field

    var assigneeList = current.additional_assignee_list.split(',');

 

    // Iterates on each user and creates an assessment for each one

    for (var i = 0; i < assigneeList.length; i++) {

        var assignee = assigneeList[i].trim();

 

        // Fetches the user's full name from sys_user

        var userGR = new GlideRecord('sys_user');

        if (!userGR.get(assignee)) {

            gs.addErrorMessage("User not found: " + assignee);

            continue; // Skip to the next assignee if the user is not found

        }

        var assigneeName = userGR.getDisplayValue('name'); // Gets the user's full name

 

        // Check if an assessment already exists for this user

        var assessmentGR = new GlideRecord('asmt_assessment_instance');

        assessmentGR.addQuery('trigger_table', current.getTableName());

        assessmentGR.addQuery('trigger_id', current.sys_id);

        assessmentGR.addQuery('trigger_condition', '0cf155cd1b60f11060d9426fe54bcb81');

        assessmentGR.addQuery('user', assignee);

        assessmentGR.query();

 

        if (!assessmentGR.hasNext()) {

            // Create a new assessment since it does not exist

            var assessmentCreation = new sn_assessment_core.AssessmentCreation();

            assessmentCreation.setUser(assignee);

            assessmentCreation.conditionTrigger(current, '0cf155cd1b60f11060d9426fe54bcb81');

 

            // Create the assessment record

            assessmentGR.initialize();

            assessmentGR.trigger_table = current.getTableName();

            assessmentGR.trigger_id = current.sys_id;

            assessmentGR.trigger_condition = '0cf155cd1b60f11060d9426fe54bcb81';

            assessmentGR.user = assignee;

            assessmentGR.u_project_object = current.sys_id; // Set the project object field

            assessmentGR.insert(); // Insert the new assessment record

 

            gs.addInfoMessage("Survey generated successfully for user " + assigneeName + ".");

        } else {

            // Optionally notify that an assessment already exists

            gs.addInfoMessage("Survey already exists for user " + assigneeName + ".");

        }

    }

 

    // Redirects to the updated record

    action.setRedirectURL(current);

}