+ Start a Discussion
Ivar@MarelIvar@Marel 

Writing a REST web service that creates new record and attachments from form on company website

Hi all.

I have been reading around all kinds of documentation and posts and haven't been able to wrap my head around what I need to accomplish my goal. What I am trying to achieve is to write a custom Apex REST web service that allows an the invoking application to create a new custom object record, and at the same time submit a list of file attachments to be latched onto the newly created record.

I have written a few services that create a new record from a REST post, so that isn't the hindrance, but rather the part where I try to include file attachments. My initial assumption was to just include a List<Blob> in the definition of parameters, but that doesn't seem to be allowed in a http service.

Any nudges in the right direction would we wildly appreciated :)

A stripped down sample of my current code below:
@RestResource(urlMapping='/WarrantyClaim/*')
global with sharing class WS_WarrantyClaims{

  global class ParamData{
    public String strWONumber;
    public String strCompanyName;
    public String strCompanyAddress;
    
    public String strFailureDescription;
    public Date dDeliveryDate;
    public Integer iProductionHours;

    
    public List<Blob> attachments;
  }
  
  
  global class ReturnData{
      String strStatus;
      String strErrorMessage;
      String strClaimId;
  }
    
  @HttpPost
  global static ReturnData doPost(ParamData params){
    ReturnData retData = new ReturnData();
    
    
    WarrantyClaim__c wc = new WarrantyClaim__c();
    wc.ltClaimDescription__c = params.strFailureDescription;
    
    try{
        insert wc;
        retData.strStatus = 'Success';
        retData.strClaimId = wc.Id;
    }
    catch( Exception e ){
        retData.strStatus = 'Error';
        retData.strErrorMessage = e.getMessage();
    }
    
    return retData;
  }
 }