How to query MongoDB with 4GL to check the current role?

1 minute read time.

Imagine in your customization you have a requirement, to check what is the current role of the user who is logged in? Maybe only someone with admin role should click on your button.

Before we get to the example, you should search online help for “API to access to user profile”



Now here is the example, I am going to add a new button in my Sales order that checks for Admin profile.

  1. Open Development, Script Dictionary , Windows.
  2. Select OSOH  window.
  3. Click on Buttons/Menus tab and add a new button like below. 



  4. Click Save  and Validate
  5. Open Setup, Sales, Entry Transactions, Orders. 
  6. Validate your entry transaction.
  7. Open Development, Script Dictionary, Script Editor and add below code.


    $ACTION
    Case ACTION
    When "EXEBOUT" : Gosub BOUTON
    Endcase
    Return

    ##############

    $BOUTON

    If BOUT="7" ###### this is the code of our button in Window dictionary
    ### A function that returns the current role code

    Local Clbfile RESHEAD
    Local Clbfile RESBODY

    Local Integer STATUSCODE

    ### Module, function and element to be returned
    Local Char MODULE(100) : MODULE = 'syracuse-4gl-api/lib/collaboration'
    Local Char FONCTION(30) : FONCTION = 'getUserProfile'
    Local Char RETURNS(50) : RETURNS = "selectedRole.code"

    ### The function is asynchronous
    Local Char MODE(4) : MODE = 'wait'

    ### The callback argument is located in the first place for this function
    Local Integer CALLB : CALLB=0

    ### No Base 64 in or out coding, the arguments can remain empty
    Local Char B64_IN(20), B64_OUT(20)

    ### No additional arguments
    Local Clbfile ARGUMENTS : ARGUMENTS = ""

    # Let's call the function
    STATUSCODE = func ASYRWEBSER.EXEC_JS(MODULE, FONCTION, MODE, ARGUMENTS, B64_IN, CALLB, RETURNS, B64_OUT, RESHEAD, RESBODY)

    # If the call fails, return an empty code
    If STATUSCODE<>200 : infbox("Something went wrong") : Endif
    If STATUSCODE=200 and RESBODY="ADMIN" : infbox("Current Role is " -RESBODY- " and it's Ok for you, to click on this link") : Endif
    If STATUSCODE=200 and RESBODY<>"ADMIN" : infbox("Current Role is " -RESBODY- " Sorry only admin can click on this link") : Endif


    Endif
    Return



  8.  Save and Compile. 
  9. Open Sales, Orders, Orders. 
  10. Click on "What is my Role" button.



  11. You will see below. 

Conclusion :You can use this API to check for current user Role.