cancel
Showing results for 
Search instead for 
Did you mean: 

issue with java script action in Build apps

Jagadeesh21
Explorer
0 Kudos
243

Hi all, 
i'm sending image to SAP Documentation information extraction service and Getting the extraction Payload as in the Code below. also specified the code i'm using to filter the payload to get the desired output.
Desired Output : 

 

 

{ 
  deliveryNote: '2006452328',
  lineItems: [
    { 
       Material: '121G0612',
       OrderQty: '2',
       Unit: 'PC',
       CustomerOrderNo: null
     }
  ]
}

 

function extractLineItemValues(input) {
    const output = {
        deliveryNote: null,
        lineItems: [],
    };
 
    // Extract Delivery Note details
    if (input.extraction && input.extraction.headerFields) {
        const deliveryNoteField = input.extraction.headerFields.find(
            (field) => field.name === "Delivery.Note.no"
        );
 
        if (deliveryNoteField) {
            output.deliveryNote = deliveryNoteField.value || null;
        }
    }
 
    // Extract Line Item details
    if (input.extraction && input.extraction.lineItems) {
        input.extraction.lineItems.forEach((lineItem, index) => {
            const lineItemDetails = {
                // lineItemIndex: index + 1,
                Material: null,
                OrderQty: null,
                Unit: null,
                CustomerOrderNo: null,
            };
 
            lineItem.forEach((field) => {
                if (field.name === "Material.Number") {
                    lineItemDetails.Material = field.value;
                } else if (field.name === "Order.Qty") {
                    lineItemDetails.OrderQty = field.value;
                } else if (field.name === "Unit") {
                    lineItemDetails.Unit = field.value;
                } else if (field.name === "Customer.Order.No") {
                    lineItemDetails.CustomerOrderNo = field.value;
                }
            });
 
            output.lineItems.push(lineItemDetails);
        });
    }
 
    return output;
}
 const input = 
   {
    "status": "DONE",
    "id": "bc4a788f-f484-41c6-9080-b542af6eceac",
    "fileName": "20240229_002955623_iOS.jpg",
    "documentType": "custom",
    "created": "2025-01-22T11:35:20.521251+00:00",
    "finished": "2025-01-22T11:35:27.302215+00:00",
    "clientId": "default",
    "languageCodes": [
        "en"
    ],
    "pageCount": 1,
    "schemaId": "8a708f91-c059-4668-b32e-6a983617a479",
    "schemaVersion": "1",
    "schemaName": "Danfoss",
    "width": 2213,
    "height": 2951,
    "country": null,
    "bocrVersion": "2.38.0",
    "doxVersion": "1.105.2",
    "fileType": "jpeg",
    "dataForRetrainingStatus": "notUsedForTraining",
    "extraction": {
        "headerFields": [
            {
                "name": "Delivery.Note.no",
                "category": "custom",
                "value": "2006452328",
                "rawValue": "2006452328",
                "type": "string",
                "page": 1,
                "confidence": 0.7454677150958504,
                "coordinates": {
                    "x": 0.21554450971531858,
                    "y": 0.34700101660454086,
                    "w": 0.09489380930863084,
                    "h": 0.0155879362927821
                },
                "model": "ai",
                "label": "Delivery.Note.no",
                "description": "\t\nDelivery Note Number"
            },
            {
                "name": "Cust.order.no",
                "category": "custom",
                "value": "4508421585",
                "rawValue": "4508421585",
                "type": "string",
                "page": 1,
                "confidence": 0.7423638309141645,
                "coordinates": {
                    "x": 0.49796656122910077,
                    "y": 0.41782446628261605,
                    "w": 0.0655219159511975,
                    "h": 0.010504913588614051
                },
                "model": "ai",
                "label": "Cust.order.no",
                "description": "Customer Order Number"
            }
        ],
        "lineItems": [
            [
                {
                    "name": "Material.Number",
                    "category": "custom",
                    "value": "121G0612",
                    "rawValue": "121G0612",
                    "type": "string",
                    "page": 1,
                    "confidence": 0.7516425639717169,
                    "coordinates": {
                        "x": 0.22413014008133755,
                        "y": 0.7095899695018638,
                        "w": 0.07681879801174876,
                        "h": 0.0155879362927821
                    },
                    "model": "ai",
                    "label": "Material.Number",
                    "description": "Material Number"
                },
                {
                    "name": "Order.Qty",
                    "category": "custom",
                    "value": "2",
                    "rawValue": "2",
                    "type": "string",
                    "page": 1,
                    "confidence": 0.7661332977672575,
                    "coordinates": {
                        "x": 0.1482150926344329,
                        "y": 0.7109454422229753,
                        "w": 0.007681879801174879,
                        "h": 0.014910199932226309
                    },
                    "model": "ai",
                    "label": "Order.Qty",
                    "description": "Order Quantity"
                },
                {
                    "name": "Unit",
                    "category": "custom",
                    "value": "PC",
                    "rawValue": "PC",
                    "type": "string",
                    "page": 1,
                    "confidence": 0.7803107131481841,
                    "coordinates": {
                        "x": 0.1789426118391324,
                        "y": 0.7106065740426973,
                        "w": 0.023497514685946685,
                        "h": 0.01491019993222642
                    },
                    "model": "ai",
                    "label": "Unit",
                    "description": "Unit of Measure"
                }
            ]
        ]
    }
}
 
// Example usa
const formattedOutput = extractLineItemValues(input);
// irpa_core.cor.log("")
console.log(formattedOutput);
 

  The issue is , its working in the VSCODE and GB Compiler , but its not working in the JS workflow action Component in SAP Build apps
@Dan_Wroblewski  @DonWilliams @Sandra_Rossi @Ulrich_Schmidt 
Many thanks in Advance.


     






View Entire Topic
Dan_Wroblewski
Developer Advocate
Developer Advocate
0 Kudos

I created this app to test your scenario, but with a simplified API.

Dan_Wroblewski_0-1737890555619.png

In the first I fetch the Customers from Northwind, and return the first one (an object).

Dan_Wroblewski_1-1737890648768.png

In the next, I take as the input the output of the previous JavaScript -- JSON object. ANd just to demonstrate I have an object and can work on it like an object, I display in the console the keys and values. I was also able to select specific fields in the object.

Dan_Wroblewski_2-1737890768685.png

I can see the output in the console.

Dan_Wroblewski_3-1737890821986.png

All this to demonstrate that the passing of JSON works between JS flow functions. Previously, we showed that your code also works in the JavaScript flow function.

Hope that helps.

 

 

 

Jagadeesh21
Explorer
hi Dan, i found the issue where i'm going wrong, in the code i'm not passing it as inputs.input1 and also i didn't define the previous jsnode output as object .Thank you very much for the solution.
Dan_Wroblewski
Developer Advocate
Developer Advocate
0 Kudos
My pleasure 🙂