Mule DataWeave Group by And Sum - sum

I am doing something rather simple and hope that I am just missing a simple statement, I am trying to do a group by and sum but can not seem to be able to make DataWeave complete the task for me. Any help would be much appreciated!
I have the following XML
<?xml version="1.0" encoding="UTF-8"?>
<getRecordsResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<getRecordsResult>
<company>0a6979a72ba5020092d4a5a667da15ac</company>
<u_billed_amount>504</u_billed_amount>
</getRecordsResult>
<getRecordsResult>
<company>548839a72ba5020092d4a5a667da15e0</company>
<u_billed_amount>29.17</u_billed_amount>
</getRecordsResult>
<getRecordsResult>
<company>0a6979a72ba5020092d4a5a667da15ac</company>
<u_billed_amount>33.75</u_billed_amount>
</getRecordsResult>
And the Following in a dataWeave expression -
%dw 1.0
%output application/xml
%namespace ns0 http://www.service-now.com/u_incident_task
---
results: {(
payload.getRecordsResponse.*getRecordsResult groupBy $.company map {
amtToBill: ($.u_billed_amount)
}
)}
Which yields the desired grouping, however I need to Sum the amount billed.
<?xml version='1.0' encoding='UTF-8'?>
<results>
<amtToBill>
<u_billed_amount>29.17</u_billed_amount>
</amtToBill>
<amtToBill>
<u_billed_amount>504</u_billed_amount>
<u_billed_amount>33.75</u_billed_amount>
</amtToBill>
</results>
What I need is the following
<?xml version='1.0' encoding='UTF-8'?>
<results>
<amtToBill>
<u_billed_amount>29.17</u_billed_amount>
</amtToBill>
<amtToBill>
<u_billed_amount>537.75</u_billed_amount>
</amtToBill>
</results>
I know there has to be a summation in dataWeave, I have not found it yet.

I found my own answer, just do this -
amtToBill: sum $.u_billed_amount

Related

How to get id field from http xml response in power automate

How to get id field from http xml response in power automate
<?xml version="1.0" encoding="UTF-8"?>
<ServiceResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://test.qg.apps.test.com/qps/xsd/3.0/was/report.xsd">
<responseCode>SUCCESS</responseCode>
<count>1</count>
<data>
<Report>
<id>3505012</id>
</Report>
</data>
</ServiceResponse>
how can i get outcome 3505012 in result
tried below
#{slice(outputs('createreportxml'), add(nthIndexOf(outputs('createreportxml'), '>', 9), 1), nthIndexOf(outputs('createreportxml'), '<', 10))}
as well as below
xpath(xml(outputs('#{outputs('createreportxml')}'))), 'string(/ServiceResponse/data/Report/id/text())'
sample flow
Got it working with below has small issue with one braces.
#{xpath(xml(outputs('createreportxml')),'string(/ServiceResponse/data/Report/id/text())')}

EBay API: getMostWatchedItems returning RequestError

Based on the API documentation of EBay:
https://developer.ebay.com/devzone/merchandising/docs/CallRef/getMostWatchedItems.html
I made the following call:
<?xml version="1.0" encoding="UTF-8"?>
<getMostWatchedItems xmlns="http://www.ebay.com/marketplace/services">
<maxResults>3</maxResults>
</getMostWatchedItems>
With the following headers:
X-EBAY-API-COMPATIBILITY-LEVEL 967
X-EBAY-API-CALL-NAME getMostWatchedItems
X-EBAY-API-SITEID 15
Content-Type text/xml
But it returns the following response:
<?xml version="1.0" encoding="UTF-8" ?>
<eBay>
<EBayTime>2018-11-09 03:20:27</EBayTime>
<Errors>
<Error>
<Code>2</Code>
<ErrorClass>RequestError</ErrorClass>
<SeverityCode>1</SeverityCode>
<Severity>SeriousError</Severity>
<Line>0</Line>
<Column>0</Column>
<ShortMessage>
<![CDATA[ Unsupported verb. ]]>
</ShortMessage>
</Error>
</Errors>
</eBay>
Does anyone encounter the same issue? Thanks for the help.
<?xml version="1.0" encoding="UTF-8" ?>
<getMostWatchedItemsResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2018-11-09 18:23:12</Timestamp>
<Ack>Failure</Ack>
<Errors>
<ShortMessage>Unsupported API call.</ShortMessage>
<LongMessage>The API call "getMostWatchedItems" is invalid or not supported in this release.</LongMessage>
<ErrorCode>2</ErrorCode>
<SeverityCode>Error</SeverityCode>
<ErrorClassification>RequestError</ErrorClassification>
</Errors>
<Version>967</Version>
<Build>18863825</Build>
</getMostWatchedItemsResponse>
It appears that getMostWatchedItems is not supported by the 967 version of the eBay API.
You're not using the correct headers. Your headers look like they're for the Shopping API, which this call (gMWI) is not part of.
For 1 thing, you don't use SiteID in gMWI. You use Global IDs.
Have a look at this Merchandising API tutorial, especially the Standard Headers section.

Grouping the message based on one field in mule data weaver

I have a requirement where I want to filter the incoming message based on the category='E'. So if the category = 'I', it should be trimmed off the message.
Sample Input
<DataSet>
<SubDataSet>
<DataNum>TEXU9022186</DataNum>
<Category>E</Category>
</SubDataSet>
<SubDataSet>
<DataNum>TEXU9022186T</DataNum>
<Category>I</Category>
</SubDataSet>
<SubDataSet>
<DataNum>TEXU9022186T</DataNum>
<Category>E</Category>
</SubDataSet>
</DataSet>
Sample Output
<DataSet>
<SubDataSet>
<DataNum>TEXU9022186</DataNum>
<Category>E</Category>
</SubDataSet>
<SubDataSet>
<DataNum>TEXU9022186T</DataNum>
<Category>E</Category>
</SubDataSet>
</DataSet>
Could you please how to achieve this by mule dataweaver
Use filter in dataweave script as
%dw 1.0
%output application/xml
---
{DataSet : payload.DataSet.*SubDataSet filter ($.Category == "E")}
Hope this helps..

reading xml file with namespace

we need to read a xml file in sql server but we are having problems because the xml have a namespace, I have tried several solutions but I can't resolve the problem.
the xml file looks like this
<?xml version="1.0" encoding="UTF-8"?>
<Status:orders xmlns:Status="http://www.test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.test.com Status.xsd">
<order>
<Header>
<Name>500039</Name>
<Letter>A</Letter>
</Header>
</order>
</Status:orders>
can you help how to retrieve the values for the Name and letter tags
thanks in advance.
Your friend is called WITH XMLNAMESPACES...
Try it like this
DECLARE #xml XML=
'<?xml version="1.0" encoding="UTF-8"?>
<Status:orders xmlns:Status="http://www.test.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.test.com Status.xsd">
<order>
<Header>
<Name>500039</Name>
<Letter>A</Letter>
</Header>
</order>
</Status:orders>';
WITH XMLNAMESPACES('http://www.test.com' AS Status)
SELECT #xml.value('(/Status:orders/order/Header/Name)[1]','int')
,#xml.value('(/Status:orders/order/Header/Letter)[1]','varchar(max)');
An alternative was, to use the asterisk.
SELECT #xml.value('(/*:orders/order/Header/Name)[1]','int')
,#xml.value('(/*:orders/order/Header/Letter)[1]','varchar(max)');
Another alternative was this:
SELECT #xml.value('(//Name)[1]','int')
,#xml.value('(//Letter)[1]','varchar(max)');
But in general it is good advice, to be as specific as possible...

eBay API: GetOrders call returning no orders

I have placed test orders in the sandbox eBay account.
I am not able to get the orders listing from the GetOrders API call.
It retruns the success message but no orders are fetched.
Below are the API call details :
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns="urn:ebay:apis:eBLBaseComponents" ><soap:Header><RequesterCredentials><eBayAuthToken>...</eBayAuthToken><Credentials><AppId>Clarion89-2b36-4da6-b073-00dafbcff12</AppId><DevId>f79169c1-f95b-4d23-9fe2-547504ffb827</DevId><AuthCert>...</AuthCert></Credentials></RequesterCredentials></soap:Header><soap:Body><GetOrdersRequest><DetailLevel>ReturnAll</DetailLevel><Version>527</Version><CreateTimeFrom>2012-04-02T09:52:27.000Z</CreateTimeFrom><CreateTimeTo>2012-05-03T09:52:27.000Z</CreateTimeTo><OrderRole>Seller</OrderRole><OrderStatus>Active</OrderStatus><ListingType>FixedPriceItem</ListingType></GetOrdersRequest></soap:Body></soap:Envelope>
Response returned as below
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<GetOrdersResponse xmlns="urn:ebay:apis:eBLBaseComponents">
<Timestamp>2012-05-03T09:54:03.650Z</Timestamp>
<Ack>Success</Ack>
<Version>771</Version>
<Build>E771_CORE_BUNDLED_14795207_R1</Build>
<PaginationResult>
<TotalNumberOfPages>0</TotalNumberOfPages>
<TotalNumberOfEntries>0</TotalNumberOfEntries>
</PaginationResult>
<HasMoreOrders>false</HasMoreOrders>
<OrderArray/>
<OrdersPerPage>100</OrdersPerPage>
<PageNumber>1</PageNumber>
<ReturnedOrderCountActual>0</ReturnedOrderCountActual>
</GetOrdersResponse>
</soapenv:Body>
</soapenv:Envelope>
Please tell me why i am not getting order details
first of all i would use an higher version (i'm actually using 771 as compatibility level)
I used to have a similar problem at the very benning when i started coded for api, then i switched CreatedTime filter to NumberOfDays wich are the days to look back from today.
What language are you using to make the call?
Check your request parameters against your orders that are stored on ebay. It may happen that there are no orders matching the parameters you are entering in your call. Try entering the most basic request parameters like
<?xml version="1.0" encoding="utf-8"?>
<GetOrdersRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<RequesterCredentials>
<eBayAuthToken>[your authentication token]</eBayAuthToken>
</RequesterCredentials>
<CreateTimeFrom>2012-05-10T00:00:00.000Z</CreateTimeFrom>
<CreateTimeTo>2012-05-15T00:00:00.000Z</CreateTimeTo>
</GetOrdersRequest>
You can enter the dates as per your requirement but make sure you use the date format accepted by ebay.