Question

Mapping Value to Code

  • 9 May 2023
  • 5 replies
  • 106 views

Userlevel 2
Badge

I am trying to create a workflow to add a user to an AD group which I mostly have working. The problem I am having is deriving the group name from data I have in Fresh Service. All of my AD groups names have a shorthand code for a particular location. I would like to build an expression to map the long names we have in Fresh to my shorthand codes I have in AD.

 

Right now I am trying to build an expression using if statements, which works with the test button, but  I get an error. I’m sure there has to be a better more maintainable way to do this within Fresh.

Invalid Syntax - One of its operands is invalid.

 

Here is a snippet of the code I have written.

if('{{ticket.ri_88_cf_location_to_access}}'=='Location 1', 'LOC1','') +

if('{{ticket.ri_88_cf_location_to_access}}'=='Location 2’, 'LOC2','') +

if('{{ticket.ri_88_cf_location_to_access}}'=='Location 3', 'LOC3','')


5 replies

Userlevel 2
Badge

I think I found that better way. I am now using a custom object with a reader and a replace function to build the ad group names.

Userlevel 7
Badge +16

Hello @rfrutiger glad you found a solution. I was going to suggest using a custom object. That is how we do it and it has worked great when working with AD groups. Plus it is much easier to make changes.

Badge

It looks like you're trying to use a series of if statements to map the values of a field in Fresh Service to shorthand codes that correspond to AD group names. While this approach can work, it can be cumbersome and hard to maintain if you have many different location values to map.

A more maintainable solution would be to use a lookup table or dictionary that maps the Fresh Service location values to their corresponding AD group codes. Here's an example of how you could implement this approach:
 

# Define a dictionary that maps Fresh Service location values to AD group codes
location_to_group = {
    'Location 1': 'LOC1',
    'Location 2': 'LOC2',
    'Location 3': 'LOC3'
}

# Look up the AD group code for the given Fresh Service location value
location = '{{ticket.ri_88_cf_location_to_access}}'
group = location_to_group.get(location, '')

# Use the AD group code in your workflow to add the user to the appropriate group
# ...

Userlevel 6
Badge +11

Hi @rfrutiger 

I was literally going to suggest the Custom Objects route - I’m glad you got it going.  That’s what I did and it is easier to maintain rather than editing what can become a mad stack of IF stmts.

However, bc my brain is wired that way (LMAO) I had to see if I could fix your code and I got it going so if you change your mind and want to go back to a nested IF, this will work for ya.  Just keep inserting copies of Location 2 and mod as needed.  FYI everything is case sensitive so “Location 2” <> “location 2”

 


if('{{ticket.ri_88_cf_location_to_access}}'=='Location 1', 'LOC1', if('{{ticket.ri_88_cf_location_to_access}}'=='Location 2', 'LOC2' , if('{{ticket.ri_88_cf_location_to_access}}'=='Location 3', 'LOC3','end')))

 

Have a good day!

Bryn

CYDEF.ca

Cyber security through patented threat hunting platform

Badge

It looks like you're trying to use a series of if statements to map the values of a field in Fresh Service to shorthand codes that correspond to AD group names. While this approach can work, it can be cumbersome and hard to maintain if you have many different location values to map.

A more maintainable solution would be to use a lookup table or dictionary that maps the Fresh Service location values to their corresponding AD group codes. Here's an example of how you could implement this approach:
 

# Define a dictionary that maps Fresh Service location values to AD group codes
location_to_group = {
    'Location 1': 'LOC1',
    'Location 2': 'LOC2',
    'Location 3': 'LOC3'
}

# Look up the AD group code for the given Fresh Service location value
location = '{{ticket.ri_88_cf_location_to_access}}'
group = location_to_group.get(location, '')

# Use the AD group code in your workflow to add the user to the appropriate group
# ...

 

Reply