How can I use workflows or SuiteScript to prevent ...
# ask-stanley-ai
s
How can I use workflows or SuiteScript to prevent the rate on a Purchase Order from changing when the vendor changes on the Purchase Order?
c
AI generated answer (using internal Slack threads):
****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ****** To prevent the rate on a Purchase Order from changing when the vendor changes on the Purchase Order, you can use a User Event script in SuiteScript. This script would be triggered before the record is submitted and would check if the vendor field has been changed. If it has, the script would then revert the rate back to its original value. Here is a sample script based on a similar scenario discussed in the threads:
Copy code
javascript
/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 */
define(['N/ui/serverWidget'],
    function(serverWidget) {
        function beforeSubmit(scriptContext) {
            var oldRecord = scriptContext.oldRecord;
            var newRecord = scriptContext.newRecord;
            var oldVendor = oldRecord.getValue('entity');
            var newVendor = newRecord.getValue('entity');

            if(oldVendor !== newVendor){
                var oldRate = oldRecord.getValue('rate');
                newRecord.setValue('rate', oldRate);
            }
        }
        return {
            beforeSubmit: beforeSubmit
        };
    });
This script checks if the vendor (
entity
) has changed. If it has, it sets the rate on the new record to the rate from the old record, effectively preventing the rate from changing. Please note that this is a simplified example and may need to be adjusted to fit your specific needs, such as handling multiple line items or different rates for different vendors.
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.836914122
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.82800293
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.827209473
Please provide your feedback on this answer.