Skip to main content
Cadet | Tier 2
February 17, 2025
Solved

URGENT : Bot Error as output while running javascript

  • February 17, 2025
  • 12 replies
  • 314 views

Hello everyone,

 

I have a requirement for merging the PDFs together, I have a data table containing the two columns (Invoice and Ref Doc Number) and a list containing some PDFs having Ref Doc Number as prefix and I have to merge the invoice with the PDFs having corresponding Ref Doc Number as prefix.

CATCH : A invoice can have multiple Ref Doc Number and a Ref Doc Number can have multiple PDFs.

I have created the input string for the given code containing the data table and the PDF list 

input_json = '{ "datatable" : [ { "Bill. Doc." : "1025294", "Ref. Doc." : "1022069792"}, { "Bill. Doc." : "54427960", "Ref. Doc." : "5750000117"}],

"pdf_list" : ["1022069792yoo","1022069792_test1","1022069792_test2","1022069792_test3","1022069792_test4","1022069792_test5","5750000117huugv","5750000117testtttt","5750000117_test11","5750000117_test2"]}' 

 

and i want the output in such a way I can merge the invoice and the PDF as mentioned above  

CODE : function generateInvoiceMapping(input_json) {
    let data = JSON.parse(input_json);
    let result = {};
    
    data.datatable.forEach(entry => {
        let invoice = entry["Bill. Doc."];
        let refDoc = entry["Ref. Doc."];
        
        if (!result[invoice]) {
            result[invoice] = {};
        }
        
        let matchingPdfs = data.pdf_list.filter(pdf => pdf.startsWith(refDoc));
        
        if (matchingPdfs.length > 0) {
            result[invoice][refDoc] = matchingPdfs;
        }
    });
    
    return JSON.stringify(result);
}

Output : {"1025294":{"1022069792":["1022069792yoo","1022069792_test1","1022069792_test2","1022069792_test3","1022069792_test4","1022069792_test5"]},"54427960":{"5750000117":["5750000117huugv","5750000117testtttt","5750000117_test11","5750000117_test2"]}}

 

ALWAYS GETTING OUTPUT AS ”Bot Error”, Although code is tunning fine in Visual Studio

Best answer by Marc Mueller

Hi ​@AryanGuru ,

As I am not able to get your JavaScript running as expected below is my approach to solve it.

I know this is not what asked for but I am a 🐍Pyhon guy 😉:

 

Maybe you want to try this:

I have put this as strInputJSON variable:


{"datatable": [{"Bill. Doc.":"1025294","Ref. Doc.":"1022069792"},{"Bill. Doc.":"54427960","Ref. Doc.":"5750000117"}],"pdf_list": ["1022069792yoo", "1022069792_test1", "1022069792_test2","1022069792_test3","1022069792_test4","1022069792_test5","5750000117huugv","5750000117testtttt","5750000117_test11","5750000117_test2"]}

 

In “Python Script: Open” action I put this:

import json

def generate_invoice_mapping(input_json):

    data = json.loads(input_json)
    result = {}

    for entry in data["datatable"]:
        invoice = entry["Bill. Doc."]
        ref_doc = entry["Ref. Doc."]

        if invoice not in result:
            result[invoice] = {}

        matching_pdfs = [pdf for pdf in data["pdf_list"] if pdf.startswith(ref_doc)]

        if matching_pdfs:
            result[invoice][ref_doc] = matching_pdfs

    return json.dumps(result)

 

###################################################################

@AryanGuru What do you think?

Cheers

Marc

 

 

 

 

 

12 replies

Marc Mueller
Most Valuable Pathfinder
Most Valuable Pathfinder
February 19, 2025

Hi ​@AryanGuru ,

As I am not able to get your JavaScript running as expected below is my approach to solve it.

I know this is not what asked for but I am a 🐍Pyhon guy 😉:

 

Maybe you want to try this:

I have put this as strInputJSON variable:


{"datatable": [{"Bill. Doc.":"1025294","Ref. Doc.":"1022069792"},{"Bill. Doc.":"54427960","Ref. Doc.":"5750000117"}],"pdf_list": ["1022069792yoo", "1022069792_test1", "1022069792_test2","1022069792_test3","1022069792_test4","1022069792_test5","5750000117huugv","5750000117testtttt","5750000117_test11","5750000117_test2"]}

 

In “Python Script: Open” action I put this:

import json

def generate_invoice_mapping(input_json):

    data = json.loads(input_json)
    result = {}

    for entry in data["datatable"]:
        invoice = entry["Bill. Doc."]
        ref_doc = entry["Ref. Doc."]

        if invoice not in result:
            result[invoice] = {}

        matching_pdfs = [pdf for pdf in data["pdf_list"] if pdf.startswith(ref_doc)]

        if matching_pdfs:
            result[invoice][ref_doc] = matching_pdfs

    return json.dumps(result)

 

###################################################################

@AryanGuru What do you think?

Cheers

Marc

 

 

 

 

 

🤝Let's connect on LinkedIn: https://www.linkedin.com/in/developer-marc-mueller >>>>>>>> 📺Check out my YouTube channel - https://youtube.com/@AutomationDevelopmentEasy
AryanGuruAuthor
Cadet | Tier 2
February 19, 2025

@Marc 1985, as i can see, the code is same, and it is working it expected, even I tried python at first but I can’t install python in my work laptop without getting non-complaint, that’s why I am using JS.

Although, thanks a lot for the efforts 😊