Recent searches


No recent searches

Ticket Import API and attachments



Posted Jan 20, 2023

Hey everyone!  I'm migrating some tickets to Zendesk, and using the imports/tickets endpoint.

I have uploaded some files that go along with the tickets, but when I try to create comments on my tickets, it claims that the uploads parameter is invalid for comments.  Here's a sample call:

    {
        "ticket": {
            "external_id": "x",
            "assignee_id": "x",
            "created_at": "2022-11-22T18:49:21Z",
            "subject": "Zendesk Merge Test Ticket:   Webform 3 with attachment",
            "description": "",
            "priority": "",
            "status": "open",
            "group_id": "x",
            "ticket_form_id": "x",
            "brand_id": "x",
            "requester_id": "x",
            "tags": [
                "no_verification_needed",
                "other/unknown",
                "other_call_back_request",
                "pre-application_help",
                "sms_verified_-_no_further_client_verification_needed"
            ],
            "custom_fields": {
                "request": {
                    "custom_fields": {
                        "id": x,
                        "value": "x"
                    }
                }
            },
            "comments": [
                {
                    "author_id": "x",
                    "created_at": "2022-11-22T18:49:21Z",
                    "body": "Original Comment."
                },
                {
                    "author_id": "x",
                    "created_at": "2023-01-04T15:13:54Z",
                    "body": "comment with 1 attachment",
                    "uploads": [
                        "zmxrhTsTvLJPmn7dVShufkoTA"
                    ]
                },
                {
                    "author_id": "x",
                    "created_at": "2023-01-08T06:38:05Z",
                    "body": "Testing multiple files.",
                    "uploads": [
                        "6kbSPIUHaDofksZBvznJRWlK1",
                        "qfhDkaNCVAwPN7Nnxc1an8vFw"
                    ]
                }
            ]
        }
    }

And here is the response:

    {
        "error": "RecordInvalid",
        "description": "Record validation errors",
        "details": {
            "uploads": [
                {
                    "description": "Uploads is invalid",
                    "error": "InvalidValue"
                }
            ]
        }
    }

Can I assume then, that I cannot attach uploads to comments as part of the import api?

If so, are there restrictions on adding comments (with uploads) to closed tickets?

If I update a ticket multiple times with {"ticket" : {"comment" : {"body":"x"},"uploads":["x":"y"], "author_id" : "x", "created_at" : date}} will it keep creating new comments?

Thanks!!

 


0

7

7 comments

Hi Ken,

In the meantime, have you found a solution ? I´m stucking at the same.

Best regards,

Marcus

 

0


Marcus Dawideit
Ken McCartney

Did you guys manage to solve this?

Am stuck on the same issue right now. My task is to import tickets from one Zendesk url to another Zendesk url.

Had no problem with using bulk import to create tickets with comments. However the attachment did not work.

Did ask the same query here
https://support.zendesk.com/hc/en-us/articles/4408887057946?page=1#comment_6162241414810


var axios = require('axios');

var dotenv = require('dotenv');

dotenv.config();

console.log('-----------------STARTING-----------------');

getTickets();

async function getTickets() {

  let currentPage = 1;

  // to be added back when scalling the app with API rates solution

  // url: `${url}/tickets.json?page=${currentPage}`,

  try {

    while (true) {

      const config = {

        method: 'GET',

        url: `${process.env.albinURL}/tickets`,

        headers: {

          'Content-Type': 'application/json',

          Authorization: `${process.env.albinAuth}`,

        },

      };

      const response = await axios(config);

      const ticketsData = response.data.tickets.slice(1, 2); // test ticket with attachmnet

      // to be added back when scalling the app with API rates solution

      // if (ticketsData.length === 0) {

      //   // No more tickets on this page, break the loop

      //   break;

      // }

      console.log('Page nr:', currentPage);

      console.log('Number of tickets on this page:', ticketsData.length);

      console.log('Total tickets from page', response.data.count);

      await setTags(ticketsData);

      await getComments(ticketsData);

      // const data = JSON.stringify({ tickets: ticketsData });

      // const isSuccess = await createTicket(data);

      // if (isSuccess) {

      getToken(ticketsData);

      // }

      // // to be added back when scalling the app with API rates solution

      // console.log(isSuccess);

      // if (res) {

      //   currentPage++;

      // } else {

      //   break;

      // }

      break;

      console.log('-----------------ENDING-----------------');

    }

  } catch (error) {

    console.log('Error:', error);

  }

}

async function createTicket(data) {

  console.log('CREATING TICKETS');

  var config = {

    method: 'POST',

    url: `${process.env.albinURL}/imports/tickets/create_many`,

    headers: {

      'Content-Type': 'application/json',

      Authorization: `${process.env.albinAuth}`,

    },

    data: data,

  };

  try {

    const response = await axios(config);

    console.log(response);

    console.log(response.status);

    return response.status === 200;

  } catch (error) {

    console.log(error.response.data);

    console.log('ERROR');

    return false;

  }

}

function setTags(ticketData) {

  ticketData.forEach((ticket) => {

    const modifiedData = {

      ...ticket,

      tags: ticket.tags

        ? [...ticket.tags, `FINID:${ticket.id}`]

        : [`FINID:${ticket.id}`],

    };

    // Update the original ticket with the modifiedData

    Object.assign(ticket, modifiedData);

  });

}

async function getComments(ticketData) {

  for (let i = 0; i < ticketData.length; i++) {

    const ticket = ticketData[i];

    try {

      const response = await axios({

        method: 'GET',

        url: `${process.env.albinURL}/tickets/${ticket.id}/comments`,

        headers: {

          'Content-Type': 'application/json',

          Authorization: `${process.env.albinAuth}`,

        },

      });

      // Add comments to the ticket

      ticket.comments = response.data.comments;

    } catch (error) {

      console.error(`Error fetching comments for ticket ${ticket.id}:`, error);

    }

  }

}

function getToken(tickets) {

  tickets.forEach((ticket) => {

    console.log(`TicketID: ${ticket.id}`);

    ticket.comments.forEach((comments) => {

      if (comments.attachments.length > 0) {

        comments.attachments.forEach((comment) => {

          console.log(comment.file_name);

          console.log(comment.content_type);

          console.log(comment.url);

        });

      } else {

        console.log('No attachmnet to this comment');

      }

    });

  });

}




0


image avatar

Christopher Kennedy

Zendesk Developer Advocacy

Hi Albin,
 
When you include attachments in the ticket comment, you first have to upload the file.  We break down this two-part process in Adding ticket attachments with the API.  So in your case, you'd need to download the file from the exported ticket and then upload it to the new instance to include in the imported ticket. 

0


Hi Christopher Kennedy

Thx for the reply. 

So in this case I will need 3 steps:

1. Download the file from the source Zendesk environmnet.
2. Upload the file and get the token towards the target Zendesk environmnet.
3. Attach the file to the ticket, again towards the target Zendesk environment.

Will this work when I am using the endpoint; /imports/tickets/create_many.
Do want to use this endpoint as I get the time metrics.

, Albin




0


image avatar

Christopher Kennedy

Zendesk Developer Advocacy

Hi Albin,
 
Yes, you would use the same process for bulk importing tickets with comment attachments.

0


I've tried this and have been getting mixed results. As with Albin's code I've been doing something similar. I download the file from another source, upload into Zendesk. retrieve the token from the response from Zendesk and add that into a JSON for create_many endpoint.  It looks all good but when I view my bulk imported tickets the attachments are not to be found broken images.  And when I view the ticket details via the API the attachment block is empty.

 

0


image avatar

Erica Girges

Zendesk Developer Advocacy

Hi Jason and Albin, 
 
Sorry to hear you've been having a difficult time getting your attachments to work via API. 
 
Out of curiosity, do you either of you happen to have private attachments enabled

0


Please sign in to leave a comment.

Didn't find what you're looking for?

New post