How to handle the system exception?

Hi All,

I am getting some system exception but I need to handle the exception like:
Each day bots needs to create a batch number only when Transaction number=1,
For example for the first transaction a new batch should be created like “1234”,
After that bot needs to create the invoice numbers(the invoices numbers should be created based on the set of input i.e if there are 10 transaction items 10 invoices numbers should be created).
Till above my code is working fine but whenever the system exception occurs bot is creating a new batch numbers again(when transaction number=1) for example: 5678, but I dont want to create the new batch instead I need to use the first created batch 1234 for entire day run.
Someone please help me to do this.

@ppr
@Yoichi
@Gokul001
@RAKESH_KUMAR_BEHERA
@Rahul_Unnikrishnan
@mukeshkala

@Vrishchik

Store the batch number and the todays date in an asset…and at the start of the processcheck if the date is todays date in asset if yes then use the batch number from asset else create a new batch number

Cheers

Hi @Vrishchik

In the Finally section you can Reset Values
For example

Use If Condition

If System is Nothing
Then
// Do Nothing

Else
// Reset the values here

Hope this helps

Hi,

Use the simple approach of Updating a value whether in Excel or Asset for every invoice created and when an exception comes the bot should check the invoices and already created invoices should be skipped. Got it

Thanks

Hi @Vrishchik , All the above approach will work. Solution here will be to store batch number corresponding to todays date and checking that in the start of process. If the value is not present, you can create a new one.

Already suggested by other members in chat, you can use either asset or excel to store the value.

Happy Automation :slight_smile:

To achieve your goal of creating a batch number only once per day and using the same batch number for the entire day, you can use some form of persistent storage to store and retrieve the batch number across multiple executions. Here’s a simple example using PHP with a file to store the batch number:

function getOrCreateBatchNumber() {
    // Specify the file path to store the batch number
    $batchNumberFile = 'batch_number.txt';

    // Check if the file exists
    if (file_exists($batchNumberFile)) {
        // If the file exists, read the batch number from it
        $batchNumber = file_get_contents($batchNumberFile);
    } else {
        // If the file doesn't exist, create a new batch number
        $batchNumber = generateNewBatchNumber();

        // Save the batch number to the file
        file_put_contents($batchNumberFile, $batchNumber);
    }

    return $batchNumber;
}

function generateNewBatchNumber() {
    // Your logic to generate a new batch number (e.g., using timestamps)
    return date('YmdHis');
}

// Usage example
try {
    // Assume there's a system exception here
    // ...

    // Get or create the batch number
    $batchNumber = getOrCreateBatchNumber();

    // Continue with processing and use $batchNumber as needed
    // ...

    // Update the batch number file if necessary
    // file_put_contents('batch_number.txt', $batchNumber);
} catch (Exception $e) {
    // Handle the exception
    echo 'Exception: ' . $e->getMessage();
}

Regards
Jack Smith

import os

def get_batch_number():
    # Check if batch number is already stored
    if os.path.exists("batch_number.txt"):
        with open("batch_number.txt", "r") as file:
            batch_number = file.read()
    else:
        # If not, create a new batch number
        batch_number = generate_new_batch_number()
        # Store the batch number in a file for future reference
        with open("batch_number.txt", "w") as file:
            file.write(batch_number)
    
    return batch_number

def generate_new_batch_number():
    # Your logic to generate a new batch number (e.g., using timestamps or a counter)
    return "1234"  # Placeholder, replace with your actual logic

def process_transaction(transaction_data):
    try:
        # Your code to process the transaction
        # If transaction number is 1, use the batch number
        if transaction_data['transaction_number'] == 1:
            batch_number = get_batch_number()
            # Your logic to create invoices using the batch number
            create_invoices(transaction_data, batch_number)
        else:
            # Process other transactions using the existing batch number
            batch_number = get_batch_number()
            process_other_transactions(transaction_data, batch_number)

    except Exception as e:
        # Handle the system exception
        print(f"Exception: {e}")

# Example usage
transaction_data = {'transaction_number': 1, 'other_data': '...'}
process_transaction(transaction_data)

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.