0

I am trying insert/update attachment on a record by making use of Named Credential that I have setup on a different org.

I am passing the attachment properties (Name, Body, ParentId, Description) from my LWC component to the Auraenabled apex method like below:

LWC code:

uploadFile() {
  let attachment = {};
  attachment.Name = 'resume.pdf';
  attachment.ParentId = 'a0H3I000001SstEUAS';
  attachment.Body = this.base64String;
  attachment.Description = 'Resume';

  // call to apex method
  uploadAttachmentToAccount({file: JSON.stringify(attachment)})
  .then((response) => {
    // actions based on the response
  })
  .catch((error) => {
    // handle error
   })
  
}

Apex controller code:

In this apex method where and how should I use the file string I receive from LWC in the below request to upsert the attachment ?

@AuraEnabled
public static string uploadAttachmentToAccount(String file){
   HttpRequest request = new HttpRequest();
   request.setMethod('PUT');
   request.setEndpoint('callout:SalesforceTest/services/data/v48.0/sobjects/Attachment');
        
   Http http = new Http();
   HTTPResponse res = http.send(request);
   String str = res.getBody();
   return str;
}      
  • You can deserialize file using JSON.deserializeUntyped method to get the attachment details (and this can be used for further processing). Why are you performing HTTP callout (instead of performing DML directly on Attachment SObject)? – arut Sep 17 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.