Tuesday 11 September 2018

Remove special characters - Groovy Script -SAP HCI/CPI

We had a requirement to remove the special characters in the field in mapping and also remove the prefix "0".

After this if the value starts with 63 or 61 then + should be added at prefix and send to target field

Note: You can find the usage for if condition, replaceAll, startsWith, replaceFirst function, concat, Boolean function

Input: 

"000ew63f@gAH-Je5^%7r/df&6!)78g"

Output:

6357678


Code:


import com.sap.it.api.mapping.*;
def String customFunc(String arg1){
def str5 = ''
def str4 = '+'
// Remove the special characters
       def str2 = arg1.replaceAll("[\\*\\?/()\\-\\^!@_+%.&#a-zA-Z]","")
// remove the leading zero's
def str3 = str2.replaceFirst ("^0*", "")
//condition to check the 63 or 61
if (str3.startsWith("63") || str3.startsWith("61"))
{
  
   str5 = str4.concat(str3)
}

return str5

}

or 

import com.sap.it.api.mapping.*;
def String customFunc(String arg1, String arg2, String arg3){
def len1
def len2
len1 = arg1.length()
len2 = arg2.length()
if( len1 > 0 )
{
def str2 = arg1.replaceAll("[\\*\\?/()\\-\\^!@_+%.&#a-zA-Z]","")
def str3 = str2.replaceFirst ("^0*", "")
def str5
def str6
def str7
if (str3.startsWith("63") || str3.startsWith("61"))
{
    def str4 = "+"
    str5 = str4.concat(str3)
return str5  
}
else 
{
    if( (len2 == 0) && (arg3 == "Melbourne" || arg3 == "Sydney"  )){
  str6 = "+61"
  str7 = str6.concat(str3)
  return str7
    }
}
}
else
{
                return arg1
}
}



6 comments:

  1. I'm seeing all the postings that really help me to use in my scenarios. Can you explain the method on how to get changes from lastrun?

    ReplyDelete
    Replies
    1. You will have the option of version management in Web UI, Whenever you want to deploy the changes can be saved as version(if require) and if required, then last changes can be reverted, as done in SAP PI/PO. Kindly check and let me know if you have any doubts

      Delete
  2. hi sir you posts are very useful and i have a doubt that how to change a field value by using groovy in sap Hci WEB UI.

    ReplyDelete
  3. Hi, Could you please elaborate your queries to clarify better.

    ReplyDelete
  4. Hi Raju, I am checking your blogs, your helping a lot. I know next question is not relevant with the point described here but not sure where to raise this question. I know that we can modify headers and property values using groovy. we have been doing this many times, but I would like to modify the value of a property from a user defined on message mapping. I can easily access and get the value, i.e. String PropertyValue = context.getProperty(property); but is it possible to set the value from the UDF? i.e. context.setProperty("property name", "property value"). Thanks!

    ReplyDelete
  5. Hi, Refer this blog:https://sapcpidesign.blogspot.com/2018/02/add-headerproperty-values-in-message.html , which will give the information to ready the property value in mapping.. use the UDF to ready the value and use this for your case. In case of any doubts revert back.

    ReplyDelete