最近の検索


最近の検索はありません

Display custom fields from a query of multiple tickets

回答済み


投稿日時:2021年8月16日

Hello! I've done a lot of python work with the Zendesk REST API, but am new to the javascript side of things in ZAF. 

Here is what I want to do; Make a sidebar app that:

1. Retrieves Custom field value from ticket.
2. Queries for all tickets with that same value
3. Loop through those tickets extracting some other custom field values and displaying them in the app in table form.

1. I have built the app and retrieved the custom value

2. looking for code snippets or resources for how to do a query and put it into an array of ticket ids.

3. looking for code snippets or resources on how to loop through that array, doing individual queries of those tickets for custom field values and putting those into this idea from the code samples:

function showInfo() {
var requester_data = {
'name': 'Jane Doe',
'tags': ['tag1', 'tag2'],
'created_at': 'November 20, 2014',
'last_login_at': 'June 27, 2016'
};

End result I'm looking for is an App in right sidebar that looks like this:

CUSTOM FIELD RELATED TICKETS
Ticket#.    Custom Field.    Custom Field.     Custom Field    
Ticket#.    Custom Field.    Custom Field.     Custom Field
Ticket#.    Custom Field.    Custom Field.     Custom Field

 

Thanks for any help or pointers!


0

4

4件のコメント

image avatar

Tipene Hughes

Zendesk Developer Advocacy

Hey Raven Agape,

Thanks for reaching out!

One possible way you could go about this is by using the client.get() method to retrieve the custom field value, and then using the client.request() method in conjunction with the Search API to query all tickets based on the custom field value. Here’s a code snippet I put together which will achieve that:

const getCustomField = async () => {
    // Grabs custom field value - replace the {id} placeholder with your custom field ID
    const customFieldData = await client.get(
        "ticket.customField:custom_field_{id}"
    );
    const customFieldValue =
    customFieldData["ticket.customField:custom_field_{id}"];

    // Request using the custom field value to query via Search API
    const settings = {
        url: `/api/v2/search.json?query=fieldValue:${customFieldValue}`,
        type: "GET",
        dataType: "json",
    };
    const matchingTickets = await client.request(settings);

    // Return array of tickets with matching custom ticket fields
    return matchingTickets.results;
  };

getCustomField();

This will return an array of ticket objects which you can then iterate over to determine if they have additional custom field with values.

I hope this helps! Feel free to reach out with any questions.

Tipene

0


Thank you so much! This has really put me on the right track.

Can you give me a pointer on how to parse the array? 

Here is what I get back:

MatchingTickets call results: {results: Array(4), facets: null, next_page: null, previous_page: null, count: 4}

I tried various like [4] or results.body or results.text.... I can see in the console the full array, but I can't find the syntax for digging into the individual tickets and their subsequent field values.

Any help appreciated!

 

0


image avatar

Tipene Hughes

Zendesk Developer Advocacy

I'm glad it's got you moving in the right direction!

Here's an example that might help you pull the field values from the array:

getCustomField().then(res => {

    // Array to store custom field values
    const hasCustomField = [];

    // Iterate over matchingTickets.results array
    res.forEach(ticket=> {
            // Iterate over custom fields of each ticket
            ticket.custom_fields.forEach(fields=> {
                    // If custom field value is present push to array
                    fields.value != null && hasCustomField.push(fields.value);
            });
    });

    // Return array of custom field values
    return hasCustomField;
});

I hope this helps! Let me know if you have any questions.

Tipene

0


this did the trick, forgot to come back and say "Thanks!!!"

0


サインインしてコメントを残してください。

お探しのものが見つかりませんか?

新規投稿