Created
March 23, 2017 22:33
-
-
Save onecrayon/a1e5d3d30b737856dde1b193205b1c95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Automatically populates Zendesk public-facing "new ticket" fields | |
* on page load. | |
* | |
* To use, copy and paste into your New Request Page template (wrapped | |
* in a `<script></script>` tag). | |
* | |
* For most fields, just use the field name (so if the field is | |
* `request[subject]`, use `subject` in the URL, while | |
* `request[custom_fields][123456]` becomes `123456`). You can also use | |
* `email` instead of `anonymous_requester_email`. For custom field | |
* dropdowns, specify the value, not the label (so something like | |
* `my_value` rather than `My Value`). | |
* | |
* You can also map custom fields to arbitrary names via the `aliases` | |
* object below. | |
* | |
* Example URL: | |
* | |
* /hc/en-us/requests/[email protected]&subject=Example+Subject | |
*/ | |
(function ($) { | |
// This is the only portion of the script you might wish to edit. | |
// It maps custom field numeric IDs to custom names you can use | |
// in the URL. So in the example below, `?custom_name=test` would | |
// set `#request_custom_fields_123` to `test`. | |
var aliases = { | |
'custom_name': '123' | |
} | |
// You should not need to edit anything past this point | |
var parsedQueryString = function () { | |
var segments = window.location.search.substr(1).replace(/\+/g, ' ').split('&'), | |
parsed = {}; | |
if (!segments) return parsed; | |
for (var i = 0, count = segments.length; i < count; i++) { | |
var parts = segments[i].split('='), | |
key = parts.shift(), | |
value = parts.length ? decodeURIComponent(parts.join('=')) : null; | |
parsed[key] = value; | |
} | |
return parsed; | |
} | |
$(document).ready(function () { | |
var query = parsedQueryString(); | |
for (var key in query) { | |
if (!query.hasOwnProperty(key) || key == 'ticket_form_id') continue; | |
var prefix = '#request_', | |
value = query[key]; | |
if (typeof aliases[key] !== 'undefined') { | |
key = aliases[key]; | |
} | |
if (key == 'email') { | |
prefix += 'anonymous_requester_'; | |
} else if (/^\d+$/.test(key)) { | |
prefix += 'custom_fields_'; | |
} | |
var field = $(prefix + key), | |
data = field.attr('data-tagger'); | |
if (data) { | |
data = JSON.parse(data); | |
var label = ''; | |
if (!value) { | |
label = data[0].label; | |
} else { | |
for (var i = 0, count; i < data.length; i++) { | |
if (data[i].value == value) { | |
label = data[i].label; | |
break; | |
} | |
} | |
} | |
field.val(value).closest('.nesty-input').text(label); | |
} else { | |
field.val(value); | |
} | |
} | |
}) | |
})(jQuery) |
@GeorgeManning Sorry, I haven’t used Zendesk for years. It’s quite likely they have updated their form logic in a way that breaks this script.
Hi George, the issue is jQuery is not added to your site. As this script relies on Jquery it fails. Once you add jQuery to Zendesk the script will work as expected. Thanks
Hi George, the issue is jQuery is not added to your site. As this script relies on Jquery it fails. Once you add jQuery to Zendesk the script will work as expected. Thanks
Thanks @JesseNovice. Appreciate your help.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Anyone try this recently? It's not working for me. I first added <script> and </script> tags to the top of the new_request_page.hbs, then pasted lines 22-78 after the <script> and before </script>. Saved the changes and publish. Then test with hc/en-us/requests/new?email=[email protected] doesn't autofill. Full test: https://georgetest.zendesk.com/hc/en-us/requests/[email protected] and another: https://georgetest.zendesk.com/hc/en-us/requests/new?description=Your+string+goes+here