Solved

Can't pass date range into MP.api.jql()

  • 16 September 2019
  • 2 replies
  • 109 views

Userlevel 4
Badge

Goal: I'd like to pass a date range into a JQL query in my Platform app.

Issue: JQL request fails


Attempt 1

var dateRange = {
   from_date: moment().subtract(30, 'days').format('YYYY-MM-DD'),
   to_date: moment(),
};
console.log('dates', dateRange); // logs obj with properly formatted dates
MP.api.jql(function main() {
   return join(Events(dateRange), People(), {
      type: "inner",
      selectors: [{
         event: 'Signed In',
         selector: 'properties["sampleProp"] > 0'
      }]
   })
})

JQL Error: moment() is not defined


Attempt 2:

var dateRange = {
   from_date: '2019-09-01',
   to_date: '2019-09-15',
};
MP.api.jql(function main() {
   return join(Events(dateRange), People(), {
      type: "inner",
      selectors: [{
         event: 'Signed In',
         selector: 'properties["sampleProp"] > 0'
      }]
   })
})

JQL Error: dateRange is not defined


How can I pass a date range into MP.api.jql()?


icon

Best answer by evanmoss 17 September 2019, 02:23

Hi mpcomm1,


You need to pass your obj as the 2nd parameter of the mp.api.jql function, like so:

mp.api.jql(yourScript, dateRange).done(function(results) {console.log(results);});


In the query itself, you can access the dateRange as the variable params.


In your example, the code should be something like:


var dateRange = {
   from_date: '2019-09-01',
   to_date: '2019-09-15',
};

MP.api.jql(
function main() {
    return join(Events(params), People(), {
      type: "inner",
      selectors: [{
          event: 'Signed In',
          selector: 'properties["sampleProp"] > 0'
      }]
    })
},
dateRange).done(
function(results) {console.log(results);}
);

View original

2 replies

Badge +1

Hi mpcomm1,


You need to pass your obj as the 2nd parameter of the mp.api.jql function, like so:

mp.api.jql(yourScript, dateRange).done(function(results) {console.log(results);});


In the query itself, you can access the dateRange as the variable params.


In your example, the code should be something like:


var dateRange = {
   from_date: '2019-09-01',
   to_date: '2019-09-15',
};

MP.api.jql(
function main() {
    return join(Events(params), People(), {
      type: "inner",
      selectors: [{
          event: 'Signed In',
          selector: 'properties["sampleProp"] > 0'
      }]
    })
},
dateRange).done(
function(results) {console.log(results);}
);

Userlevel 4
Badge

That worked perfectly! Thank you @evanmoss


Reply