Wednesday, 27 March 2019

Compare Date's using Groovy script in mapping in SAP CPI

If required to compare the two date's and provide the output to the field in mapping.


Groovy Script:


import com.sap.it.api.mapping.*;
import java.text.SimpleDateFormat;
import com.sap.aii.mapping.api.*;
import com.sap.aii.mapping.lookup.*;
import com.sap.aii.mappingtool.tf7.rt.*;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;

/*Add MappingContext parameter to read or set headers and properties
def String customFunc1(String P1,String P2,MappingContext context) {
         String value1 = context.getHeader(P1);
         String value2 = context.getProperty(P2);
         return value1+value2;
}
Add Output parameter to assign the output value.
*/
def void custFunc2(String[] startDateJob,String[] endDateJob, String[] Field,Output output,MappingContext context) {
   
boolean out = false;
Date date1;
Date date2;
long diff;
long days;

String[] bDate;
SimpleDateFormat formatter1=new SimpleDateFormat("yyyy-MM-dd");

for(int i=0;i < startDateJob.length;i++)
    {
       
              date1=formatter1.parse(startDateJob[i]); 

              for(int j=0;j < endDateJob.length;j++)
                           {

                           try{
              date2=formatter1.parse(endDateJob[j]);
              diff = date1.getTime() - date2.getTime();
              days = diff / (24 * 60 * 60 * 1000);
             
              out = (days.equals(1));
                                }
                           catch ( java.text.ParseException e)
                           { }
                          

    if (i==0 && j==0)
              {
     output.addValue(" ");
    }
   else if (days==1)
    {
     output.addValue(Field[i]);
    }}
}
}

Thanks Shruthi for the support on this script.



No comments:

Post a Comment