0

How do I add notes and attachments to opportunity objects via SObject Tree. I am dong this via java script ajax POST "New Notes" "Attach File"

"Opportunities": {
 "records": [{
   "attributes": { "type": "Opportunity", "referenceId": "opp1" },
   "name": "Closing Soon",
   "closedate": "2018-09-01",
   "stagename": "Prospecting"
 }]

1 Answer 1

0

Create a Note or Attachment simply by providing them as children records of the parent record; reference the relationship name Notes or Attachments to insert the appropriate type of record.

Example:

{
 "records": [{
   "attributes": { "type": "Opportunity", "referenceId": "opp1" },
   "name": "Closing Soon",
   "closedate": "2018-09-01",
   "stagename": "Prospecting",
     "Notes": { "records": [{
       "attributes": { "type": "Note", "referenceId": "note1" },
       "title": "This is a note",
       "body": "This is the content of the note"
     }]},
     "Attachments": { "records": [{
       "attributes": { "type": "Attachment", "referenceId": "att1" },
       "name": "filename.txt",
       "body": "VGhpcyBpcyB0aGUgY29udGVudCBvZiB0aGUgYXR0YWNobWVudA==",
       "contenttype": "text/plain"
     }]}
 }]
}

Please note that the body of an attachment must be base64-encoded.

7
  • so if the user browse a file from their computer image, or pdf, it has to change to base64 first , and then pass that to the body
    – Metawaa
    Mar 9, 2018 at 19:58
  • @Metawaa Yes. You can use FileReader.readAsDataURL to convert the file to base64, then upload the body part.
    – sfdcfox
    Mar 9, 2018 at 20:03
  • do I need to have the extension for the file in conteentType? like if is PDF or JPEG?
    – Metawaa
    Mar 9, 2018 at 20:08
  • 1
    @Metawaa Use the content type that the browser gives you from the data URI. It should be a mime type, like "image/jpeg" or "application/pdf".
    – sfdcfox
    Mar 9, 2018 at 20:16
  • how do you make it so if attachment is empty, still submits the sobject tree. right now when its empty , it errors out
    – Metawaa
    Mar 12, 2018 at 17:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .