Add "Result of javascript function" as an option for a model's condition

  • 1
  • Idea
  • Updated 5 years ago
I'd like to be able to do a calculation and use the result of that as the value of a model's condition. For instance, pass a date into the page via URL, get another date from another model, compare the two and create some result, then use that result as a condition for another model on the page.
Photo of Peter Bender

Peter Bender, Champion

  • 6,246 Points 5k badge 2x thumb

Posted 5 years ago

  • 1
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
That's pretty intense! The main difficulty here is that in order for Conditions to have effect on your Models when your page is first loaded, the Conditions have to get evaluated server-side (e.g. in Apex / SOQL). So it would really have to be "result of Apex snippet" in order for this to work on page initial load.

However, if you didn't need that Model to be populated with existing data immediately on page load, but either just need this Condition to be set as a "default" for new rows the user needs to create, or were okay with delaying the load of data into this model until the page has loaded, then your idea of a Condition Value type of "Result of JavaScript snippet" WOULD be possible, but this Model would either need to be "lazy-loaded", or no data would initially be loaded.

Here's how you could do something like this now with some Inline JavaScript:



(function(skuid){
var $ = skuid.$;
// Wait until the page has finished loading
$(function(){
// Get a date passed in via URL, in format 'yyyy-mm-dd'
var startDate = skuid.page.params.startdate;
// Convert the date string into a JavaScript date
if (startDate) startDate = skuid.time.parseSFDate(startDate);

// Get a date from a model's first row, say a 'semester' model
var semesterModel = skuid.model.getModel('Semester'),
semester = semesterModel && semesterModel.getFirstRow(),
semesterStartDate = semester
&& semesterModel.getFieldValue(semester,'Start_Date__c',true);
// Convert the semester start date from a Salesforce date string
// to a Javascript date object
if (semesterStartDate) {
semesterStartDate = skuid.time.parseSFDate(semesterStartDate);
}

// If we have both dates, determine which is greater
if (startDate && semesterStartDate) {
// Perform some calculation to get a resulting date
var result = (startDate >= semesterStartDate)
? startDate : semesterStartDate;

// Use our result as a condition on another model
var sessionsModel = skuid.model.getModel('Sessions');
var sessionStartDateCondition
= sessionsModel.getConditionByName('sessionStartDate');
sessionsModel.setCondition(
sessionStartDateCondition,
skuid.time.getSFDate(result),
false // Do not affect cookies. Set to true to store in cookie
);
// Reload our sessions model's data
sessionsModel.updateData();
}

});

})(skuid);

Photo of Peter Bender

Peter Bender, Champion

  • 6,246 Points 5k badge 2x thumb
Right... that's a critical point about javascript - it will only run after page load, and thus after all the data has been retrieved.

So, just so I understand what's going on with your script above, the page would load, then this javascript code would run. So if you displayed fields from the "Sessions" model on the page, it would display the original data, not the reloaded data, right? So what would happen if someone tried to edit that data and save it? (understanding that is not the intent with this)