My customers need to be invoiced monthly for a non...
# ask-stanley-ai
s
My customers need to be invoiced monthly for a non-inventory item, but some customers can get add-ons which are additional non-inventory items. I created custom records that hold the item, quantity, and rate of what they should be invoiced for monthly. How can I write a map reduce script that would look at a search result of these custom records and create an invoice for each customer that has an existing record? Specifically, if they have multiple items, they should be added to one invoice.
c
AI generated answer (using internal Slack threads):
To create a Map/Reduce script that would look at a search result of these custom records and create an invoice for each customer that has an existing record, you can follow these steps: 1. Input Stage: In the getInputData stage, you would create a search that looks for all the custom records that need to be processed. This search should return the customer, item, quantity, and rate.
Copy code
javascript
function getInputData() {
    var searchObj = search.create({
        type: "customrecord_your_custom_record",
        filters: [
            ["custrecord_processed", "is", false]
        ],
        columns: [
            "custrecord_customer",
            "custrecord_item",
            "custrecord_quantity",
            "custrecord_rate"
        ]
    });
    return searchObj;
}
2. Map Stage: In the map stage, you would group all the lines by customer. This can be done by using the
context.write
function where the key is the customer id and the value is the item, quantity, and rate.
Copy code
javascript
function map(context) {
    var searchResult = JSON.parse(context.value);
    context.write({
        key: searchResult.values["custrecord_customer"].value,
        value: {
            item: searchResult.values["custrecord_item"].value,
            quantity: searchResult.values["custrecord_quantity"],
            rate: searchResult.values["custrecord_rate"]
        }
    });
}
3. Reduce Stage: In the reduce stage, you would create the invoice for each customer. You would loop through all the values (which are the items) and add them to the invoice.
Copy code
javascript
function reduce(context) {
    var invoiceRec = record.create({type: record.Type.INVOICE});
    invoiceRec.setValue("entity", context.key);
    context.values.forEach(function(value) {
        var itemInfo = JSON.parse(value);
        invoiceRec.selectNewLine({sublistId: "item"});
        invoiceRec.setCurrentSublistValue({sublistId: "item", fieldId: "item", value: itemInfo.item});
        invoiceRec.setCurrentSublistValue({sublistId: "item", fieldId: "quantity", value: itemInfo.quantity});
        invoiceRec.setCurrentSublistValue({sublistId: "item", fieldId: "rate", value: itemInfo.rate});
        invoiceRec.commitLine({sublistId: "item"});
    });
    invoiceRec.save();
}
4. Summarize Stage: In the summarize stage, you can log the number of invoices created and any errors that occurred.
Copy code
javascript
function summarize(summary) {
    log.debug("Number of invoices created: " + summary.mapSummary.keys.length);
    summary.mapSummary.errors.iterator().each(function(key, error) {
        log.error("Error creating invoice for customer " + key + ": " + error);
        return true;
    });
}
Remember to set the deployment of the script to be scheduled and set the appropriate parameters. Also, make sure to update the custom record to indicate that it has been processed to avoid creating duplicate invoices. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.846726596
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.844554186
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.839102328
Please provide your feedback on this answer.