cancel
Showing results for 
Search instead for 
Did you mean: 

Map values in CPI using groovy script does not work

mariusz_jurcz
Participant
0 Kudos

Dear Experts,

I tried to map message from payload consists from couple node into one string. In order to do that I used groovy script. In simulation, the mapping seems to be correct:

I want to append in "Text" some of the values from all nodes of transcripts ("element" - values: alias, date, message ).

This is the code for mapping:

import com.sap.it.api.mapping.*;
import groovy.util.*;
import java.util.HashMap;
import groovy.util.XmlSlurper;
import groovy.xml.XmlUtil;
import java.lang.String;
import groovy.lang.GString;



def void Append(String[] inp, Output message, MappingContext context) {
        String value1 = '';
         def root = new XmlSlurper().parseText(inp[0]);
        StringBuilder sB = new StringBuilder();

 root.element.each {
            ele ->

 sB << ele.alias;

 sB << ele.date;

sB << ele.message;

 sB << '\r\n';
};


value1 = sB.toString();

message.addValue(value1);
}

Message queue:

The problem is, that in integration flow the Field "Text" is empty, and element ServiceRequestTextCollection appears 3 times, but it supposed to be only one.

Can someone explain it to me why is it happening?


Best regards,

Mariusz Jurcz

Accepted Solutions (0)

Answers (1)

Answers (1)

Sriprasadsbhat
Active Contributor
0 Kudos

Hello Marius,

Could you please share the context of subsequent parent nodes of the fields mapped to Text.

Also it would be easy to provide solution if you paste some input XML and expected target XML ( maybe you can mask the sensitive info ).

Regards,

Sriprasad Shivaram Bhat

mariusz_jurcz
Participant
0 Kudos

Hello Sriprasad,

Here is input payload:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <browser>Chrome (3.0.195.27)</browser>
    <chat_duration>15</chat_duration>
    <chat_ended_by>agent</chat_ended_by>
    <chat_waittime>2</chat_waittime>
    <city>Boulder</city>
    <country>United States</country>
    <country_code>US</country_code>
    <created_at_date>Oct 3, 2014 2:14:31 PM</created_at_date>
    <created_at_milliseconds>268</created_at_milliseconds>
    <created_at_seconds>1412367271</created_at_seconds>
    <description>Testing the Integration API...</description>
    <entry_url>http://www.yoursite.com</entry_url>
    <id>xxxxxx</id>
    <ip_address>XX.XXX.ZZZ.YYY</ip_address>
    <javascript_variables>
        <element>
            <name>variable1</name>
            <value>v1_1234</value>
        </element>
        <element>
            <name>variable2</name>
            <value>v2_123456789</value>
        </element>
    </javascript_variables>
    <labels>
        <element>
            <name>note</name>
            <value>this is a note entered by the agent</value>
        </element>
        <element>
            <name>customerTemp</name>
            <value>Very Happy</value>
        </element>
    </labels>
    <language_code>en-US,en;q=0.8</language_code>
    <latitude>39.9147</latitude>
    <longitude>-105.0809</longitude>
    <operator_variables>
        <element>
            <name>note</name>
            <value>this is a note entered by the agent</value>
        </element>
        <element>
            <name>customerTemp</name>
            <value>Very Happy</value>
        </element>
    </operator_variables>
    <os>Microsoft Windows Vista</os>
    <page_url>http://www.webpage.html</page_url>
    <proactive_chat>false</proactive_chat>
    <referrer_url>http://www.comingfromurl.com</referrer_url>
    <region>16</region>
    <requested_by>test@test.com</requested_by>
    <IndividualCustomer>
        <Email>test@test.com</Email>
        <FirstName />
        <CustomerID>10160032</CustomerID>
        <LastName>Jim Tester</LastName>
        <RoleCode>CRM000</RoleCode>
    </IndividualCustomer>
    <requester_details>
        <address>111 2nd Ave.</address>
        <address_2 />
        <age>44</age>
        <avatars>
            <element>http://www.avatars.com/jim</element>
        </avatars>
        <city>Boulder</city>
        <company_name>Company</company_name>
        <company_profile_link>http://facebook.com/company</company_profile_link>
        <country>US</country>
        <emails>
            <element>test@example.com</element>
        </emails>
        <employees>Abe,Betty,Charlie</employees>
        <gender>Male</gender>
        <industry>Tech</industry>
        <influencer_score>88.8</influencer_score>
        <name>Jim Tester</name>
        <name_profile_link>http://facebook.com/jimtester</name_profile_link>
        <notes>This is just for testing</notes>
        <other_data />
        <phones>
            <element>111-111-1111</element>
        </phones>
        <revenue>100</revenue>
        <social_profile_links>
            <element>www.facebook.com/jim</element>
            <element>www.twitter.com/jim</element>
        </social_profile_links>
        <state>CO</state>
        <title>CEO</title>
        <website>www.company.com</website>
        <zip>80303</zip>
    </requester_details>
    <source_id>2</source_id>
    <transcripts>
        <element>
            <alias>visitor</alias>
            <date>Oct 3, 2014 2:14:31 PM</date>
            <date_milliseconds>154</date_milliseconds>
            <date_seconds>1412367271</date_seconds>
            <id />
            <message>Hola, </message>
            <translations>
                <en>Hello,</en>
            </translations>
        </element>
        <element>
            <alias>test</alias>
            <date>Oct 3, 2014 2:14:31 PM</date>
            <date_milliseconds>456</date_milliseconds>
            <date_seconds>1412367271</date_seconds>
            <id>jerome@company.com</id>
            <message>Good day</message>
            <translations>
                <es>Buen día</es>
            </translations>
        </element>
        <element>
            <alias>visitor</alias>
            <date>Oct 3, 2014 2:14:31 PM</date>
            <date_milliseconds>835</date_milliseconds>
            <date_seconds>1412367271</date_seconds>
            <id />
            <message>Esto es una prueba.</message>
            <translations>
                <en>This is a test.</en>
            </translations>
        </element>
    </transcripts>
    <type>chat</type>
    <url>https://www.company.com/viewcase?c=XXXX</url>
    <user_agent>Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/532.0 (KHTML, like Gecko) Chrome/3.0.195.27 Safari/532.0,gzip(gfe)</user_agent>
    <widget_id>XXXXXX</widget_id>
</root>

Here is output payload (what it should be after mapping)

<?xml version="1.0" encoding="UTF-8"?>
<ServiceRequestCollection>
    <ServiceRequest>
        <DataOriginTypeCode>7</DataOriginTypeCode>
        <Name>Testing the Integration API...</Name>
        <ProcessingTypeCode>ZSRV</ProcessingTypeCode>
        <ServiceRequestTextCollection>
            <ServiceRequestTextCollection>
                <TypeCode>10004</TypeCode>
                <LanguageCode>EN</LanguageCode>
                <Text>Oct 3, 2014 2:14:31 PMOct 3, 2014 2:14:31 PMOct 3, 2014 2:14:31 PM visitortestvisitor Hola, Good dayEsto es una prueba.</Text>
            </ServiceRequestTextCollection>
        </ServiceRequestTextCollection>
    </ServiceRequest>
</ServiceRequestCollection>


Best regards,

Mariusz

mariusz_jurcz
Participant
0 Kudos

Do I need to raise incident with this case?

Regards,

Mariusz