E9.2 Custom groovy to read a form request response and check for errors

JohnDanter2

JohnDanter2

VIP Member
Hi folks

I wish to add 2 output variables scsSuccess and scsFail to my orchestration.
Now this is easy to do (even for me) in the manipluate output section it works great if the consumer was expecting JSON back, but in this case I want to see the 2 output as columns for use in Excel.
So I need to create a step with the 2 variables declared so they show up. (So I did that in a groovy step)

So this is my code in the manipulate outputs section

Code:
import groovy.json.JsonSlurper;
import groovy.json.JsonBuilder;
import com.oracle.e1.common.OrchestrationAttributes;
String main(OrchestrationAttributes orchAttr, String input)
{
  def jsonIn = new JsonSlurper().parseText(input);
  // modify jsonIn;
  
     //Default reponse to be OK
     jsonIn.scsSuccess = "1"
     jsonIn.scsFail='0'
  
 //if error detected set success to 0 and error to 1 
   if (jsonIn.continuedOnError!=null) {
       //set the control variable scsResult to be in error state 
       jsonIn.scsSuccess = "0"
       jsonIn.scsFail='1'
   }
  
  def jsonOut = new JsonBuilder(jsonIn).toString();
  // orchAttr.writeWarn("custom log entry - warning");
  // orchAttr.writeDebug("custom log entry - debug");
  return jsonOut;
}

So if I make the groovy step and declare the 2 output variables, they show up fine, but...... how would I now add code here to do the same thing and map in a 1 or 0 if the reponse from the previous step contains errors

I know I need to map in the previous steps response (so thats raw output) then I need to check it and map the 2 variables. Seems easy but my groovy is pants

1706612882104.png

1706612811090.png

Thanks

John
 
Last edited:
OK go me!!! Almost there!!!

I've managed to map the raw output from the SREQ (RRD_SREQ_P4210_W4210E.output) into my groovy step.
I've then created groovy code in the step to setup 2 output variables (scsSuccess and scsFail)
I then check if input contains, in the example '4' and map a 1 or 0 as suits.

This works great runnign the test icon but I am only looking for '4', I eventually want to check the input response contains the text 'continuedOnError' and it's getting upset if I add a b c etc. It only seems to like numbers in testing....?


Code:
import com.oracle.e1.common.OrchestrationAttributes;
import java.text.SimpleDateFormat;
import groovy.json.JsonSlurper;
HashMap<String, Object> main(OrchestrationAttributes orchAttr, HashMap inputMap)
{
  HashMap<String, Object> returnMap = new HashMap<String, Object>();
 
  // Add logic here  //RRD_SREQ_P4210_W4210E.output
 //  String stringVal = (String)inputMap.get("inputStringVal");
    def sreqResponse = inputMap.get("sreqResponse");
    String stringVal = (String)inputMap.get("sreqResponse");
    def jsonSlurper = new JsonSlurper()
    def jsonObject = jsonSlurper.parseText(stringVal)
     //Default reponse to be OK
        scsSuccess = "1"
        scsFail="0"
 //if error detected set success to 0 and error to 1

    if (sreqResponse.contains('4')){
       scsSuccess = "0"
       scsFail="1"
   }
   
  // BigDecimal numVal = new BigDecimal((String)inputMap.get("inputNumVal"));
  // SimpleDateFormat format = new SimpleDateFormat(orchAttr.getSimpleDateFormat());
  // Date dateVal = format.parse((String)inputMap.get("inputDateVal"));

  // orchAttr.writeWarn("custom log entry - warning");
  // orchAttr.writeDebug("custom log entry - debug");

  // def array = '[{"Column1":"val1","Column2":"val2"},{"Column1":"val3","Column2":"val4"}]';
  // returnMap.put("Output Array", array);
   
  // returnMap.put("Output String", stringVal);
    returnMap.put("scsSuccess", scsSuccess);
    returnMap.put("scsFail", scsFail);
   
  return returnMap;
}


1706623274638.png

If I change the input Test Value by adding any characters in there (abc etc) it falls over.
So 12356789a falls over as such

Exception org.apache.groovy.json.internal.Exceptions$JsonInternalException: unexpected character a

So what simple step am I missing here again :) As the input will eventually contain a whole host of info eventually as I want to search for the string 'continuedOnError'

Thanks

John
 
Last edited:
OK go me!!! Almost there!!!

I've managed to map the raw output from the SREQ (RRD_SREQ_P4210_W4210E.output) into my groovy step.
I've then created groovy code in the step to setup 2 output variables (scsSuccess and scsFail)
I then check if input contains, in the example '4' and map a 1 or 0 as suits.

This works great runnign the test icon but I am only looking for '4', I eventually want to check the input response contains the text 'continuedOnError' and it's getting upset if I add a b c etc. It only seems to like numbers in testing....?


Code:
import com.oracle.e1.common.OrchestrationAttributes;
import java.text.SimpleDateFormat;
import groovy.json.JsonSlurper;
HashMap<String, Object> main(OrchestrationAttributes orchAttr, HashMap inputMap)
{
  HashMap<String, Object> returnMap = new HashMap<String, Object>();
 
  // Add logic here  //RRD_SREQ_P4210_W4210E.output
 //  String stringVal = (String)inputMap.get("inputStringVal");
    def sreqResponse = inputMap.get("sreqResponse");
    String stringVal = (String)inputMap.get("sreqResponse");
    def jsonSlurper = new JsonSlurper()
    def jsonObject = jsonSlurper.parseText(stringVal)
     //Default reponse to be OK
        scsSuccess = "1"
        scsFail="0"
 //if error detected set success to 0 and error to 1

    if (sreqResponse.contains('4')){
       scsSuccess = "0"
       scsFail="1"
   }
  
  // BigDecimal numVal = new BigDecimal((String)inputMap.get("inputNumVal"));
  // SimpleDateFormat format = new SimpleDateFormat(orchAttr.getSimpleDateFormat());
  // Date dateVal = format.parse((String)inputMap.get("inputDateVal"));

  // orchAttr.writeWarn("custom log entry - warning");
  // orchAttr.writeDebug("custom log entry - debug");

  // def array = '[{"Column1":"val1","Column2":"val2"},{"Column1":"val3","Column2":"val4"}]';
  // returnMap.put("Output Array", array);
  
  // returnMap.put("Output String", stringVal);
    returnMap.put("scsSuccess", scsSuccess);
    returnMap.put("scsFail", scsFail);
  
  return returnMap;
}


View attachment 20047

If I change the input Test Value by adding any characters in there (abc etc) it falls over.
So 12356789a falls over as such

Exception org.apache.groovy.json.internal.Exceptions$JsonInternalException: unexpected character a

So what simple step am I missing here again :) As the input will eventually contain a whole host of info eventually as I want to search for the string 'continuedOnError'

Thanks

John
If the goal is to indicate whether or not the orchestration worked, I would just leave the Form Request by itself and and let the orchestration return the error to Excel. In the event that an orchestration returns an error, the add-in will add an error column to the output and provide the error. If you would like to get a count of how many lines were successful, you can set an orchestration variable to return 1. When the orchestration is successful, you get a one back. When it fails, you get an error message that would (hopefully) help resolve the issue.

To capture the output of the form request, you will need to turn on Return Raw Output in the Transformations step for the Form Request. You can pass that output into your custom request, parse the JSON, and then get a count on the error array. However, in this scenario I would just leave the Form Request to fail and let the add-in handle it.
 
Cheers Kevin

I've tried both as yes I did see the red error appearing (for some reason its totally disappeared on me now)

I got the groovy to work buy doing this

Code:
import com.oracle.e1.common.OrchestrationAttributes;
import java.text.SimpleDateFormat;
import groovy.json.JsonSlurper;
HashMap<String, Object> main(OrchestrationAttributes orchAttr, HashMap inputMap)
{
  HashMap<String, Object> returnMap = new HashMap<String, Object>();
 
  // Add logic here  //RRD_SREQ_P4210_W4210E.output
   def sreqResponse = (String)inputMap.get("sreqResponse");
    def jsonSlurper = new JsonSlurper()

 //if error detected set success to 0 and error to 1

    if (sreqResponse)
    {scsSuccess = "1"
        scsFail="0" }
    else
    {scsSuccess = "0"
        scsFail="1"}
   
    returnMap.put("scsSuccess", scsSuccess);
    returnMap.put("scsFail", scsFail);
   
  return returnMap;
}
1706632268046.png
 
@Kevin Long, I had to do it this way as the SREQ isn't able to add outputs, only what's on the screen.
Also any output variables you add in the json manipulate outputs section is not visible in the plug in. Only steps with outputs are.

Unless I'm missing something.
I'll try again tomorrow to see why the errors have disappeared on me as yes, they did work and showed on Monday :)
 
It looks like you are not getting an error back. Do you have error handling on the form request set to continue? That would suppress the error.
 
I do indeed. Awesome tip. Thank you :)
I switched it to continue to enable the ORCH to flow and feed into my last groovy step.
they need the simple count columns I added. SO what I may do is add a third that parses out the errors coming back, as yes your tool automatically does it if I click abort.

I do love what you've done :)
 
Last edited:
HI

I've just decided to let your plug in handle all the errors, I will then just simply add a count myself in Excel using COUNTIF, "SUCCESS" and COUNTIF,"<>"

Great thing about your plug in and the way it saves info is a super user would make the template to call a specific ORCH and so once the columns are mapped back into ABC etc, we can just add this count.
Issue is subesquent run displace the error text column to column M N etc :)

1706704014191.png
 
Yeah, I have been back and forth on whether to replace the error column each run or provide the history. One of the things the add-in does is look at the output columns and only process inputs where there is no output. The idea is that the user could resolve the errors and reprocess the workbook to retry only the ones that failed. By appending error columns, the user would have the history of errors over subsequent runs. I should probably make overwriting/appending the error column an option and let the user choose.
 
Hi @JohnDanter2, could you please share how you managed to pass the JSON dataset from the data/form request output to the groovy script? Thanks.
 
Hi @JohnDanter2, could you please share how you managed to pass the JSON dataset from the data/form request output to the groovy script? Thanks.

Hi
In the posts above as I say I do the following things:-
1) Ensure I tick Return Raw Output in the Transformations of the data/form request
2) Make sure error handling is set to continue
3) In the groovy steps Transformations I map my data/form recording output (RRD_SREQ_P4210_W4210E.output) as an input to my groovy using variable sreqResponse
3) in the groovy I suck in def sreqResponse = (String)inputMap.get("sreqResponse");
 
Last edited:
Back
Top