Discussions
Moving Attachment and Note from Exisiting Job to New Job
5 days ago by Alger Krames
Hi,
I want to ask how can I move the attachment and note from existing job to a New Job.
This is my code currently:
`
const attachRes = await axios.get(
`https://api.servicem8.com/api_1.0/attachment.json?$filter=job_uuid eq '${jobUuid}'`,
{ headers: authHeaders() }
);
console.log("attachRes", attachRes.data);
// Photos = attachments with mime_type starting with "image/"
const photos = attachRes.data.filter(att => att.mime_type?.startsWith("image/"));
// Copy photos
for (const photo of photos) {
const fileBuffer = await axios.get(photo.url, { responseType: "arraybuffer" });
const formData = new FormData();
formData.append("job_uuid", newJobUuid);
formData.append("file", fileBuffer.data, { filename: photo.filename || "photo.jpg" });
await axios.post(
"https://api.servicem8.com/api_1.0/attachment.json",
formData,
{
headers: {
...authHeaders(true),
...formData.getHeaders(),
},
}
);
}
// Copy specific attachments by keyword
const selectedAttachments = attachRes.data.filter(att =>
ATTACHMENT_KEYWORDS.some(keyword => att.filename.includes(keyword))
);
for (const att of selectedAttachments) {
const fileBuffer = await axios.get(att.url, { responseType: "arraybuffer" });
const formData = new FormData();
formData.append("job_uuid", newJobUuid);
formData.append("file", fileBuffer.data, { filename: att.filename });
await axios.post(
"https://api.servicem8.com/api_1.0/attachment.json",
formData,
{
headers: {
...authHeaders(true),
...formData.getHeaders(),
},
}
);
}
//Transfer Notes
const notesRes = await axios.get(
`https://api.servicem8.com/api_1.0/jobcomment.json?$filter=job_uuid eq '${jobUuid}'`,
{ headers: authHeaders() }
);
console.log("notesRes", notesRes.data);
const selectedNotes = notesRes.data.filter(note =>
NOTE_KEYWORDS.some(keyword => note.text.includes(keyword))
);
for (const note of selectedNotes) {
await axios.post(
`https://api.servicem8.com/api_1.0/jobcomment.json`,
{
job_uuid: newJobUuid,
text: note.text,
},
{ headers: authHeaders() }
);
}
//Link Jobs in Diaries
await axios.post(`https://api.servicem8.com/api_1.0/jobcomment.json`, {
job_uuid: newJobUuid,
text: `#${existingJob.job_no}`,
}, { headers: authHeaders() });
await axios.post(`https://api.servicem8.com/api_1.0/jobcomment.json`, {
job_uuid: jobUuid,
text: `#${createJobRes.data.job_no}`,
}, { headers: authHeaders() });
`
and it gives me this error:
Error: {
errorCode: 400,
message: 'jobcomment is not an authorised object type',
documentation: 'Please refer to https://developer.servicem8.com/llms.txt for developer documentation.'
}
I've got error when fetching attachments and notes.
do you any process on how to fix the error or process that I can move the attachment and notes to a new job?
Thank you