cancel
Showing results for 
Search instead for 
Did you mean: 

API call to fetch data based on Current Month in CPI

babruvahana
Contributor
1,186

Hi Experts,

I have a requirement to make an API call to get the invoices if they are created on that current month.

But, the created date in the invoice is in Unix timestamp.

My approach is as below:

* Convert the first day and last of the current month range to Unix Timestamp and store it in a property.

*Use that property in the API query to fetch. So this will be a dynamic API call for each run of the IFlow.

So, looking for groovy scripts or any better alternative approaches, If any.

Regards,

Pavan G

Accepted Solutions (1)

Accepted Solutions (1)

MortenWittrock
Active Contributor

Hi Pavan

Your approach is sound. However, the devil is in the detail.

If the API requires two timestamps A and B, the range probably includes A, but does it exclude B? I.e. does it find timestamps A <= x < B or A <= x <= B? The former case is easier (and probably more likely), because B is then the timestamp of the first day of the next month.

Also, that leaves the question of actually finding those timestamps. Keep in mind that the server might not be (and probably isn't) in the same time zone as you. Therefore, make sure to explicitly state your time zone.

Here's some code using the Java 8 java.time package to find the timestamps:

ZonedDateTime midnightToday = ZonedDateTime.now(ZoneId.of("UTC+2")).truncatedTo(ChronoUnit.DAYS)
ZonedDateTime firstOfThisMonth = midnightToday.withDayOfMonth(1)
ZonedDateTime firstOfNextMonth = firstOfThisMonth.plusMonths(1)

long timestampLower = firstOfThisMonth.toEpochSecond()
long timestampUpper = firstOfNextMonth.toEpochSecond()

Remember to adjust the time zone! The UTC+2 one above is for Denmark 🙂 Also, the code requires the following imports:

import java.time.ZonedDateTime
import java.time.ZoneId
import java.time.temporal.ChronoUnit

Let me know how it works out.

Regards,

Morten

babruvahana
Contributor
0 Kudos

Hi Morten,

Thanks for the details:

I am facing issues with my Groovy Script { I am trying to convert it to Groovy Script from Java Script }

The output of fd gives me = 120-4-1 instead of 2020-04-1

Can you help me resolving this code ?

If this code works, I will use the same code for the last day of the previous month.

Then in API query, I will use FDPM >= X <=LDPM.

def Message processData(Message message) {
def firstDay = new Date();
//setting date to first day of previous Month
   firstDay.setDate(1);
def month = firstDay.getMonth();
def day = firstDay.getDate();
def year = firstDay.getYear();

//generating first day of previous month
def fd = year + "-" + month + "-" + day;

//generating the first day of previous month with timezone 
def tz = new Date(fd);
def fdpm=d.setTime(tz.getTime()/1000);

println month
//Set the first day of the previous months to Property
//message.setProperty("FDPM",fdpm);
    }
MortenWittrock
Active Contributor
0 Kudos

Hi babruvahana

I've already provided you with code that works. Which took some research (= time), mind you. So I'm not inclined to debug a completely different script as well.

Regards,

Morten

babruvahana
Contributor

Hi 7a519509aed84a2c9e6f627841825b5a,

I missed setting properties when I was using your code.
I corrected, it is working fine now.
Thanks a lot for providing me the code. It helped me a lot. 🙂


Regards,
Pavan

Answers (0)