Skip to content

Instantly share code, notes, and snippets.

@onecrayon
Created March 23, 2017 22:33
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onecrayon/a1e5d3d30b737856dde1b193205b1c95 to your computer and use it in GitHub Desktop.
Save onecrayon/a1e5d3d30b737856dde1b193205b1c95 to your computer and use it in GitHub Desktop.
/**
* 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/new?email=test@test.com&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
Copy link

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