Skip to content

Instantly share code, notes, and snippets.

@dpawluk
Created December 28, 2016 19:48
Show Gist options
  • Save dpawluk/85e2467a9ddd77af6dd0286779da3035 to your computer and use it in GitHub Desktop.
Save dpawluk/85e2467a9ddd77af6dd0286779da3035 to your computer and use it in GitHub Desktop.
Get user data using the ticket.requester while in the ticket_sidebar location
<html>
<head>
<style type="text/css">
h2 {
border: 5px solid #efefef;
border-radius: 4px;
padding: 5px;
font-size: 100%;
text-align: center;
margin-bottom: 30px;
}
html {
background: #f9f9f9;
}
</style>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-jsonview/1.2.3/jquery.jsonview.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h2>Display Requester Fields</h2>
<div id="field_display">
<pre id="json-viewer">
</pre>
</div>
<!-- https://github.com/zendesk/zendesk_app_framework_sdk -->
<script type="text/javascript" src="https://assets.zendesk.com/apps/sdk/2.0/zaf_sdk.js"></script>
<!-- jquery from cdn -->
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous">
</script>
<!-- make pretty json, yay ZAFv2!! -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-jsonview/1.2.3/jquery.jsonview.min.js" type="text/javascript">
</script>
<script>
// Initialise the Zendesk JavaScript API client
// https://developer.zendesk.com/apps/docs/apps-v2
var client = ZAFClient.init();
client.invoke('resize', { width: '100%', height: '400px' });
client.on('app.registered', function(){
//when the app fires, grab the requester's user id
client.get('ticket.requester').then(function(response){
var requester_id = response["ticket.requester"].id;
//once we have the id we can pass it to the handler for getting the user data including custom fields
getUserData(requester_id);
});
});
function getUserData(id){
//want string interpolation but I'll just deal w/it
var url = '/api/v2/users/' + id + '.json';
//Because we are in the ticket_sidebar location, this instance of 'client' knows nothing about user objects, instead we just use the Zendesk REST API as below
client.request(url).then(function(res){
var user = res.user;
var custom_field_data = user.user_fields;
//Lets just log the user fields and display the entire user json object for example
console.log(custom_field_data);
//Now just do the things with the fields
$('#json-viewer').JSONView(user, { collapsed: true });
});
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment