This topic contains 6 replies, has 0 voices, and was last updated by Duncan 8 years, 2 months ago.

  • Author
    Posts
  • #23527

    Duncan

    I have programming/IT background, but I have never written in SuiteScript before. I understand triggers/events and built-in objects like "this". But I am new to SuiteScript version of those concepts.

    My goal is to remind user that the customer of current invoice record (Invoice form) has a negative balance. The reason is, accounting department told me that they issue credit memos to paid invoices, and those credits stay there forever never to be used again because there is no obvious way of knowing if customer has any negative balance when they create a new invoice.

    Based on examples and the object reference I came up with this. I want to pop up a message if the customer of the current invoice record has any negative balance. Am I on the right track? The invoice is usually created from the customer record, so I don't think customer_onChange trigger will be necessary. Thanks in advance.

    PHP Code:

    /**
     * @NApiVersion 2.x
     * @NScriptType UserEventScript
     */
    define(['N/record'],
        function (record)
        {

            function showErrorMessage(msgText) {
                var myMsg = msg.create({
                    title: "Negative Balance",
                    message: msgText,
                    type: msg.Type.ERROR
                });

                myMsg.show({
                    duration: 5000
                });
            }

            function CheckBalance(context)
            {

                var invoiceRecord = context.currentRecord;

                var customerRecord = invoiceRecord.entity;

                if (customerRecord.balance < 0)
                {
                    showErrorMessage("This customer has " + customerRecord.balance + " credit. Consider before accepting any full payments");
                    return false;
                }

                return true;
            }

            return {
                pageInit: CheckBalance,

            };
        });
    This is a cached copy. Click here to see the original post.

  • #23528

    torin@aminian.com

    I'm not familiar with scripts but this seems like something that can be done using an action in a workflow. The action would be show message and the condition would be a formula {entity.balance}<0.

  • #23529

    k_dunc

    Couldn't agree more with torin@aminian.com. A Workflow will quite sufficiently handle this task without having to do any coding.

  • #23530

    chanarbon

    Similar to the provided solution coming from @torin.aminian.com, workflow should can address this concern with a simple Show Message.

    But maybe you have another set of validations that you want. But here's some tips on the changes you need for your script to work properly. Also use invoiceRecord.getValue({"fieldId":"entity"}) instead of invoiceRecord.entity. You also need to load the customerRecord and then perform a getValue() to get the balance.

  • #23531

    erictgrubaugh

    You seem to be on the right track with your code. torin@aminian.com is also correct that this could alternatively be a pretty straight-forward Workflow.

    The pageInit function doesn't return anything, so your two `return` statements are unnecessary. You are also missing the actual import of the N/ui/message module as `msg`. Other than that, I don't see anything glaring about your code. Did you have a more specific question? Or an error you're seeing?

  • #23532

    Duncan

    Thank you for the answers. I am looking into "workflow" now as I am not sure what it is, but sounds less complicated. And I definitely prefer less complicated methods

  • #23533

    Duncan

    Thank you, @torin@aminian.com

    Your solution did the trick (for the formula I just used the GUI). I showed it to our accounting, they were happy to see how simple it was. I think they might even manage it by themselves for any future WorkFlows similar to this.

You must be logged in to reply to this topic.