Use SAP b1 service layer

Hello everyone,

after much research on the net, I am looking for a solution to this.

I’m building an API for data exchange between SAP B1 and an e-shop. For this I use the service layer of SAP B1, according to the doc mentioned here:

https://help.sap.com/doc/6ab840ef2e8140f3af9eeb7d8fef9f06/10.0/en-US/Working_with_SAP_Business_One_Service_Layer.pdf

To illustrate my problem, here is a piece of code that works for the first query (I pass the connection and recovery of the session and others…), but the second poses a problem.

The service layer answers me: 400 Bad Request

<cfscript>
	// session.serviceLayer.sessionID = "B1SESSION=692xxxxxxxxxxxxxxxxxx17;ROUTEID=.nodeXX" this is ok for other query
	server = "https://10.10.0.10:50000"
	businessPartnersURL = "/b1s/v1/BusinessPartners"
	queryCount = "/$count?$filter=startswith(CardCode, 'C')"
	queryMaxDate = "?$apply=aggregate(UpdateDate with max as MaxDate)"
	// aggregate in page : 37 / 3.7.7.3 in SAP Doc
	// this query (URL) works very well for example in postman.
	
	cfhttp(method = "get", charset = "utf-8", url = server & businessPartnersURL & queryCount , result="resultQuery") {
            cfhttpParam(type = "header", name="Cookie" , value = session.serviceLayer.sessionID );
	};

	cfhttp(method = "get", charset = "utf-8", url = server & businessPartnersURL & queryMaxDate , result="resultQuery") {
            cfhttpParam(type = "header", name="Cookie" , value = session.serviceLayer.sessionID );
	};
	
</cfscript>

Response queryCount : result-queryCount.png

Response queryMaxDate : result-queryMaxDate.png

Response queryMaxDate in postman :

An idea ? Am I wrong somewhere?

Thank you for your feedback
Christophe

PS: Sorry for English, I speak French

1 Like

did you try url encoding the second query maybe?

#encodeForUrl("?$apply=aggregate(UpdateDate with max as MaxDate)")#

you could also try passing url params via cfhttparam?

don’t worry about your english, I’m Australian, it’s also my second language :slight_smile:

1 Like

thank you for the answer

I hadn’t thought of that. But despite encodeForUrl, it doesn’t change. Same answer!

I also tried this:

         server="https://10.10.0.10:50000"
         businessPartnersURL = "/b1s/v1/BusinessPartners"
         cfhttp(method = "get", charset = "utf-8", url = server & businessPartnersURL , result="resultQuery") {
             cfhttpparam(type = "url", name = "$apply", value = "aggregate(UpdateDate with max as MaxDate)")
             cfhttpparam(type = "header" name = "Content-Type" value = "application/json");
             cfhttpParam(type = "header", name = "Cookie", value = session.serviceLayer.sessionID);
         };

         server="https://10.10.0.10:50000"
         businessPartnersURL = "/b1s/v1/BusinessPartners"
         cfhttp(method = "get", charset = "utf-8", url = server & businessPartnersURL , result="resultQuery") {
             cfhttpparam(type = "url", name = "$apply", value = encodeForURL("aggregate(UpdateDate with max as MaxDate)") )
             cfhttpparam(type = "header" name = "Content-Type" value = "application/json");
             cfhttpParam(type = "header", name = "Cookie", value = session.serviceLayer.sessionID);
         };

Same error…

I’m trying to find a SAP service layer log to get more explanation about the error.

Thanks, I’ll let you know if I find it.

Hello again everyone,

after finding the service layer logs, i found. By default CFHTTP still URL, so the serviceLayer would receive this:

[20/Jul/2023:11:40:04 +0200] x.x.x.x 5125 "GET /b1s/v1/BusinessPartners?%24apply=aggregate%28UpdateDate%20with%20max%20as%20MaxDate%29 HTTP/1.1" 400 117 ssl=TLSv1.3 t=0s pid=9860 sid=cbaXXXXXXXXXXXXXXXXXXXXXXX n=.node1 -

When he was to receive this:

[20/Jul/2023:11:41:22 +0200] x.x.x.x 35237 "GET /b1s/v1/BusinessPartners?$apply=aggregate(UpdateDate%20with%20max%20as%20MaxDate) HTTP/1.1" 200 132 ssl=TLSv1.3 t=0s pid=986 0 sid=cce0XXXXXXXXXXXXXXXXXXXXXXX n=.node2 +

I added the encodeurl = false parameter to the http query and encoded only the content of the query and it works!

this.BusinessPartners_query_last_UpdateDate = "?$apply=#encodeForURL('aggregate(UpdateDate with max as MaxDate)')#"

And now, the
If it can help… :wink:

3 Likes