I want to get the number of SENT messages for a specific date range for a specific channel using getMessageCount(). So the "filter" is by 3 items: status=SENT startDate=2020-10-15 endDate=2020-10-16
This is the code I am using:
var client = new com.mirth.connect.client.core.Client('https://localhost:8443');
var loginStatus = client.login('myusername', 'mypassword');
if (loginStatus.getStatus() != com.mirth.connect.model.LoginStatus.Status.SUCCESS) {
logger.error('Unable to log-on the server (status ' + loginStatus.getStatus() + ')');
return;
}
var channelId = 'b544f44c-xxxxxxxxxx-xxxxxxx';
var count = client.getMessageCount(channelId,'status=SENT,startDate=2020-10-15,endDate=2020-10-16'); // THIS FAILS, because 2nd parameter is not a string
I am getting an error: Can't find method com.mirth.connect.client.core.Client.getMessageCount(string,string).
Syntax of getMessageCount is: getMessageCount(String channelId, MessageFilter filter)
So how can I construct/pass the "filter" parameter that includes the 3 items mentioned above? Please give a clear example.
To get the SENT count of "yesterday", below is solution and works well:
// create a client instance and initialize it with the server to which it should connect
var client = new com.mirth.connect.client.core.Client('https://localhost:8443');
// log on to the server
try{
var loginStatus = client.login($g('apiUsername'), $g('apiPassword'));
} catch(ex) {
throw 'Unable to log-on the server "' + server + '" Error: ' + ex.message;
}
// check if login was successful
if (loginStatus.getStatus() != com.mirth.connect.model.LoginStatus.Status.SUCCESS) {
logger.error('Unable to log-on the server (status ' + loginStatus.getStatus() + ')');
return;
}
var filter = new com.mirth.connect.model.filters.MessageFilter;
var calendar = java.util.Calendar;
var startDate = new calendar.getInstance();
var endDate = new calendar.getInstance();
startDate.add(calendar.DATE, -1); // set to YESTERDAY
endDate.add(calendar.DATE, -1); // set to YESTERDAY
startDate.set(startDate.get(calendar.YEAR), startDate.get(calendar.MONTH), startDate.get(calendar.DAY_OF_MONTH), 0, 0, 0);
startDate.set(calendar.MILLISECOND, 0);
// Set endDate to 1 millisecond right before Midnight
// Ex: 2020-10-15 23:59:59.999 (right before midnight)
endDate.set(endDate.get(calendar.YEAR), endDate.get(calendar.MONTH), endDate.get(calendar.DAY_OF_MONTH), 23, 59, 59);
endDate.set(calendar.MILLISECOND, 999);
filter.setStartDate(startDate);
filter.setEndDate(endDate);
var statuses = new java.util.HashSet();
var Status = com.mirth.connect.donkey.model.message.Status;
var channelId = 'b544f44c-xxxxxxxxxx-xxxxxxx';
statuses.add(Status.SENT);
filter.setStatuses(statuses);
var sentCount= client.getMessageCount(channelId, filter);
logger.info('Number of messages SENT Yesterday: ' + sentCount);