When i need to compare against multiple values, i use a hybrid approach of an Array for easy readability of each value and then use the Join function to create a string for each value in the array with a delimiter added to the beginning of the string, between each value, and to the end of the string, then use the InStr function the way Kevin did to search for a given value, with the delimiter added to the beginning and ending of the value, within the final string. If InStr returns a value greater than 0, then a match is found. Double quotes can be used for the beginning and ending of the final string as well as to surround the value to search for. The important thing is to make sure you can't possible have your value running into match match between two value from the array.
This avoids the need to loop through each value in the array.
Here are three examples that all accomplish the same thing. Obviously you don't gain much when doing this for 2 values but when comparing more, it sure beats using a If condition for each one as seen in example 3.
' Example 1.
If IsObject(oSession) Then
aCompanyCodes = Array("TOM", "TST")
If IsArray(aCompanyCodes) Then
If InStr(UCase("""" & Join(aCompanyCodes, """,""") & """"), """" & UCase(oSession.CompanyCode)) & """" > 0 Then
' Do stuff here.
End If
End If
If IsArray(aCompanyCodes) Then Erase aCompanyCodes
End If
' Example 2.
If IsObject(oSession) Then
aCompanyCodes = Array("TOM", "TST")
If IsArray(aCompanyCodes) Then
If InStr(UCase("_" & Join(aCompanyCodes, "_") & "_"), "_" & UCase(oSession.CompanyCode)) & "_" > 0 Then
' Do stuff here.
End If
End If
If IsArray(aCompanyCodes) Then Erase aCompanyCodes
End If
' Example 3.
If IsObject(oSession) Then
If UCase(oSession.CompanyCode) = UCase("ABC") Or UCase(oSession.CompanyCode) = UCase("ABX") Then
' Do stuff here.
End If
End IfAlso, VBScript doesn't support the SubString function you are using. You need to use the Mid or Left function.
https://www.w3schools.com/asp/func_mid.asp
https://www.w3schools.com/asp/func_left.asp
These examples use a simple variable in place of the account segments to search in but you can piece this all together. The first argument of InStr is the string to search within, the second argument is the string to search for. I always convert both arguments to uppercase (or lowercase) when i need case-insensitive matches to guarantee i don't have casing issues.
' Mid example.
If InStr(UCase(sAccountSegments), Mid(UCase(sAccount), 1, 3)) > 0 Then
' Do stuff here.
End If
' Left example.
If InStr(UCase(sAccountSegments), Left(UCase(sAccount), 3)) > 0 Then
' Do stuff here.
End If
Kevin brought up a valid point regarding the account. You can handle it like this.
oBusObj.ReadAdditional "AccountKey"
sAccount = "" : oSession.AsObject(oBusObj.GetChildHandle("AccountKey")).GetValue "Account$", sAccount
When using SetError, make sure your script is tied to a "pre" type event, it will not work on "post" type events.
------------------------------
David Speck II
Tennessee Software Solutions
------------------------------
Original Message:
Sent: 05-21-2020 11:09
From: Kevin Moyes
Subject: Script to check GL Account in Journal Entry
(Also note that you are only getting the AccountKey, not the Account... which might be something you can obtain by opening an object with GetChildHandle).
retVal=oBusObj.GetValue("AccountKey$", sAcct)
------------------------------
Kevin Moyes
Technical Systems Analyst
Munjal White Consulting Co.
Toronto ON
------------------------------
Original Message:
Sent: 05-21-2020 11:04
From: Michelle Taylor
Subject: Script to check GL Account in Journal Entry
This is awesome! I SO need to learn this language!! Thanks again :)
------------------------------
Michelle Taylor
ERP Consulting Manager, CS3 Technology
918-388-9772
Original Message:
Sent: 05-21-2020 11:02
From: Kevin Moyes
Subject: Script to check GL Account in Journal Entry
Oh, and you might find something like this to be a bit more efficient (no loop... and if you add to the list you don't have to remember to increase the number of iterations).
sCashAcct = "_101_102_103_104_"sAcct=""if oSession.AsObject(oSession.Security).IsMember("ROLE") Then retVal=oBusObj.GetValue("AccountKey$", sAcct) If instr(sCashAcct,"_" & sAcct.Substring(0, 3) & "_") > 0 Then retVal = oScript.SetError("You are not allowed to post to cash accounts.") exit sub end ifEnd If
------------------------------
Kevin Moyes
Technical Systems Analyst
Munjal White Consulting Co.
Toronto ON
Original Message:
Sent: 05-21-2020 10:29
From: Kevin Moyes
Subject: Script to check GL Account in Journal Entry
Yes, of course... your role check was not closed. :-)
------------------------------
Kevin Moyes
Technical Systems Analyst
Munjal White Consulting Co.
Toronto ON
Original Message:
Sent: 05-21-2020 10:17
From: Michelle Taylor
Subject: Script to check GL Account in Journal Entry
Thank you Kevin! The issue was a missing End if.
------------------------------
Michelle Taylor
ERP Consulting Manager, CS3 Technology
918-388-9772
Original Message:
Sent: 05-20-2020 18:16
From: Kevin Moyes
Subject: Script to check GL Account in Journal Entry
When I SetError, my typical next line is an Exit Sub.
------------------------------
Kevin Moyes
Technical Systems Analyst
Munjal White Consulting Co.
Toronto ON