Search code examples
xpagesxpages-ssjs

XPages SSJS Action Group


I have an action group on click of a button. First action, does some validation, second action throws up a confirm (Do you want to save?) And if yes, the third action goes off and does some other stuff.

The issue I have, is if validation fails on the first action, I don't want the other 2 actions to run, so I should get no confirmation etc.

If validation fails, I've tried doing a break, and a return false, but neither seem to work. I'm sure I'm missing something obvious, and suffering from Monday syndrome, but I can't seem to work it out!

Event handler code below, thanks:

        <xp:eventHandler event="onclick" submit="true"
        refreshMode="complete">
        <xp:this.action>
            <xp:actionGroup>

                <xp:executeScript>
                    <xp:this.script><![CDATA[#{javascript:for (var i = 1; i < viewScope.rows+1; i++) {
print("Starting Array.....");

var fieldName:string = "ObjectiveSelfAssessment" +i;
var fieldName2:string = "ObjectiveDetails" +i;
print ("Field Name: " + fieldName);

var fieldValue = document1.getItemValueString(fieldName);   
var fieldValue2 = document1.getItemValueString(fieldName2); 

print ("Field Value: " + fieldValue);
if (fieldValue =="" || fieldValue==null){
    print("Assessment Empty");
    if(!fieldValue2 =="" || !fieldValue2 == null){
        print("Objective Empty");
        //Do validation
        var o = {};
        o.title = "Validation Failed";
        o.body = "You must enter self assessment details for each objective";
        o.alertIcon = "fa-thumbs-down fa-lg";
        o.autoClose = true;
        o.alertType = "danger";
        requestScope.put("alertServer",o);
        //requestScope.put("validated",false);
        return false;
        break;
    }           
}
}
}]]></xp:this.script>
                </xp:executeScript>
                <xp:confirm>
                    <xp:this.message><![CDATA[#{javascript:"Are you sure you want to submit your self assessment?"}]]></xp:this.message>
                </xp:confirm>

                <xp:executeScript>
                    <xp:this.script><![CDATA[#{javascript:document1.replaceItemValue("rows",viewScope.rows);
//document1.replaceItemValue("status","Self Assessment Completed");
document1.save();
var o = {};
o.title = "Document Saved";
o.body = "This document has been succesfully submitted";
o.alertIcon = "fa-thumbs-up fa-lg";
o.autoClose = true;
o.alertType = "success";
requestScope.put("alertServer",o);
}]]></xp:this.script>
                </xp:executeScript>
            </xp:actionGroup>
        </xp:this.action>
    </xp:eventHandler>

Update 1: Conditional code on action group:

<xp:eventHandler event="onclick" submit="true" refreshMode="complete">
    <xp:this.action>
        <xp:actionGroup>
            <xp:this.condition><![CDATA[#{javascript:for (var i = 1; i < viewScope.rows+1; i++) {
print("Starting Array.....");

var fieldName:string = "ObjectiveSelfAssessment" +i;
var fieldName2:string = "ObjectiveDetails" +i;
print ("Field Name: " + fieldName);

var fieldValue = document1.getItemValueString(fieldName);   
var fieldValue2 = document1.getItemValueString(fieldName2); 

print ("Field Value: " + fieldValue);
if (fieldValue =="" || fieldValue==null){
    print("Assessment Empty");
    if(!fieldValue2 =="" || !fieldValue2 == null){
        print("Objective Empty");
        var o = {};
        o.title = "Validation Failed";
        o.body = "You must enter self assessment details for each objective";
        o.alertIcon = "fa-thumbs-down fa-lg";
        o.autoClose = true;
        o.alertType = "danger";
        requestScope.put("alertServer",o);
        print("FALSE");
        return false;
        break;
    }else{
        print("TRUE");
        return true;
    }               
  }
}
}]]></xp:this.condition>

            <xp:confirm>
                <xp:this.message><![CDATA[#{javascript:"Test11111"}]]></xp:this.message>
            </xp:confirm>
            <xp:executeScript>
                <xp:this.script><![CDATA[#{javascript:document1.replaceItemValue("rows",viewScope.rows);
//document1.replaceItemValue("status","Self Assessment Completed");
document1.save();
var o = {};
o.title = "Document Saved";
o.body = "This document has been succesfully submitted";
o.alertIcon = "fa-thumbs-up fa-lg";
o.autoClose = true;
o.alertType = "success";
requestScope.put("alertServer",o);}]]></xp:this.script>
            </xp:executeScript>
        </xp:actionGroup>
    </xp:this.action></xp:eventHandler>

Solution

  • Action groups are useful for basic, minimum-coding actions. But, as you're seeing, "easier" point-and-click, configuration-driven actions mean flexibility is harder. Manual coding may seem harder, but makes flexibility easier. If you put a confirm action on an XPage, preview it, and look at the source, it will tell you what function you need to use for manual coding. When I looked at the XSP class about seven years ago, I think the function used was XSP.confirm() equivalent to a basic JavaScript confirm() call. Using that may make your code more readable and make flexibility easier.