Need help in copying a ticket field's value to another ticket field

Answered


Posted Jun 25, 2021

I need to copy value of one of the custom ticket fields to another custom field. So, I am trying to get the value of field1 using "client.get()" and once it is returned, trying to assign it to field2 using "client.set()", as shown in the code.

But it isn't working. Can someone please suggest what's is wrong with this code or provide an alternative code to achieve this?

I am executing this on "ticket.save" event in ticket_sidebar location.

var client = ZAFClient.init();
client.invoke('resize', { width: '100%', height: '200px' });

client.on('ticket.save', function(data) {
client.get('ticket.customField:custom_field_900009336126').then(function(data){

console.log('fulfilled'); //this is printing
console.log("Field_1_Value", data['ticket.customField:custom_field_900009336126']); //this is printing

//Assign value of field_1(900010861363) to another field_2(900005555555)
client.set('ticket.customField:custom_field_900010861363', data['ticket.customField:custom_field_900005555555']); //this doesn't work

console.log('Print Last Line'); //this is printing

});

});

0

12

12 comments

image avatar

Eric Nelson

Zendesk Developer Advocacy

Hey Rishi! 

With you wrapping the 'get' into the ticket save event it's causing an issue with the order of operations. Basically what is happening is the ticket has begun saving while your function is still updating that field. Due to that it's going to come back empty.

If you were to remove the client.on wrapper and just use the client.get function it would solve your issue.

client.get('ticket.customField:custom_field_900009336126').then(function(data){

console.log('fulfilled'); //this is printing
console.log("Field_1_Value", data['ticket.customField:custom_field_900009336126']); //this is printing

//Assign value of field_1(900010861363) to another field_2(900005555555)
client.set('ticket.customField:custom_field_900010861363', data['ticket.customField:custom_field_900005555555']); //this doesn't work

console.log('Print Last Line'); //this is printing

});


Let me know if you need anything else or have any questions,

Thanks,

Eric

0


Hi Eric

Thanks for providing this information. But this won’t solve the problem. In the code, which I have provided, “Get” is returning value of field1. But “Set” is not setting the value of field2.

My use case is that on or after save operation I need to copy value of field1 to field2. 

To achieve that, I thought of first getting the value during or after save operation and then copying it to field2. Can you please suggest how can I do that before/on/after save operation?

Thanks

Rishi

0


image avatar

Eric Nelson

Zendesk Developer Advocacy

Hey Rishi,

Appreciate the additional context. So if I'm understanding this correct -  A user fills out field 1 and saves the ticket. You then want field 1 to get copied into field 2.

Do you mind if I ask what you're accomplishing with this? Also is it necessary for this copy to happen during or immediately after ticket save? This could easily be solved with an automation if it's not time sensitive. 

Any information on what you're trying to solve / accomplish would be much appreciated.

Thanks,

Eric

0


Hey Eric

It's not that time sensitive but it is preferred to update field2 around the same time when the ticket is being saved, because it is referred in reports.

The exact requirement is to consolidate values from various fields into one, so that in reporting I don't need to pull all those different fields but only the summary field (field2).

Actually, we have multi-level root cause (level1, sub Category Level 2, Sub Category Level 3). The ask is to store value of each level in three different fields. The functionality should be such that based on the value selected in Level 1, next set of corresponding sub category values should appear in Level 2 field and so on for Level 3 (based on Level 2 value). But since Zendesk doesn't support conditional filtration of values in subsequent levels (i.e, based on parent value), I am using conditional fields functionality. But for that, I need to create separate fields for all different buckets of root causes at Level 2 & 3.

Now when it comes to reporting, all these fields need to be pulled. To avoid it, we thought of creating 2 hidden fields for Level 2 and Level 3 respectively, so that when the ticket is saved, value chosen in any of the Level 2 category fields should be copied to this hidden summary field "Root Cause Level 2" and likewise for Level 3 in "Root cause Level 3". In report, I will pull only these two fields.

That's what we are trying to achieve. I hope I am able to provide the context here. :)

Thanks

Rishi Mehtani

0


image avatar

Eric Nelson

Zendesk Developer Advocacy

So you could accomplish this without having to use a custom app and instead just using a trigger and target combination.

What you'd need to do is: 

1. Create an "HTTP" target extension (not a "URL" target) using PUT method and Content Type of JSON.

2. Give the target extension a URL value of: https://your_subdomain.zendesk.com/api/v2/tickets/{{ticket.id}}

3.Enable Basic authentication and enter an agent level email and password. If you are using an "API token", the format for the email should be john@example.com/token with the password being the API token. See reference documentation for details.

4. Then, similar to the above, create a Trigger that fires on ticket update. Have a "Notify target" action that calls the above HTTP target extension created in steps 1 thru 3

5. For the body of the trigger action's request you pass a JSON payload, similar to what's documented at PUT /api/v2/tickets/{id}.json

The trigger action's JSON for the above example would look like:

{
"ticket":{
"custom_fields": [{"id": 25356371(this is the field you are updating), "value": "{{ticket.ticket_field_12345}}, {{ticket_field_6789}} (<- the fields you're copying from)"}]
}
}

Another note is that, technically, calling into your Zendesk instance from a target extension is not officially supported. It can work, but there are possible issues depending on the action, what other triggers are doing, and the expectation of the agent on seeing those background changes surface in the UI.

All that said, the above steps should accomplish what you are trying to accomplish. Let me know how it works!

1


Thanks @....... This is also a good solution. I learnt this for the first time.

However, I might have to apply certain conditions rather than simply copying the value. But I would definitely use this approach as well, wherever applicable,

I have also figured out another way to update custom fields using the background app which uses API in turn to update the values.

But I am still unable to understand why isn't "Set" statement executing under "Get" in Trigger.save event. If in case, you have an idea, can you please help in understanding it?

 

Thanks

Rishi Mehtani

0


image avatar

Eric Nelson

Zendesk Developer Advocacy

Hey Rishi,

Glad you learned something! As for your other question, it still relates to the order of operations.

Ticket.save actions are async so you'll need to adjust your code to utilize promises at the top of the get /set function so that it follows the correct order and updates the field before actually saving the ticket. The way you have it written now, everything is running asynchronously so the ticket is saving, before the get/set promise is complete. Thus not able to complete. I've adjusted your code below to show how to use promises to solve this. 

client.on('ticket.save', function(data) {
// by adding in 'return' we are moving the promise one level up, this allows for the full get/set promise to complete before saving.
return client.get('ticket.customField:custom_field_900009336126').then(function(data){

console.log('fulfilled'); //this is printing
console.log("Field_1_Value", data['ticket.customField:custom_field_900009336126']); //this is printing

//Assign value of field_1(900010861363) to another field_2(900005555555)
client.set('ticket.customField:custom_field_900010861363', data['ticket.customField:custom_field_900005555555']);

console.log('Print Last Line'); //this is printing

});

});


Hope this helps!

Eric

1


It worked. Really appreciate your help. :)

Thanks

Rishi

0


image avatar

Eric Nelson

Zendesk Developer Advocacy

Glad we got that sorted out for you! 

All the best,

Eric

0


image avatar

Heather Rommel

Zendesk LuminaryThe Product Manager Whisperer - 2021Community Moderator

Eric Nelson,

Regarding the comment on the Trigger/Target combo, obviously going forward we're to use Webhooks -- but my question is, any chance we can use this to copy an incident ticket fields to the problem ticket fields? We could add a text field to store the source/destination ticket number if needed. Thoughts?

1


image avatar

Eric Nelson

Zendesk Developer Advocacy

Hey Heather,

Just to make sure I answer appropriately, are you referring to pushing the fields from an incident into a different ticket's (the problem ticket) fields?

Thanks! 

0


image avatar

Heather Rommel

Zendesk LuminaryThe Product Manager Whisperer - 2021Community Moderator

Hi Eric Nelson,

Yes, exactly! We'd love to be able to copy an Incident's set of fields to a problem ticket on create, and in some circumstances, on update. Bonus points if we can create the Problem ticket and auto-link the Incident to the Problem ticket too!

0


Please sign in to leave a comment.

Didn't find what you're looking for?

New post