Hello, I have a form that includes a dropdown menu of what department an employee is in, and if they are not a department head then the form goes to that department's department head. It is a user task and I need help figuring out how to email the right department head based off of what the employee selected for their department.
Question
Question
How to email someone based off of a dropdown menu selection?
Answer
Oh! I misunderstood and thought you were just sending an email to someone.
Doing user tasks is probably going to be easier. We have a few options:
We can definitely continue with the options we listed before, but instead of listing the department head's email address - you'll want to list their User ID. Then on the User Task, you can assign to "Users" and use the variable from the populated User ID (what had been the email field in the examples before) as the user name(s) for the task assignment.
Or, if you have your teams in LFForms set-up to match your departments, you can completely ignore the formula and Javascript examples I gave before, and just use the Department drop-down field to ultimately select the Team you want. You can do this on the user task via the "Team based on variable" option, listing the Department Drop-down's variable in the "Team Name" option. In this case, you probably want a filter set-up on all of those department teams that specifically identifies the head of that department/team (this part can be a little time consuming to set-up, but it would be reusable). You would probably want to do this via a role that you add on all of the teams and a filter to return the users with that role (make sure you set it up the same way on all the teams, and name it the same thing on all the teams, because you are referring to it by name in the "Dynamic Filter" field on the user task). If the role is named "Department Head" for example, and you add the user(s) that are the head(s) of that department to that role in each team - then the filter (which you would add to every team) just returns all users that have that role, like this:
$result=team.findMembersByRole('Department Head');
Or, if you don't want to have a dedicated team for each department - maybe you have a single team of just department heads, and then have a role for each department. In your "Department Heads" team, you have roles for "Team A", "Team B", "Team C", etc. With the name of the role exactly matching how you listed the name of the Department on the drop-down field on the form. Then assign all the department heads to that team and give them the role for the specific department that they head. This is probably the easiest to set-up, because on your User Task, you'll select the "Team" option and select your "Department Heads" team, and then set-up a filter like this:
var departmentName = $util.getValue('select_department'); $result=team.findMembersByRole(departmentName);
Replies
Does the information about the departments and who the department head is (and what their email address is) exist in a database you manage, or do you have it set-up in LFForms Teams, or somewhere else that it can be accessed via database lookup? Or are you willing to set it up in a database?
That's going to be your easiest option, if the list of departments comes from a database table that also includes and the name and email address of that department head, then you can have all of that work from lookups on your form.
There are ways to do this without a database lookup, but that will require values that are hardcoded on your form (and thus more difficult to update when changes happen - especially if this is going to be used in multiple forms).
How could I do it without a database lookup for one form?
If you don't want to have a database, that means you'll need either formulas or Javascript to make it work. And any changes to your list of departments and department heads will necessitate updating those formulas or Javascript.
Let's say you have three teams in your dropdown, Team A, Team B, and Team C, and for simplicity sake, let's say their email addresses are a@email.com, b@email.com, and c@email.com respectively.
If your department drop-down field has a variable name of select_department, then you could populate the email addresses into a single line field via a variable with nested IF statements. This is tested on both the Classic Designer and the New Designer on Version 11.0.2212.30907:
=IF(select_department="Team A", "a@email.com", IF(select_department="Team B", "b@email.com", IF(select_department="Team C", "c@email.com", "")))
Of course, that may get fairly unmanagable if you have a lot of departments.
You could also do it via Javascript. Using the same examples as above. Give the Department dropdown the CSS Class of selectDepartment, and give the Department Head Email field the CSS Class of departmentHeadEmail.
Here's Javascript tested on Classic Designer Version 11.0.2212.30907:
//The document ready function is automatically called when the form loads. $(document).ready(function() { //When the selectDepartment drop-down (select) field is changed, //the value of the departmentHeadEmail (input) field will be populated. $('.selectDepartment select').change(function() { var emailAddress = ''; if($(this).val() == 'Team A') { emailAddress = 'a@email.com'; } else if($(this).val() == 'Team B') { emailAddress = 'b@email.com'; } else if($(this).val() == 'Team C') { emailAddress = 'c@email.com'; } $('.departmentHeadEmail input').val(emailAddress); }); });
Here's Javascript tested on New Designer Version 11.0.2212.30907:
//Reference the form field by class name - the [0] refers to the first //field with that class name. var departmentField = LFForm.findFieldsByClassName('selectDepartment')[0]; var emailField = LFForm.findFieldsByClassName('departmentHeadEmail')[0]; //When the departmentField is changed, use it's value to determine and //populate the value of the emailField. LFForm.onFieldChange(function() { var emailAddress = ''; var departmentName = LFForm.getFieldValues(departmentField); if(departmentName == 'Team A') { emailAddress = 'a@email.com'; } else if(departmentName == 'Team B') { emailAddress = 'b@email.com'; } else if(departmentName == 'Team C') { emailAddress = 'c@email.com'; } LFForm.setFieldValues(emailField, emailAddress); }, departmentField );
Could I have multiple email addresses assigned to the same choice? We have a department with co-department heads and I would want the form be assigned to both of them.
Would something like this work in your first example?
=IF(select_department="Team A", "a@email.com", "b@email.com")
This:
=IF(select_department="Team A", "a@email.com", "b@email.com")
Is saying "if select_department is Team A, then return a@email.com, otherwise return b@email.com" so that wouldn't work.
If you are familiar at all with how the IF formula works in spreadsheets like Excel, it's the same method here: =IF(check if something is true, return this value if true, return this value if false). In the example I gave before of three nested IF statements, instead of "return this value if false", we went into a second IF statement, and if that was false, went into a third IF statement. It was only if all three were false that if returned the blank string "" at the end.
In order to return multiple email addresses for the team, we need to make sure those email addresses are in the same value, something like this:
=IF(select_department="Team A", "a@email.com,b@email.com", "")
That is saying "if select_department is Team A, then return 'a@email.com,b@email.com', otherwise return blank". It's returning both email addresses within a single value, separated by a comma.
Now, the part I have not tested, is taking the variable from that email field, and plugging it into the "Email Service Task" in your process diagram. I'm pretty sure it'll work, but since I haven't tested it, I can't say with certainty that it will like the comma between the two email addresses ('a@email.com,b@email.com'), maybe it would actually prefer a semi-colon instead ('a@email.com;b@email.com'), you'll just need to test that part to see.
I'm actually trying to plug it into a user service task. The employee submits the form with department head approval needed and I want it to be sent to the right DH so they can fill out their part of the form. I would want it so that the employee can choose their department and the department head's email would be attached somehow so they can do the next step of the process.
Oh! I misunderstood and thought you were just sending an email to someone.
Doing user tasks is probably going to be easier. We have a few options:
We can definitely continue with the options we listed before, but instead of listing the department head's email address - you'll want to list their User ID. Then on the User Task, you can assign to "Users" and use the variable from the populated User ID (what had been the email field in the examples before) as the user name(s) for the task assignment.
Or, if you have your teams in LFForms set-up to match your departments, you can completely ignore the formula and Javascript examples I gave before, and just use the Department drop-down field to ultimately select the Team you want. You can do this on the user task via the "Team based on variable" option, listing the Department Drop-down's variable in the "Team Name" option. In this case, you probably want a filter set-up on all of those department teams that specifically identifies the head of that department/team (this part can be a little time consuming to set-up, but it would be reusable). You would probably want to do this via a role that you add on all of the teams and a filter to return the users with that role (make sure you set it up the same way on all the teams, and name it the same thing on all the teams, because you are referring to it by name in the "Dynamic Filter" field on the user task). If the role is named "Department Head" for example, and you add the user(s) that are the head(s) of that department to that role in each team - then the filter (which you would add to every team) just returns all users that have that role, like this:
$result=team.findMembersByRole('Department Head');
Or, if you don't want to have a dedicated team for each department - maybe you have a single team of just department heads, and then have a role for each department. In your "Department Heads" team, you have roles for "Team A", "Team B", "Team C", etc. With the name of the role exactly matching how you listed the name of the Department on the drop-down field on the form. Then assign all the department heads to that team and give them the role for the specific department that they head. This is probably the easiest to set-up, because on your User Task, you'll select the "Team" option and select your "Department Heads" team, and then set-up a filter like this:
var departmentName = $util.getValue('select_department'); $result=team.findMembersByRole(departmentName);
So I feel like we're getting much closer, but I'm still running into issues. I want to do the second option - one team for all the department heads separated into roles - but the code given doesn't seem to be working. Here are the screenshots that I have.
You need to have them as a Team Member to have tasks assigned to them in that team.
Team Administrator lets them create/edit/delete the team, along with view and reassign tasks, and adding/removing users to the team - so you usually want to really limit who is marked that way.
Team Manager can view and reassign tasks, and adding/removing users to the team - so less restricted than Team Administrator, but you probably still want it to be limited to specific individuals.
And then Team Member is the actual members of the team.
And you can have more than one selected, so the Team Administrator for example could be also the Team Manager and Team Member.
Even though these are Department Heads, I'm guessing most of them are just going to be Team Members on this team of Department Heads.
That's the only issue I'm seeing on that screenshot, so hopefully that works.
Ok so I got the code working and everything, but the department head isn't receiving an email that a form has been submitted for them to sign. It shows up in their inbox but I want them to get an email notification too. I currently have the checkbox checked for emailing users when the task has been assigned or reassigned.
Do you have other processes in Forms sending emails already?
Because if Forms is configured to send emails and is already sending them, and the user is set-up with their email address, and you have the checkbox marked on the user task to send an email, then it should work.
So if it's not working, my questions relate to those items I listed:
- Is Forms configured to send emails? And is it tested and confirmed as working?
- Does the user's LFForms account include their email address?
- Is the User Task marked to send emails? (you already confirmed this one).
Yes and when I test the process with me as all of the roles, Forms sends me emails no problem. It is only when I try to send an email to someone other than me that they do not receive it. This was not an issue before updating Forms, but now that I'm on 11 update 3 somehow it's not working anymore. The person that I am sending an email to just has the team member security role, would that be an issue?