10

Dynamically send email based upon different environment (DEV/QA/PROD)

 1 year ago
source link: https://blogs.sap.com/2022/10/31/dynamically-send-email-based-upon-different-environment-dev-qa-prod/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
October 31, 2022 2 minute read

Dynamically send email based upon different environment (DEV/QA/PROD)

2 4 146

Introduction

This blog helps you to solve the most common requirement to send the Email to diffrent list of recipients based on the different environments of SAP Cloud Platform Integration Tenant’s (eg-Dev,Prod,Qa).

Current Scenario:

In this scenario we are storing the Tenant URL and Email IDs of the recipients for different environment (prod,dev,test) in a single value mapping as shown below.

Why we are doing so?

To give us the flexibility to send Email (as per requirement) from different Cloud Platform Integration (CPI) Tenant as we move our iFlow from one enviroment to another.

STEP 1: Creating a Value Mapping to store the URLs and Email IDs.

Screenshot-2022-10-20-104636.png

STEP 2: Using a Groovy Script in your iFlow to use the Value Mapping.

Screenshot-2022-10-20-111617.png

I-Flow Explanation:

  • We are using a “Timer” to trigger the iflow on specified time.
  • Then using a “Groovy Script” to envoke the Value Mapping.
  • Now you can accces the header (set inside groovy) in the Mail adapter’s “To” field using the apache camel expression

Groovy Script:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.HashMap;
import com.sap.it.api.ITApiFactory;
import com.sap.it.api.mapping.ValueMappingApi;
import groovy.xml.*;

def Message processData(Message message) {

    def baseUrl = "${message.getHeaders().get('CamelHttpUrl')}".toString()
    def Environment="";
     def valueMapApi = ITApiFactory.getApi(ValueMappingApi.class, null);
    def DEVURL = valueMapApi.getMappedValue('SKEY', 'SIDENTIFIER', "DEVURL", 'TKEY', 'TIDENTIFIER');
    def PRODURL = valueMapApi.getMappedValue('SKEY', 'SIDENTIFIER', "PRODURL", 'TKEY', 'TIDENTIFIER');
    def TESTURL = valueMapApi.getMappedValue('SKEY', 'SIDENTIFIER', "TESTURL", 'TKEY', 'TIDENTIFIER');
    if(baseUrl.contains(PRODURL))
    {
        Environment="PRODDL"
    }
    else if(baseUrl.contains(TESTURL))
    {
        Environment="TESTDL"
    }
    else {
        Environment="DEVDL"
    }

     //Set mail recipient
       def mailDl = valueMapApi.getMappedValue('SKEY', 'SIDENTIFIER', Environment, 'TKEY', 'TIDENTIFIER');
        message.setHeader("Mail_DL",mailDl )
        message.setHeader("ENV", Environment)
    return message;
}

Groovy Script Explanation:

  • First, we are fetching all the tenant env. URLs using value mapping API. We are then storing this in different variables: DEVURL,TESTURL and PRODURL.
  • Using Camel Http we are fetching the base Url of the tenant from the header.
  • Now, we are comparing the tenant base url with variables (from earlier) to figure out the current environment.
  • Based on the current environment we are fetching the Email recipients ID from ValueMapping by using ValueMappingAPI.
  • Now, we are setting Mail ID in header dynamically.

Conclusion

So, to conclude this blog helps to send Email to different recipients based on the development tenants of SAP Clould Platform Integration(CPI).

If you have any queries, please feel free to ask your question in comments. I would request everyone to provide your feedback and like if this blog post finds helpful for you.

Thanks & Regards,

Souragopal


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK