Shelby Owens
03/31/2025, 3:42 PMCeligo AI
03/31/2025, 3:43 PMAI 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.
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.
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.
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.
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.
******Celigo AI
03/31/2025, 3:43 PMSlack 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
Celigo AI
03/31/2025, 3:43 PM