Scripting

Expand all | Collapse all

I'm trying what I thought would be a simple enough

  • 1.  I'm trying what I thought would be a simple enough

    Posted 10-26-2017 17:21
    I'm trying what I thought would be a simple enough script, which will be used to prevent saving a sales order if Payment Type is a credit card and the Auth No and Auth Amount fields are empty, but I can't see my mistake, unless I'm choosing the wrong event. I have this set up as a PreWrite on the Sales Order Header table. Any advice would be greatly appreciated! sSalesOrder = """" : sPymtType = """" : nAuthAmt = 0 : sAuthNo = """" retVal = oBusObj.GetValue(""SalesOrderNo$"", sSalesOrder) retVal = oBusObj.GetValue(""PaymentType$"", sPymtType) If sPymtType = ""AMEX"" or sPymtType = ""DISC"" or sPymtType = ""MC"" or sPymtType = ""VISA"" then set oPayment = oBusObj.AsObject(oBusObj.PaymentObj) retVal = oPayment.SetKeyValue(""SalesOrderNo$"", sSalesOrder) retVal = oPayment.Find() If retVal = 1 Then retVal = oPayment.GetValue(""CreditCardAuthorizationNo$"", sAuthNo) retVal = oPayment.GetValue(""TransactionAmt"", nAuthAmt) If sAuthNo = """" or nAuthAmt = 0 Then sMsg = ""A Credit Card Authorization is Required"" retVal = oScript.SetError(sMsg) End If End If End If


  • 2.  RE: I'm trying what I thought would be a simple enough

    Posted 10-26-2017 17:31
    is it possible that they could set the PaymentType to one of the values you are looking for and not go to the credit tab and trigger the creation of the key in the payment table? If so, then the line where you are checking if the retVal of the Find method could return a zero which will prevent the SetError method from being reached. Perhaps you should instead remove the ""if"" structure checking the retVal from the Find method. Even if the Find method fails, you could still call the two GetValue methods and then check if either does not meet your conditions.


  • 3.  RE: I'm trying what I thought would be a simple enough

    Posted 10-26-2017 17:37
    I believe you need to follow a ""SetError"" statement with an ""exit sub


  • 4.  RE: I'm trying what I thought would be a simple enough

    Posted 10-26-2017 17:55
    @MichaelNottoli, I believe that would be needed if there was additional code that would be required to execute only if the condition wasn't met to cause the SetError. The SetError only works in the ""pre"" events though. The ""exit sub"" is just a quick way to get out of the current script event.


  • 5.  RE: I'm trying what I thought would be a simple enough

    Posted 10-26-2017 18:31
    Thanks for the quick and thorough response, David. I commented out the ""if retval = 1"" which then did cause my pop-up to occur...but it pops up even when I have authorized the card, and it won;t let me save what would be a valid authorization and order.


  • 6.  RE: I'm trying what I thought would be a simple enough

    Posted 10-26-2017 18:35
    Can you do a DebugPrint or display a message box with the values in sAuthNo and nAuthAmt to see what values are in the variables when they are being checked? Are you sure you have the field names typed correctly?


  • 7.  RE: I'm trying what I thought would be a simple enough

    Posted 10-26-2017 18:38
    That's what I'm going to try next. But I do see that when I am working on the Payment tab, there's no record yet in the SO_SalesOrderPayment table, which is what I think you were saying before...which means it has nothing to pull/reference. Now I'm wondering if Pre-Write is even the right event.


  • 8.  RE: I'm trying what I thought would be a simple enough

    Posted 10-26-2017 18:43
    i didn't think of this until now, i don't have any experience with the payment object but if you are getting the handle to it from the header object, then it might already be on a record in memory and your SetKeyValue and Find method is stepping off of it. Can you try commenting out the SetKeyValue and Find?


  • 9.  RE: I'm trying what I thought would be a simple enough

    Posted 10-26-2017 18:51
    Prudy I think your oPayment.Find() is failing because you're not including the PaymentSeqNo earlier which is part of the key. This object is also an odd duckling. ------------ If you think Seq will always be ""000001"" which is presumable for a new payment record, just try SetKeyValue on that (and try ""000002"" if retVal is 0 on that). Otherwise if you think it varies more than that, the object has some unique functions available to you. I haven't tested these but here goes: ------------ ' STEP 1 retVal = oPayment.SetKeyValue(""SalesOrderNo$"", sSalesOrder) 'same as above SeqNo = """" retVal = oPayment.GetNextSeqNo(SeqNo) ' At this point go one of these routes (use the one that works) 'STEP 2A retVal = oPayment.SetKeyValue(""PaymentSeqNo$"", SeqNo) retVal = oPayment.Find() 'Run your If/Then SetError test 'OR STEP 2B (this would payment object in an edit state so probably not applicable to you ) retVal = oPayment.SetRecordKey(SeqNo) ' this actually does the SetKeyValue on PaymentSeqNo and then SetKey too. 'Run your If/Then SetError test retVal = oPayment.Clear() 'to clear the edit state ---------------------------- Yet another way would be to just to grab from the screen - something like this: isUIObjSet = IsObject(oUIObj) ' If an event script set oUIObj - button scripts and UI scripts already have it If isUIObjSet = False Then oUIObj = oScript.UIObj Set oUIObj = oSession.AsObject(oUIObj) Else 'oUIObj is already passed in for us End If If sPymtType = ""AMEX"" or sPymtType = ""DISC"" or sPymtType = ""MC"" or sPymtType = ""VISA"" Then retVal = oUIObj.GetValue(""CreditCardAuthorizationNo"", sAuthNo) 'use the control name from Ctrl-F9 instead of field name retVal = oUIObj.GetValue(""TransactionAmt"", nAuthAmt) 'use the control name from Ctrl-F9 instead of field name 'Run your If/Then SetError test


  • 10.  RE: I'm trying what I thought would be a simple enough

    Posted 10-26-2017 19:32
    This works great when I assign the SeqNo: set oPayment = oBusObj.AsObject(oBusObj.PaymentObj) retVal = oPayment.SetKeyValue(""SalesOrderNo$"", sSalesOrder) sSeqNo = ""000001"" retVal = oPayment.SetKeyValue(""PaymentSeqNo$"", sSeqNo) retVal = oPayment.Find() If retVal = 1 Then retVal = oPayment.GetValue(""CreditCardAuthorizationNo$"", sAuthNo) retVal = oPayment.GetValue(""TransactionAmt"", nAuthAmt) But I don't feel comfortable with that, as I have seen multiple sequence numbers for a sales order but have yet to understand why (since I haven't put a lot of thought into it). I'm going to try Step2B next, and I think that UI code is very cool! Thank you, Alnoor! As always, you are fantastic.


  • 11.  RE: I'm trying what I thought would be a simple enough

    Posted 10-26-2017 19:42
    Better to get STEP 2A to work if possible.


  • 12.  RE: I'm trying what I thought would be a simple enough

    Posted 10-26-2017 20:24
    Perhaps a GetResultSets would be a better way to handle this. Here is a link to a good example of it https://sagecity.na.sage.com/support_communities/sage100_erp/f/sage-100-business-object-interface/71048/advanced-getresultsets A run down of how i might do this would be the following. --------------------------------------------------------------------- sSalesOrder = """" : sPymtType = """" : nAuthAmt = 0 : sAuthNo = """" retVal = oBusObj.GetValue(""SalesOrderNo$"", sSalesOrder) retVal = oBusObj.GetValue(""PaymentType$"", sPymtType) If sPymtType = ""AMEX"" Or sPymtType = ""DISC"" Or sPymtType = ""MC"" Or sPymtType = ""VISA"" Then Set oPayment = oBusObj.AsObject(oBusObj.PaymentObj) sPaymentSeqNumbers = """" sPaymentKeys = """" sFilter = ""CreditCardAuthorizationNo$ = """""""" OR TransactionAmt = 0"" sBegin = sSalesOrder sEnd = sSalesOrder & Chr(254) nInvalidPaymentFound = 0 nInvalidPaymentFound = oPayment.GetResultSets(""PaymentSeqNo$"", ""SalesOrderNo$+PaymentSeqNo$"", sPaymentSeqNumbers, sPaymentKeys, sFilter, sBegin, sEnd) ' if nInvalidPaymentFound equals 1, then this means one or more records were found where there was a match to the filter we specified, CreditCardAuthorizationNo$ = """" OR TransactionAmt = 0, the sPaymentSeqNumbers variable will contain a SEP delimited string of the PaymentSeqNo values. sPaymentKeys will contain the combined primary key for each record found that matches. If nInvalidPaymentFound = 1 Then sMsg = ""A Credit Card Authorization is Required"" retVal = oScript.SetError(sMsg) End If End If --------------------------------------------------------------------- @AlnoorCassim, it's been a while since i dealt with the Begin and End parameter with GetResultSets and i could not remember the best way to do it off the top of my head, In the above scenario where the records have a multi-part key and we only know the first part, should the Begin end with the CHR(0) character in the same way the End ends with the CHR(254) character or would Amber be better off using just the sSalesOrder variable in the Begin and End parameter?


  • 13.  RE: I'm trying what I thought would be a simple enough

    Posted 10-27-2017 10:32
    It seems to me that SetBrowseFilter would be easier than dealing with the SeqNo.