This topic contains 3 replies, has 0 voices, and was last updated by jharden 7 years, 7 months ago.

  • Author
    Posts
  • #21766

    jharden

    Hello All,

    I'm working on a routine to determine the total amount of deposits against a SalesOrder. Here's what I have so far. I'd love a second set of eyes on this to see if there is a better way to handle this or if there are any edge cases I'm missing.

    Thanks in advance!

    Code:
    function getCustomerDepositTotalForSalesOrder(salesOrderId) {
    var filters = [
    new nlobjSearchFilter('salesorder', null, 'anyof', salesOrderId)
    ];

    var columns = [
    new nlobjSearchColumn('total', null, 'sum')
    ];

    var results = nlapiSearchRecord(
    'customerdeposit',
    null,
    filters,
    columns
    );

    if(!results) {
    log("no deposits found for record: " + salesOrderId)
    return 0;
    }

    return results[0].getValue('total', null, 'sum')
    }
    This is a cached copy. Click here to see the original post.

  • #21767

    michoel

    Looks good to me!

    I've had to do something similar before, but I needed the total of unapplied deposits. Here's what I came up with:

    Code:
    function checkUnappliedDepositsOnSalesOrder(salesorder) {
    var unappliedDeposits = 0;

    var filters = [
    new nlobjSearchFilter('type', null, 'anyof', 'CustDep'),
    new nlobjSearchFilter('status', null, 'anyof', ['CustDep:A', 'CustDep:B']),
    new nlobjSearchFilter('internalid', 'appliedtotransaction', 'equalto', salesorder).setSummaryType('max')
    ];

    var columns = [
    new nlobjSearchColumn('internalid', null, 'group'),
    new nlobjSearchColumn('formulanumeric', null, 'sum').setFormula("MAX({amount}) – ABS(SUM({applyingtransaction.amount}))")
    ];

    nlapiCreateSearch('transaction', filters, columns)
    .runSearch()
    .forEachResult(function(result) {
    unappliedDeposits = unappliedDeposits + parseFloat(result.getValue('formulanumeric', null, 'sum'));
    return true;
    }
    );

    return unappliedDeposits;
    }

  • #21768

    markjasonflores

    jharden, it looks good! though you might want to set filter Mainline = T

    Also some for performance optimization, I suggest creating a saved search and just add filter thru script instead of dynamically creating the whole thing itself

    1. Create a transaction saved search with filters

    -Type is customer deposit

    -Mainline = T

    2. In your script, load the saved search you created in #1 and add filter for the salesorder

    new nlobjSearchFilter('salesorder', null, 'anyof', salesOrderId)

  • #21769

    jharden

    markjasonflores Thanks for the tip! I did not realize that saved searches increase performance.

    michoel Thanks for the snippet! From what I can tell, if a CustomerDeposit is connected to a SalesOrder when it is created it cannot be applied to an invoice/other sales order. I'm curious how the deposits in your case could have been applied to another SalesOrder. If they were applied to another SalesOrder, the SalesOrder was billed, and there was still an unapplied amount on the deposit although that amount could be applied to a new SalesOrder it wouldn't be "connected" to the new SalesOrder, right? So it wouldn't show up in that search?

    Thanks for the help! I want to make sure all of the edge cases are covered here.

You must be logged in to reply to this topic.