I'm looking into sales orders automatically creati...
# suitescript
a
I'm looking into sales orders automatically creating work orders for sales orders that need over 50 gallons of material, my problem is when a order is put in for an assembly it doesn't break down the components which is where the gallons reside that would be the trigger... any ideas? Slack Conversation
n
Have you tried looking for assembly with the gallons component?
a
when I put in a sales order it doesn't break down the components of the assembly so would not trigger off the sales order correct?
n
It can trigger if you want it to.
a
the gallons component is inside 11-0025 so it doesn't show, or am I missing something?
n
You have the item, search the components. If it has gallons, trigger your functionality.
a
I'll have to dig its not firing off. I'm trying to use an assembly that a quantity of 48 requires 200+ gallons so I should be getting something
b
what does the code look like?
a
Copy code
/**
/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/log'], (record, log) => {

    const afterSubmit = (context) => {
        // Process only on CREATE or EDIT events
        if (context.type !== context.UserEventType.CREATE &&
            context.type !== context.UserEventType.EDIT) {
            return;
        }

        const salesOrder = context.newRecord;
        const lineCount = salesOrder.getLineCount({ sublistId: 'item' });
        let workOrderCreated = false;
        let createdWorkOrderId = null;

        for (let i = 0; i < lineCount; i++) {
            // Retrieve the assembly item from the "item" field
            const assemblyItemId = salesOrder.getSublistValue({
                sublistId: 'item',
                fieldId: 'item',
                line: i
            });
            // Retrieve the Sales Order line's quantity from the "quantity" field
            const salesOrderLineQuantity = salesOrder.getSublistValue({
                sublistId: 'item',
                fieldId: 'quantity',
                line: i
            });

            if (!assemblyItemId) {
                log.error('Missing Assembly Item', 'Sales Order line ' + i + ' does not have a valid assembly item.');
                continue;
            }

            try {
                // Load the Assembly Item record to access its components
                const assemblyItemRecord = record.load({
                    type: record.Type.ASSEMBLY_ITEM,
                    id: assemblyItemId,
                    isDynamic: false
                });

                // Get the number of components in the assembly item
                const componentCount = assemblyItemRecord.getLineCount({ sublistId: 'component' });
                let workOrderNeeded = false;

                // Loop through each component to check if it meets criteria
                for (let j = 0; j < componentCount; j++) {
                    // Get the unit of measure (assumed to be in the "units" field)
                    const unit = assemblyItemRecord.getSublistValue({
                        sublistId: 'component',
                        fieldId: 'units',
                        line: j
                    });

                    // Get the component quantity
                    const componentQty = assemblyItemRecord.getSublistValue({
                        sublistId: 'component',
                        fieldId: 'quantity',
                        line: j
                    });

                    // If the unit is 'Gal' and the component quantity is over 50, flag for work order creation
                    if (unit === 'Gal' && componentQty > 50) {
                        workOrderNeeded = true;
                        break;
                    }
                }

                // If criteria met, create a Work Order
                if (workOrderNeeded) {
                    try {
                        const workOrder = record.create({
                            type: record.Type.WORK_ORDER,
                            isDynamic: true
                        });
                        workOrder.setValue({
                            fieldId: 'assemblyitem',
                            value: assemblyItemId
                        });
                        workOrder.setValue({
                            fieldId: 'quantity',
                            value: salesOrderLineQuantity
                        });
                        // Optionally, set additional fields (e.g., location) here

                        createdWorkOrderId = workOrder.save();
                        log.debug('Work Order Created', 'Work Order ID: ' + createdWorkOrderId +
                            ' created for Assembly Item ID: ' + assemblyItemId);
                        workOrderCreated = true;
                    } catch (createErr) {
                        log.error('Error Creating Work Order', 'Sales Order line ' + i + ': ' + createErr.message);
                    }
                }
            } catch (loadErr) {
                log.error('Error Loading Assembly Item', 'Sales Order line ' + i + ': ' + loadErr.message);
            }
        }

        // If a Work Order was created, update the Sales Order custom field with its number
        if (workOrderCreated && createdWorkOrderId) {
            try {
                record.submitFields({
                    type: salesOrder.type,
                    id: salesOrder.id,
                    values: {
                        custbodycustbody_work_order_id: createdWorkOrderId
                    }
                });
                log.debug('Sales Order Updated', 'Custom field updated with Work Order ID: ' + createdWorkOrderId);
            } catch (updateErr) {
                log.error('Error Updating Sales Order', 'Sales Order ID ' + salesOrder.id + ': ' + updateErr.message);
            }
        }
    };

    return { afterSubmit };
});
b
honestly looks doomed
keanu thanks 1
that work order is not actually linked to the sales order
a
its just want the good old chatgpt kicked out. I'm not a coder at all
b
do you want to create an unrelated work order or did you want to actually create a linked work order
a
its not a HUGE deal if its not linked for now
I hear its kind of hard to script it to link as its already a function NS does natively?
b
there actually wont really be a huge difference between the two approaches, it just determines what you need to find in the ui
a
I just want the script to search the WO SO for the assembly, look at components, and find any unit of Gal and if its over 50 quantity, create a work order
I had a script that would create WO if the SO had a quantity of 50 or more but thats is not what I want in the end game
b
thats not what your script does
start building it in small parfs, for example, finding assembly items
and then finding the components to those items
right now the script loads every item as an assembly item, which will inevitably fail when your sales orders gets non assembly items
🫡 1
a
would I just look into my script execution logs as I kept adding steps to make sure it fired off?
b
usually people will add logs to see what their script is doing
the alternative is the script debugger, but that tends to be pretty slow
a
thanks! I'll check it out