Sage 100

 View Only
  • 1.  Script to check GL Account in Journal Entry

    Posted 05-20-2020 16:33
    Starting scripting, yep true beginning of my venture, and any help is much appreciated.  

    Looking to test GL Accounts in General Journal Entry.  If the user enters one of a set of accounts, stop them from saving the entry.  This is what I have, yet it is erroring at the Exit For with "Expected 'If' .
    error message


    sCashAcct = ["101","102","103","104"]sCashAcct = ["101","102","103","104"]
    sAcct=""

    if oSession.AsObject(oSession.Security).IsMember("ROLE") Then

          retVal=oBusObj.GetValue("AccountKey$", sAcct)

          For i = 0 To 4          
                If sAcct.Substring(0, 3) = sCashAccts(i) Then
                    retVal = oScript.SetError("You are not allowed to post to cash accounts.")
                    Exit For          
                End If
          Next


    ------------------------------
    Michelle Taylor
    ERP Consulting Manager, CS3 Technology
    918-388-9772
    ------------------------------


  • 2.  RE: Script to check GL Account in Journal Entry

    Posted 05-20-2020 18:17
    When I SetError, my typical next line is an Exit Sub.

    ------------------------------
    Kevin Moyes
    Technical Systems Analyst
    Munjal White Consulting Co.
    Toronto ON
    ------------------------------



  • 3.  RE: Script to check GL Account in Journal Entry

    Posted 05-21-2020 10:18
    Thank you Kevin!  The issue was a missing End if.

    ------------------------------
    Michelle Taylor
    ERP Consulting Manager, CS3 Technology
    918-388-9772
    ------------------------------



  • 4.  RE: Script to check GL Account in Journal Entry

    Posted 05-21-2020 10:29
    Yes, of course... your role check was not closed.  :-)

    ------------------------------
    Kevin Moyes
    Technical Systems Analyst
    Munjal White Consulting Co.
    Toronto ON
    ------------------------------



  • 5.  RE: Script to check GL Account in Journal Entry

    Posted 05-21-2020 11:02
    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 if
    End If


    ------------------------------
    Kevin Moyes
    Technical Systems Analyst
    Munjal White Consulting Co.
    Toronto ON
    ------------------------------



  • 6.  RE: Script to check GL Account in Journal Entry

    Posted 05-21-2020 11:05
    This is awesome!  I SO need to learn this language!!  Thanks again :)

    ------------------------------
    Michelle Taylor
    ERP Consulting Manager, CS3 Technology
    918-388-9772
    ------------------------------



  • 7.  RE: Script to check GL Account in Journal Entry

    Posted 05-21-2020 11:09
    (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
    ------------------------------



  • 8.  RE: Script to check GL Account in Journal Entry

    Posted 05-22-2020 12:08
    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 If

    Also, 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
    ------------------------------



  • 9.  RE: Script to check GL Account in Journal Entry

    Posted 05-22-2020 12:33
    Thank you David!  Priceless!!  This really, really helps me move on to bigger, better script project.  SO appreciated :)

    ------------------------------
    Michelle Taylor
    ERP Consulting Manager, CS3 Technology
    918-388-9772
    ------------------------------