CA Service Management

 View Only
Expand all | Collapse all

CA Service Catalog - script - how to get current user

  • 1.  CA Service Catalog - script - how to get current user

    Posted Mar 06, 2025 07:39 AM
    Edited by Peter Schmidt Mar 06, 2025 08:14 AM

    Hi all,

    in form - javascript I would like to preset an user-select-field by the current logged in user.

    How can I get current logged in user?

    Something like ca_fd.GetCurrentUser() would be great.

    Or is there any ca_fd-variable containing current user?

    Something like

    var    ActUser = $(_.user.id).selector;        _.user is undefined

    runs into an error.

    Thanks,

    Peter 



  • 2.  RE: CA Service Catalog - script - how to get current user

    Posted Mar 07, 2025 01:25 AM

    Hello Peter,

    You can try $(_.user.firstName) and $(_.user.lastName) for the first name and last name fields.

    Patrick




  • 3.  RE: CA Service Catalog - script - how to get current user

    Posted Mar 07, 2025 02:04 AM
    Hi Patrick,
    _.user in my case is undefined.
    Regards,
    Peter

    Gesendet von Outlook für Android<https: aka.ms aab9ysg>




  • 4.  RE: CA Service Catalog - script - how to get current user

    Posted Mar 07, 2025 03:13 AM

    By 'undefined' do you mean they are not in the ca_contact table?




  • 5.  RE: CA Service Catalog - script - how to get current user

    Posted Mar 07, 2025 03:26 AM
    Hi,
    When tracing JavaScript with F12 setting stop in Script..
    Hovering _.user var is undefined.
    Regards,
    Peter


    Gesendet von Outlook für Android<https: aka.ms aab9ysg>





  • 6.  RE: CA Service Catalog - script - how to get current user

    Posted Mar 07, 2025 01:29 AM




  • 7.  RE: CA Service Catalog - script - how to get current user

    Posted Mar 11, 2025 04:58 AM

    Hi,

    on form-level I get the correct data for _.user.id

    --> Text-Field's value shows the logged in user-id (cnt.userid)

    But when I try to use it for the query of the user-select-field, it doesn't work:

    query seems not to run.

    Testing the report by typing the userid - also is working correct.

    Any idea?




  • 8.  RE: CA Service Catalog - script - how to get current user

    Broadcom Employee
    Posted Mar 12, 2025 12:59 AM

    Hi Peter,

    Syntax to pass the query variable is like $({'<variable_name>':'value'})

    Ex: for OOTB report data object 'catalog__content_country_list'

    Query : SELECT id,name FROM ca_country WHERE (ca_country.name like N'%%%STRING%%%')

    Select Box Parameters will be

    Report/Plug-in Id: catalog__content_country_list

    Report/Plug-in Variables: $({'STRING':_.user.location.country})

    Regards,

    Satya Kiran




  • 9.  RE: CA Service Catalog - script - how to get current user

    Posted Mar 12, 2025 02:13 AM
    Edited by Peter Schmidt Mar 12, 2025 02:20 AM

    Hi Satya Kiran,

    thanks for your guidance.

    That's the way I tried it now and it worked but not the way as I'm looking for.

    When filling Report/Plug-In Variables as described, preselect is done and no other dataset can be selected, means - query is done with value given and no other contacts (in my case) are selectable.

    What I'm looking for is a way to DEFAULT selection in the selection-field and having the chance to select another contact as the defaulted in my case. All contacts should be selectable but the logged in user should be defaulted in the select field.

    What I tried now is to write a script-function (in custom_form_examples.js) which is called as onLoad of the field.

    There I have called

       ca_fdFetchSelectData(FormID,FielID); 

    and then, in a setTimeout, I call with fix parameters

       ca_fdSelectOption(FormID, FielID, 'myLastname,myFirstName(myUserid)', 'myUserid');

    This is working, but slow.

    When I'm trying to use _.userid.<something like last_name> for the parameters,

    I found $(_.user.id) (tested with F12) undefined in script.

    So, what can I do to default selection with the logged in user?

    Thanks and regards,

    Peter

       




  • 10.  RE: CA Service Catalog - script - how to get current user

    Broadcom Employee
    Posted Mar 12, 2025 02:54 AM

    Hi Peter,

    To Achieve what you are looking for, 

    1) Remove any "where" clause from the report object to load all the data into select field

    Ex: SELECT userid,last_name FROM ca_contact

    2) Remove "Report/Plug-in Variables" attribute of select field if any

    2) set "Eager" attribute of select field to "true"

    3) set "onLoad" attribute of select field to "ca_fd.js.loadDefaultUser();"

    4) add form script as below ('sel_1' to be replaced with select field id)

    {
      loadDefaultUser: function(){
        ca_fdSelectOption(ca_fd.formId, 'sel_1', _.user.lastName, _.user.id);
      }
    }

    5) Save form and test in request page

    After the form is loaded, default selection should be shown in a second or two.

    Regards,

    Satya Kiran




  • 11.  RE: CA Service Catalog - script - how to get current user

    Posted Mar 12, 2025 05:44 AM

    Hi Satya Kiran,

    your last suggestion is working now.

    Many, many thanks.

    My main error was, to use wrong _.user-Attribute-Names.

    I expected that they are the same as in cnt-object.

    Using them like you suggested in the form-script it is working now.

    Allow me one last question 😉

    When I want to use _.user.??? in external script because I don't want to replicate code in different forms of different offerings,

    What can I try incustom_form_example.js where _.user seems not to be defined?

    Can I give _.user in the onload script-call as parameter and use it there with the known parameters?

    Do you have an example for this case?

    Many thanks,

    Peter




  • 12.  RE: CA Service Catalog - script - how to get current user

    Broadcom Employee
    Posted Mar 12, 2025 06:53 AM

    Hi Peter,

    _.user. should work in custom_form_lib.js as well, and yes you can pass as arguments too as below

    onLoad:  loadDefaultUser(ca_fd.formId,'sel_1',_.user.commonName,_.user.id);

    Note: The function call does not contain ca_fd.js prefix if the function is defined in the custom js

    custom_form_lib.js:

    function loadDefaultUser(formId,fieldId, cname, id){
        ca_fdSelectOption(formId, fieldId,cname+'('+id+')', id);
    }

    Regards,

    Satya Kiran




  • 13.  RE: CA Service Catalog - script - how to get current user

    Posted Mar 12, 2025 08:35 AM
    Hi Satya Kiran,
    many thanks for your productive help.

    It’s now working as I wanted it to work.
    Calling it via ServicePoint it is a little bit slow but has to be discussed on another place.

    Many thanks again and have a nice day,
    Peter