I have a Purchase Order that has an item on it. I ...
# ask-stanley-ai
s
I have a Purchase Order that has an item on it. I then added the item to an existing sales order. How do I tie the PO to the item on the sales order? I don't want to use a custom field but want the link to display in the CREATE PO filed that is already on the line, like I hit the drop ship or special order links that are displaying there now. I also want to do it in suitescript.
c
AI generated answer (using internal Slack threads):
To link a Purchase Order (PO) to an item on a Sales Order (SO) without using a custom field, you can use SuiteScript to automate this process. Here's a general approach: 1. Create a User Event script that triggers on the creation or update of a Sales Order. 2. In the script, identify the item that needs to be linked to a Purchase Order. 3. Search for the corresponding Purchase Order that contains the same item. 4. Once the Purchase Order is found, you can link it to the Sales Order by setting the 'Created From' field on the Purchase Order to the internal ID of the Sales Order. Please note that this approach assumes that the Purchase Order is created after the Sales Order. If the Purchase Order is created before the Sales Order, you might need to adjust the script to run on the creation or update of a Purchase Order instead. Here's a simplified example of what the SuiteScript might look like:
Copy code
javascript
/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/search'], function(record, search) {
    function afterSubmit(context) {
        var salesOrder = context.newRecord;
        var itemId = salesOrder.getValue('item');

        var poSearch = search.create({
            type: search.Type.PURCHASE_ORDER,
            filters: [['item', 'anyof', itemId]]
        });

        var poSearchResult = poSearch.run().getRange({start: 0, end: 1});
        if (poSearchResult.length > 0) {
            var po = record.load({
                type: record.Type.PURCHASE_ORDER,
                id: poSearchResult[0].id
            });

            po.setValue('createdfrom', salesOrder.id);
            po.save();
        }
    }

    return {
        afterSubmit: afterSubmit
    };
});
This script will run every time a Sales Order is created or updated. It will search for a Purchase Order with the same item and link it to the Sales Order. ****** 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.85949707
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.855651855
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.848632812
Please provide your feedback on this answer.