Thursday, March 14, 2024

The sender will pass the data in JSON format without converting it to XML format. We need to read the particular field.

The sender will pass the data in JSON format without converting it to XML format. We need to read the particular field to check the condition of the router using the Groovy script.

Input:

{
  "Enquiry_Request": {
    "Enquiry": {
      "Enquiry_Type": "General",
      "First_Name": "Christopher",
      "Last_Name": "Rath",
      "Phone": "9988998877",
      "Email": "chris@gmail.com.dummy",
      "Note": "Hi, I am looking for the details of the product being launched",
      "Language": "EN"
    }
  }
}

Groovy Scripts:

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import groovy.json.*; 

def Message processData(Message message) {

def json = message.getBody(java.io.Reader);

def data  = new JsonSlurper().parse(json);

message.setProperty("EnquiryType", data.Enquiry_Request.Enquiry.Enquiry_Type);

return message;

}

Implementation Design:



Wednesday, December 27, 2023

In the Monitor tab, we need to capture the custom header value.

 In the Monitor tab, we need to capture the custom header value.

eg-po_number value

Step 1:

Content Modifier- In the content modifier, we need to store the particular value below the image. We have stored the po_number.

Input-
<root>
    <po_number>123</po_number>
</root>
        



Groovy Scripts: In the monitor tab, we need to create a custom header with any value, and then we need to use the below Groovy scripts.

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message){
      def messageLog= messageLogFactory.getMessageLog(message);
  if(messageLog != null)
{
      def po_number = message.getHeaders().get("po_number");
  if(po_number !=null)
  {
  messageLog.addCustomHeaderProperty("po_number",po_number);
  }
  }
return message;
}

Monitor Output: Now, we can see the particular field value in the monitor tab.













Thursday, May 12, 2022

Payload Logging using Groovy Script in SAP CPI.

For the future reference, we have a script to save the payload in the iflow and that can be viewed in the Tenant .

Go to Monitoring page in SAP CPI - Choose iflow - Logs - MPL Attachment - Click on "C4CRequestPayload" to view the payload for this example.


Groovy Scripts:

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

def Message processData(Message message) 

{

    def body = message.getBody(java.lang.String) as String;

    def messageLog = messageLogFactory.getMessageLog(message);

    if(messageLog != null){

        messageLog.setStringProperty("Logging#1", "Printing Payload As Attachment")

        messageLog.addAttachmentAsString("C4CRequestPayload:", body, "text/plain");

     }

    return message;

}

(OR)

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import com.sap.it.api.mapping.*;

import java.text.SimpleDateFormat;

import java.util.Calendar;


def Message processData(Message message) {

map = message.getProperties();

property_ENABLE_PAYLOAD_LOGGING = "TRUE";

if (property_ENABLE_PAYLOAD_LOGGING.toUpperCase().equals("TRUE")) {

def header = message.getHeaders() as String;

def body = message.getBody(java.lang.String) as String;

String timeStamp = new SimpleDateFormat("HH:mm:ss.SSS").format(new Date());

                String logTitleH = timeStamp + " Response Headers ";

String logTitleB = timeStamp + " Response Body ";

 def messageLog = messageLogFactory.getMessageLog(message);

if (messageLog != null) {

messageLog.addAttachmentAsString(logTitleH, header, "text/xml");

messageLog.addAttachmentAsString(logTitleB, body, "text/xml");

}

}

return message;

}

Groovy Script to Remove Special Characters (or) Replace String.

 We had a requirement to remove the special characters in the field using groovy script.


Groovy Script:

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import java.io.*;

def Message processData(Message message) 

{

def body = message.getBody(java.lang.String) as String; 

body = body.replaceAll("&",""); 

message.setBody(body); 

return message; 

Groovy Script to read the data whenever C4C sends SAP_UUID or not. If SAP_UUID is not able then it will store Payload Logging using Groovy Script.

First using Request Reply we need to connect C4C(Cloud for Customer) System. From the C4C system it will send SAP_UUID some time it won't send SAP_UUID. If it's not send SAP_UUID then we need to store Payload Logging using below Groovy scripts.


Using Content Modifier, We need to store SAP_UUID after using groovy. We can check whether SAP_UUID is available or not.

Groovy Scripts:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import com.sap.it.api.mapping.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;

def Message processData(Message message) {
 
map = message.getProperties();

def SAP_UUID1=map.get("SAP_UUID")    

if(SAP_UUID1.equals(""))
{
def body = message.getBody(java.lang.String) as String;
def messageLog = messageLogFactory.getMessageLog(message);
if (messageLog != null) {
messageLog.addAttachmentAsString("C4C_Error_Response:", body, "text/xml");
 }
}
return message;
}

Router Condition:

After the Groovy script we need to use the router and check if SAP_UUID is not available at the end of the iflows, If SAP_UUID is available then it will go further.










Wednesday, April 27, 2022

Groovy Script to read the data in the same field level and send it all values in one field.

Using Groovy Scripts to read the same field level and send it all values in one field.

Read the input value in field name -SAPScriptLineText

Now 2 same fields come 2 different values i need to concat and send to receiver 1 value using groovy script.

Input:

<SAPScriptLineText>BG AV 302 LED BG AV 302 LED BG AV 302 LED BG AV 302 LED BG AV 302 LED BG</SAPScriptLineText>

<SAPScriptLineText>AV 302 LED BG AV 302 LED BG AV 302 LED</SAPScriptLineText>

Output:

<SAPScriptLineText>BG AV 302 LED BG AV 302 LED BG AV 302 LED BG AV 302 LED BG AV 302 LED BG AV 302 LED BG AV 302 LED BG AV 302 LED</SAPScriptLineText>

Example:

Below Script can use the inside Mapping of groovy script.

import com.sap.it.api.mapping.*;

def void addConcatText(String[] input, Output output, MappingContext context)
{
    String final_value = "";
    for(int i=0; i<input.length; i++)
    {
        final_value += input[i]
    }
    output.addValue(final_value)
}

Screenshot:

Groovy Script name: addConcatText.









Friday, April 8, 2022

Groovy Scripts to read the date and time and reduce -5:30 hrs of date and time.

Using Groovy Scripts to read the date and time and reduce -5:30 hrs of date and time.

Read the input value in property name -ActivityStartDate

Example:

Below Script can use the inside Mapping of groovy script.

import com.sap.it.api.mapping.*;

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import java.text.SimpleDateFormat;

import java.util.TimeZone;

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;

def String customFunc(String arg1)

{

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");

def currentTime= arg1;

LocalDateTime datetime = LocalDateTime.parse(currentTime,formatter);

datetime=datetime.minusHours(5).minusMinutes(30);

return datetime;

}

                                                                     (OR)

Below Script can use the inside iflows of groovy script.

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import java.text.SimpleDateFormat;

import java.util.TimeZone;

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;

def Message processData(Message message) {

//Body

def body = message.getBody();

//Properties

map = message.getProperties();

value = map.get("ActivityStartDate");

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");

def currentTime= value;

LocalDateTime datetime = LocalDateTime.parse(currentTime,formatter);

datetime=datetime.minusHours(5).minusMinutes(30);

message.setProperty("CurrentDate",datetime);

return message;

}

Input:

3:00 PM 09-04-2022

3:00 AM 09-04-2022

Output:

9:30 AM 09-04-2022

9:30 PM 08-04-2022

The sender will pass the data in JSON format without converting it to XML format. We need to read the particular field.

The sender will pass the data in JSON format without converting it to XML format. We need to read the particular field to check the conditio...