stop next action in javascript snippet by returning false - what am i doing wrong?

  • 1
  • Problem
  • Updated 9 months ago
  • Solved
  • (Edited)
I've referenced these 2 posts (here and here), and both say returning false should stop the successive actions from running...but I'm having no luck.

var params = arguments[0];
var step = params.step; var $ = skuid.$; // Get Incentive and Template Requirements models var models = skuid.model.map(); var requirements = models.TemplateRequirement; var incentive = models.Incentive; // Get fields from each model for use in validation var incentiveRow = incentive.getFirstRow(); var incentiveStart = incentive.getFieldValue(incentiveRow, "Incentive_Start_Date__c"); var incentiveEnd = incentive.getFieldValue(incentiveRow, "Incentive_End_Date__c"); // for each Template Requirement, validate the Milestone Date is within range of the Incentive Start and End Dates. $.each(requirements.getRows() ,function(){ var requirementRow = this; var milestone = requirements.getFieldValue(requirementRow, "Milestone_Date__c"); if (milestone < incentiveStart) { alert("Milestone Date(s) can not be prior to the Incentive Start Date"); return false; } else if (milestone > incentiveEnd) { alert("Milestone Date(s) can not be later than the Incentive End Date"); return false; } });

If I'm doing something blatantly obvious and wrong, please be kind :) This is the first block of code I've ever written from scratch.
Photo of Conlan O'Rourke

Conlan O'Rourke

  • 3,280 Points 3k badge 2x thumb

Posted 9 months ago

  • 1
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
Using the branch action will certainly do the trick.
Photo of Conlan O'Rourke

Conlan O'Rourke

  • 3,280 Points 3k badge 2x thumb
I thought about that, but what formula am I supposed to use?
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Conlan,

The problem is that you're returning false inside the $.each() function. That means you're exiting the .each(), but not returning false for the whole snippet.

Try something like this:
// for each Template Requirement, validate the Milestone Date is within range of the Incentive Start and End Dates.
var returnVal = true;
$.each(requirements.getRows() ,function(){
    var requirementRow = this;
    var milestone = requirements.getFieldValue(requirementRow, "Milestone_Date__c");
    if (milestone < incentiveStart) {
        alert("Milestone Date(s) can not be prior to the Incentive Start Date");
        returnVal = false;
        // return false; //also do this if you want to exit the .each()
    } else if (milestone > incentiveEnd) {
        alert("Milestone Date(s) can not be later than the Incentive End Date");
        returnVal = false;
        // return false; //also do this if you want to exit the .each()
    }
});
return returnVal;
(Edited)
Photo of Conlan O'Rourke

Conlan O'Rourke

  • 3,280 Points 3k badge 2x thumb
Matt, this worked great. Much appreciated for the assistance!