E9.2 Connector Response

Stank1964

Well Known Member
So, I have a spiffy ORCH and it connects outbound to SalesForce. So, I put the SalesForce Connector into its' own ORCH that way I can get the connecor response regardless of that it is - raw output is on. Now, I need to parse the response to figure out what happened. I expect to receive at least 3 different response variants: success, failed for authentication (or possibly communications) reasons, failed based on the request. As I expect the response to be quite different based on these variants, I think I need groovy to figure this out. So, I open a custom groovy request and tighten my seat belt.....

I am trying to figure out which response I have based on IF a part of the json exists or not. So, I have an IF statement like so.

if (inResponse.continuedOnError[0] != null) {
....
}

That all works well, but for only one of the 3 variants. "inResponse.continuedOnError[0] != null" bombs out if that segment is not present at all in the response.

Might anyone have an idea what I can do about this?

Thanks!
 
The answer was quite simple..... Referring to a non-existing element in the object created by jsonSlurper will crash the groovy code. Even trying to check to see if something non-existent is null, is all it takes to crash groovy - "inResponse.continuedOnError[0]". You get around this, with a try/catch code block. Using this approach, we can use a custom groovy request to parse the response from the API via a connector and then act on the response further downstream in the ORCH.

try{
String stringVar = inResponse.continuedOnError[0];
} catch(Exception e){
stringVar = 'none';
}
 
Okay, in the interest of knowledge sharing.... When it comes to outbound REST calls from JDE, I suggest that the connector be put into its' own ORCH and that be the only component in that orchestration. Set it to return raw data. A top level ORCH is where all of the real work is done. In the top level orchestration which calls the connector orchestration, set the connector ORCH to return raw data as well. The connector has to return the response in 100% of all cases. This is how I have come to learn to do it, thanks to Nami on the Oracle help line.

When you do this, the top level ORCH will get the connector response regardless of what it is. In my opinion (based on my very limited experience with outbound REST calls), a connector will yield 3 responses: success, failure returned by a server (communications issues, authentication, token, etc), or failure based on an invalid request. In the top level ORCH, immediately following the connector ORCH, add a groovy custom request to parse the response. Here is a sample of how I do this.

Note, try/catch code blocks are used to determine which of the 3 possible responses was actually received. try/catch in groovy allows the statement to execute if the json element is not present without crashing groovy with a null reference error. An example - I am speaking about this statement specifically - "testString = inResponse.continuedOnError[0];". Each try/catch block contains a statement similar to this one and this is how we can use groovy to figure out which of the 3 possible responses were returned by the connector.

import com.oracle.e1.common.OrchestrationAttributes;
import java.text.SimpleDateFormat;
import groovy.json.*;

HashMap<String, Object> main(OrchestrationAttributes orchAttr, HashMap inputMap)
{
HashMap<String, Object> returnMap = new HashMap<String, Object>();

String inResponseString = (String)inputMap.get("inResponseString");
def inResponse = new JsonSlurper().parseText(inResponseString);

String orchestrationName = orchAttr.getOrchestrationName();

String testString;
String responseType;
String responseStatus;
String responseStatusCode;
String responseErrorMessage;

// test for failed token or authentication (the server rejected the request)
try{
testString = inResponse.continuedOnError[0];
responseStatus = 'Failed - Token';
responseStatusCode = inResponse.continuedOnError[0].message."ServiceRequest: 55CN_SalesForceCase_Connector".statusCode;
responseErrorMessage = inResponse.continuedOnError[0].message."ServiceRequest: 55CN_SalesForceCase_Connector".error;
} catch(Exception e){}

// test for failed data
try{
testString = inResponse.ConnectorRequest1.compositeResponse[0];
responseStatus = 'Failed - Data';
responseStatusCode = inResponse.ConnectorRequest1.compositeResponse[0].httpStatusCode;
responseErrorMessage = inResponse.ConnectorRequest1.compositeResponse[0].body[0].message;
} catch(Exception e){}

// test for success
try{
testString = inResponse.compositeResponse[0].httpsStatusCode;
responseStatus = 'Success';
responseStatusCode = inResponse.compositeResponse[0].httpStatusCode;
responseErrorMessage = inResponse.compositeResponse[0].referenceId;
} catch(Exception e){}

returnMap.put("orchName", orchestrationName)
returnMap.put("inResponseString", inResponseString)
returnMap.put("responseStatus", responseStatus);
returnMap.put("responseStatusCode", responseStatusCode);
returnMap.put("responseErrorMessage", responseErrorMessage);
return returnMap;
}
 
Last edited:
Sorry to deviate a bit
if (inResponse.continuedOnError[0] != null) {

How are you getting the inResponse. to be valid groovy code, whenever I try this it says it's undefined.
So is there something you've imported at the top to define it?
What TR are you on?

I'd love to use this as it appears to be generic and not confined to a block of JSON output from an ORCH step, like most of my groovy is

Thanks

John
 
You need to code a try/catch code block, what s what prevents the null reference error and is the key to how to make this work.
 
You need to code a try/catch code block, what s what prevents the null reference error and is the key to how to make this work.
I meant to say the try/catch code block is what prevents a null reference error....
 
Back
Top