Display custom fields from a query of multiple tickets

답변함

4 댓글

  • 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
  • Raven Agape

    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
  • 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
  • Raven Agape

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

    0

댓글을 남기려면 로그인하세요.

Zendesk 제공