Example 1: Searching an invoice by its number.
This is a complete script.
Notice for beginners: The example constantly uses double quotation marks around JavaScript String literals. To include the double quotation mark itself in such a string, a backslash needs to be put in front. At run time the backslash will not be a character in the string. To insert one, two backslashes would be necessary.
function getInvoiceByNumber(invNumber)
{
if(invNumber.indexOf("\"") >= 0)
return null;
var filter = "invoiceNo=\"" + invNumber + "\"";
var theFile = myFRS.first();
if(theFile != null)
{
if(myFRS.next())
{
return null;
}
else
return theFile;
}
return null;
}
var invoiceNum = "064821";
var theInvoice = getInvoiceByNumber(invoiceNum);
if(theInvoice != null)
util.out("Company:" + theInvoice.company);
else
util.out("Invoice number " + invoiceNum + " does not uniquely exist.");
Example 2: A few hard-codes filters using the operator "begins with"
var filter1 = "company|~peach";
var filter2 = "company|~\"peach it\"";
var filter2b = "company|~'peach it'";
var filter2c = "company|~" + util.getQuoted("peach it");
Example 3: Testing a field for multiple search terms
function createFindOneFilter(fieldname, values)
{
if(typeof(fieldname) != "string") throw "Invalid call of createFindOneFilter(): the parameter \"fieldname\" must be a string";
if(typeof(values) != "object") throw "Invalid call of createFindOneFilter(): the parameter \"values\" must be an array of strings";
var valueExpr = util.getQuoted(values[0]);
var counter;
for(counter = 1; counter < values.length; ++counter)
{
valueExpr = valueExpr + "|" + util.getQuoted(values[counter]);
}
return "SETMETHOD(1)" + fieldname + "~" + util.getQuoted(valueExpr);
}
var colourfulCustomers = new Array("yellowfish", "greenhorse", "black frog", "blue panther");
var filterExpr = createFindOneFilter("company", colourfulCustomers);
var fileRS =
new FileResultset(
"ftOrder", filterExpr,
"orderDate+");
Example 4: A numeric range filter
var minVal = context.convertNumericToString(3.1415, context.getClientLang(), 4);
util.out(minVal);
var maxVal = context.convertNumericToString(10, context.getClientLang());
util.out(maxVal);
var numRangeFilter = "total>=" +minVal + " AND total<=" + maxVal;
Example 5: A date range filter
function getDaysInMonth(month, year)
{
if(month < 0 || month > 11)
throw "Month is out of bounds (0..11)";
switch(month) {
case 1:
if(year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0))
return 29;
else
return 28;
case 3:
case 5:
case 8:
case 10:
return 30;
default:
return 31;
}
}
var minVal = new Date();
minVal.setHours(0);
minVal.setMinutes(0);
minVal.setSeconds(0);
minVal.setMilliseconds(0);
var maxVal = new Date(minVal.getTime());
minVal.setDate(1);
maxVal.setDate(getDaysInMonth(maxVal.getMonth(), maxVal.getFullYear()));
var fmtPattern = "@yyyymmdd";
var dateRangeFilter = "orderDate>=" + util.convertDateToString(minVal, fmtPattern)
+ " AND orderDate<=" + util.convertDateToString(maxVal, fmtPattern);