0

I am using Quickbooks QBFC and want to retrive the value of "First Check Number" field programatically.

It can be found in Quickbooks at File>Print Forms>Checks

Please suggest how this can be done or any reference i can look at.

Thakur
  • 1,890
  • 5
  • 23
  • 33

1 Answers1

1

You can't do this directly, but you can query for the most recent checks that have been written against the account that you are interested in. The default first check number will be one greater than the highest check number that's been written.

As a rule, I don't write people's code for them, I'm making an exception today only because it's Friday and I'm in a crazy mood. The following code can be used as a starting point for a useful last check number routine. In order to have a realistic, usable routine you, not I, will have to deal with all of the eccentricities that attach to the way people use their checkbooks: non-numeric numbers, numbers out of sequence, etc.

Please note: this code uses the Zombie open source library and QBFC 11.

class CheckNumbers
{
    private const int DATE_INTERVAL = -7;

    private string _accountName;
    private DateTime _lastToDate;
    private DateTime _lastFromDate;

    public CheckNumbers(string accountName)
    {
        _accountName = accountName;
        _lastToDate = DateTime.Today;
        _lastFromDate = _lastToDate.AddDays(DATE_INTERVAL);
    }

    private void SetCriteria(IORTxnQuery qry)
    {
        qry.TxnFilter.AccountFilter.ORAccountFilter.FullNameList.Add(_accountName);

        var dateFilter = qry.TxnFilter.ORDateRangeFilter.ModifiedDateRangeFilter;

        dateFilter.FromModifiedDate.SetValue(_lastFromDate, true);
        dateFilter.ToModifiedDate.SetValue(_lastToDate, true);
    }

    private void ProcessCheckNumber(string checkNumber, ref int highestNumber)
    {
        if (!string.IsNullOrEmpty(checkNumber))
        {
            int thisCheck;
            if (!int.TryParse(checkNumber, out thisCheck))
            {
                throw new Exception(string.Format("Check number {0} cannot be read as an integer", checkNumber));
            }
            if (thisCheck > highestNumber) highestNumber = thisCheck;
        }
    }

    public int GetLastCheckNumber()
    {
        DateTime failSafe = DateTime.Parse("1/1/2010");

        using (var cn = Zombie.ConnectionMgr.GetConnection())
        {
            int highestCheck = 0;

            while (highestCheck == 0 && _lastFromDate > failSafe)
            {
                var batch = cn.NewBatch();

                var checkQuery = batch.MsgSet.AppendCheckQueryRq();
                checkQuery.IncludeRetElementList.Add("RefNumber");

                SetCriteria(checkQuery.ORTxnQuery);

                batch.SetClosures(checkQuery, b =>
                    {
                        var checks = new Zombie.QBFCIterator<ICheckRetList, ICheckRet>(b);
                        foreach (var check in checks)
                        {
                            ProcessCheckNumber(Zombie.Safe.Value(check.RefNumber), ref highestCheck);
                        }
                    });

                var billCheckQuery = batch.MsgSet.AppendBillPaymentCheckQueryRq();
                billCheckQuery.IncludeRetElementList.Add("RefNumber");

                SetCriteria(billCheckQuery.ORTxnQuery);

                batch.SetClosures(billCheckQuery, b =>
                    {
                        var checks = new Zombie.QBFCIterator<IBillPaymentCheckRetList, IBillPaymentCheckRet>(b);
                        foreach (var check in checks)
                        {
                            ProcessCheckNumber(Zombie.Safe.Value(check.RefNumber), ref highestCheck);
                        }
                    });

                if (!batch.Run()) return 0;

                _lastToDate = _lastFromDate;
                _lastFromDate = _lastToDate.AddDays(DATE_INTERVAL);
            }

            return highestCheck;
        }
    }
}
Paul Keister
  • 12,851
  • 5
  • 46
  • 75
  • Thanks @paul, which date field do we check for most recent checks, account. Also which interface should we use? – Thakur Aug 30 '12 at 05:12
  • 1
    There is no "last modified date" in the SDK, so you'll have to roll your own. Here's how I would do it: pick a conservative date (yesterday, perhaps) and see if the query returns checks. If it doesn't keep extending the range backwards until you find something. You can keep the query lightweight by using the IncludeRetElementList to limit the return to just the TxnId of the check, the TxnDate, and the RefNumber. – Paul Keister Aug 30 '12 at 17:30
  • Thanks @Paul, really appreciate it. Will try to implement it tomorrow and let you know. – Thakur Aug 30 '12 at 19:48
  • I want to get last generated check number for a specific account. This field is mentioned in QB On Screen Reference: AccountQuery IncludeRetElementList but I and unable to retrieve the LastCheckNumber using ACcountQuery. Can you please help? – Thakur Sep 28 '12 at 09:33