Skip to content
Snippets Groups Projects
valueProcess.js 5.28 KiB
import("Sql_lib");
import("Campaign_lib");
import("system.result");
import("system.translate");
import("system.vars");
import("system.neon");

var campaignParticipantsAmount = 0;

var targetTable = vars.get("$param.dataSourceTableName_param");
var selectionRowIds = JSON.parse(vars.getString("$param.campaignParticipantsRowIds_param"));
var selectedCampaignId = vars.get("$field.CAMPAIGN_ID");
var selectedCampaignStepId = vars.get("$field.CAMPAIGNSTEP_ID");
var isUpdate = vars.get("$field.isUpdate");

var participantCondition = "";
var messageString = "";
var resultValue = "false";

if(selectedCampaignId != '')
{
    /*
     * First determines how many of the selected are already in the campaign
     * If there are no valid participants at all or too many in respect of the
     * selected steps max number of participants, nothing will be added. 
     * A message describing this situation will be written.
     * 
     * If none of those criteria match, all participants are good to be added.
     */
    var countParticipantsAlreadyInCampaign = 0;
    var countParticipantsToAdd = 0;
    var countValidParticipantsToAdd = 0;
    var whereCondition = newWhere();
    var rowIdsComparisonField = "";
        
    //if theres a selection, we have to use it, otherwise use the condition
    if(selectionRowIds != null && selectionRowIds.length > 0)
    {
        countParticipantsToAdd = selectionRowIds.length;
        
        
        /*
         * The field against which will be checked, if the selection is already contained in the campaignStep
         * depends on wether isUpdate is true or not.
         * If true, the selected IDs are CampaignParticipantIds, otherwise contactIds.
         * This happens because on true the action is called in the participants filterview to move participants
         * between steps, on false new participants are in the process of being added to a step.
         */
        if(isUpdate == "true")
        {
            whereCondition.and("CAMPAIGNPARTICIPANT.CAMPAIGNSTEP_ID", selectedCampaignStepId);
            whereCondition.and("CAMPAIGNPARTICIPANT.CAMPAIGNPARTICIPANTID", selectionRowIds, SqlBuilder.IN())
        }
        else
        {
            whereCondition.and("CAMPAIGNPARTICIPANT.CAMPAIGN_ID", selectedCampaignId);
            whereCondition.and("CAMPAIGNPARTICIPANT.CONTACT_ID", selectionRowIds, SqlBuilder.IN())
        }
        countParticipantsAlreadyInCampaign = CampaignUtils.GetParticipantsCountByCondition(whereCondition);
    }
    else
    {
        participantCondition = JSON.parse(vars.getString("$param.campaignParticipantsCondition_param")).condition;
        
        let useRightJoinToGetOrgs = "false";
        if(targetTable == "ORGANISATION")
        {
            useRightJoinToGetOrgs = "true";
            
            //Necessary because organisations don't have a personid'