PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Custom Routing Based On Form Fields

  1. Hello,

    Having a bit of trouble trying to get custom routing working. I get no errors, but I also do not get the email notifications.

    I have two address fields and I need to get the state from each. If neither of these fields are Illinois, it should route to emailone@email.com else send to emailtwo@email.com.

    Here is my filter and function:

    add_filter("gform_notification_2", "route_notification", 10, 3);
    // gform_notification_2 is form ID 2
    function route_notification($email_to, $entry) {
    global $post;
    $fromstate = $entry["4.4"]; //the first number (4) is address field id, the second number is the state field id in the address field.
    $tostate = $entry["12.4"] ; //the first number (12) is address field id, the second number is the state field id in the address field. 
    
    if ($fromstate != "Illinois" || $tostate != "Illinois") {
    		$email_to = 'emailone@email.com';
    	} else {
    		$email_to = 'emailtwo@email.com';
    }
    	return $email_to;
    }

    Any assistance would be greatly appreciated. I think my main concern/issue is that I may not be getting field values correctly.

    Posted 11 years ago on Thursday May 30, 2013 | Permalink
  2. OK.. so I had some issues with the filter (had a mix of deprecated and new code). However, I am still not there yet. Everything seems to be working but the ability to check field values for the from and to state. It appears I am unable to get the form post values. So my IF statement always fires off emailtwo@email.com.

    So here is what I have at the moment. Any thoughts on how I can get the field values so I can check the $fromstate and to $tostate?

    add_filter("gform_notification_2", "route_notification", 10, 3);
    
    function route_notification($notification, $form, $entry) {
    
    global $post;
    global $fromstate;
    global $tostate;
    
    	if($notification["name"] == "Admin Notification"){
    
    	$notification['toType'] = "email";
    	$fromstate = $_POST["input_4.4"];
    	$tostate = $_POST["input_12.4"] ;
    
    			if ($fromstate = "Illinois" || $tostate = "Illinois") {
    					$notification['to'] = "emailone@email.com";
    				} else {
    					$notification['to'] = "emailtwo@email.com";
    			}
    	}
    
    return $notification;
    }
    Posted 11 years ago on Friday May 31, 2013 | Permalink
  3. David Peralty

    Is there any reason why you aren't using Conditional Logic in our Notifications system to do this? You could have a Notification that says: Send this email if all the following are true:
    if user selects Illinois in statefrom
    If user selects Illinois in stateto

    Posted 11 years ago on Friday May 31, 2013 | Permalink
  4. Hi David,

    Thanks for the reply. I would love to use the built in conditional logic on notifications but it does not look like address fields are selectable as part of the list of selectable form fields. All my other fields show up but the ones in an address field (address, city, state, zip).

    I am trying to target the address.state field for the from and to addresses. I know I can create these fields individually and not use an address field, but need to use the address fields as they need to sit side by side and is part of a multi page form.

    So.. I am trying to do this via code instead.

    Posted 11 years ago on Friday May 31, 2013 | Permalink
  5. David Peralty

    I think when they are posted the fields become input_4_4 instead of input_4.4. Try that and let me know.

    Posted 11 years ago on Friday May 31, 2013 | Permalink
  6. No luck. I tried the following formats...

    input_4_4
    input_12_4

    input_2_4_4
    input_2_12_4

    I was also incorrect. The email I get is the default email when you setup an admin notification. So, no matter what logic I apply, it always send to the notification address setup in the notifications. So... neither of these two email I have in my code get triggered.

    Posted 11 years ago on Friday May 31, 2013 | Permalink
  7. Dave, thanks for all the help. I got it working. I had to change the name of my email notification from Admin Notification to my Admin Email (which is what I named mine)

    Here is the working version if anyone else happens to be using the address field and needs to use routing based on the address.state field...

    add_filter("gform_notification_2", "route_notification", 10, 3);
    
    function route_notification($notification, $form, $entry) {
    
    global $post;
    global $fromstate;
    global $tostate;
    
    	if($notification["name"] == "Admin Email"){ // The name of my admin notification 
    
    	$notification['toType'] = "email";
    	$fromstate = $_POST["input_4_4"]; //my form field id
    	$tostate = $_POST["input_12_4"] ; //my form field id
    
    			if ($fromstate == "Illinois" || $tostate == "Illinois") { // Here I am checking for two fields and if they = Illinois
    					$notification['to'] = "emailtwo@email.com"; //If illinois send to the iaddress
    				} else {
    					$notification['to'] = "emailtwo@email.com"; // If no illinois send to this address
    			}
    	}
    
    return $notification;
    }

    Would be cool to add these fields to the built in conditional logic.

    Posted 11 years ago on Friday May 31, 2013 | Permalink