RE: Sending Email to scripted recipient in workflow notifications

Jamie Sammons, modified 3 Years ago. New Member Posts: 6 Join Date: 5/23/22 Recent Posts

I am currently woking on a workflow on submission for a Liferay form.
In this form is a field for email address so the user filling out the form can put his email in this field and on submitting the form the workflow should get all the options and values of the form and send a confirmation email to the address provided by the user.

Just for clarification!!     The user is not a "user" in the liferay system that has his own ID but a guest that does not log into the site and simply provides his email address in the form.

In the Liferay workflow i created a task which should handle all of that.
In the task there is the notification tab and in there i selected the scripted recipient.

So my question is how do i simply take the email address from the form field and set it as recepient email address?

Jamie Sammons, modified 3 Years ago. New Member Posts: 6 Join Date: 5/23/22 Recent Posts

                    import com.liferay.portal.kernel.workflow.WorkflowConstants;
                    import com.liferay.portal.kernel.util.GetterUtil;
                    import com.liferay.dynamic.data.mapping.model.DDMFormInstanceRecordVersion;
                    import com.liferay.dynamic.data.mapping.service.DDMFormInstanceRecordVersionLocalServiceUtil;
                    import com.liferay.dynamic.data.mapping.storage.DDMFormFieldValue;
                    import com.liferay.dynamic.data.mapping.model.Value;

                    // Fetch the form record Id from the workflow context
                    long recVerId = GetterUtil.getLong((String)workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));

                    // Initialize form record version
                    DDMFormInstanceRecordVersion recVer = DDMFormInstanceRecordVersionLocalServiceUtil.getFormInstanceRecordVersion(recVerId);

                    // Retrieve the form field values
                    List<DDMFormFieldValue> formFieldVals = recVer.getDDMFormValues().getDDMFormFieldValues();

                    for (DDMFormFieldValue fmval : formFieldVals) {
                        Value val = fmval.getValue();
                        String data = val.getString(Locale.ROOT);
                        String name = fmval.getName();
                        System.out.println("Form field values : "+name+"| data : "+data);
                    }
                    return;

This is the code i have in the task to get the fields from the form
 

 

thumbnail
Mohammed Yasin, modified 3 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts

Hi, 

AFAIK, you can either assign user or list of roles in scripted recipient. You can check below code for reference:

 <scripted-recipient>
					<script>
					<![CDATA[
						import com.liferay.portal.kernel.model.Role;
						import com.liferay.portal.kernel.model.User;
						import com.liferay.portal.kernel.service.RoleLocalServiceUtil;
					    import com.liferay.portal.kernel.util.GetterUtil;
						import com.liferay.portal.kernel.workflow.WorkflowConstants;
			
						
						long companyId = GetterUtil.getLong((String)workflowContext.get(WorkflowConstants.CONTEXT_COMPANY_ID));
						roles = new ArrayList<Role>();

						Role role = RoleLocalServiceUtil.getRole(companyId, "Administrator");
						roles.add(role);
						user=null;  						
					]]>
					</script>
					<script-language>groovy</script-language>
				</scripted-recipient>

 

Jamie Sammons, modified 3 Years ago. New Member Posts: 6 Join Date: 5/23/22 Recent Posts

Hey thanks for your reply!
Is there no way to just set the email address alone as scripted recipient?
Because im trying to send an email to the person that filled out the form that is not a user in the liferay system 

thumbnail
Mohammed Yasin, modified 3 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts

Hi ,

In that case you can just call your custom service for sending email inside <scripted-recipient> action block with the email address you want to send to.

Jamie Sammons, modified 3 Years ago. New Member Posts: 6 Join Date: 5/23/22 Recent Posts

Well i want to avoid using my own custom mail service because that would kind of defeat the whole purpose of the workflow.
If i have a user that is logged in i could just set the user as email notification recipient but if i have a "user" that is not logged in and is not in the user database and does not have an account i want to simply get the email from a field in the form the user fills out and then set the email address i got as notification recipient.
I have the script working to the point where i get the email address from the field as a string variable, so now all that is missing is how to declare this string as the email address for the scripted recipient.
 

import com.liferay.portal.kernel.workflow.WorkflowConstants;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.dynamic.data.mapping.model.DDMFormInstanceRecordVersion;
import com.liferay.dynamic.data.mapping.service.DDMFormInstanceRecordVersionLocalServiceUtil;
import com.liferay.dynamic.data.mapping.storage.DDMFormFieldValue;
import com.liferay.dynamic.data.mapping.model.Value;

// Fetch the form record Id from the workflow context
long recVerId = GetterUtil.getLong((String)workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));

// Initialize form record version
DDMFormInstanceRecordVersion recVer = DDMFormInstanceRecordVersionLocalServiceUtil.getFormInstanceRecordVersion(recVerId);

// Retrieve the form field values
List<DDMFormFieldValue> formFieldVals = recVer.getDDMFormValues().getDDMFormFieldValues();

String emailFieldReference = "Field34735534";

//get email address from form fields and send mail with KexMailUtil
for (DDMFormFieldValue fmval : formFieldVals) {
    Value val = fmval.getValue();
    String emailAddress = val.getString(Locale.ROOT);
    String name = fmval.getName();

    if(name.equals(emailFieldReference)){
        
        //HERE SET SCRIPTED RECIPIENT EMAIL ADDRESS
    }
}
return;

 

Jamie Sammons, modified 3 Years ago. New Member Post: 1 Join Date: 6/9/22 Recent Posts

Well i want to avoid using my own custom mail service because that would kind of defeat the whole purpose of the workflow.
If i have a user that is logged in i could just set the user as email notification recipient but if i have a "user" that is not logged in and is not in the user database and does not have an account i want to simply get the email from a field in the form the user fills out and then set the email address i got as notification recipient.