function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rahul Kumar DeyRahul Kumar Dey 

How to convert synchronous apex rest api web-service to Queueable interface (Asynchronous)?

Hi guys, 
I want to convert and REST API web-service synchronous to queable-
Here is the code which is perfectly worked for upload attachment upto 5.5 MB 64 encoded file. And I see in the document we can upload upto 12MB if this is in asynchronous in nature. 
 
@RestResource(urlMapping='/insertCaseWithAttachmentRestService/*')
global with sharing class insertCaseWithAttachmentRestAPI {
    //In this method we insert case with multiple attached files
   @httpPost
    global static Id insertCase(caseRequest req){
        Case caseObj = new Case();
        caseObj.Product__c = req.pdt;
        caseObj.Type = req.type;
        caseObj.Status = req.status;
        caseObj.Origin = req.origin;
        caseObj.Subject = req.subject;
        insert caseObj;
        system.debug('CaseId->>>>'+caseObj.Id);
        
        List<Attachment> attList = new List<Attachment>();
      
        for(attachmentRequest att : req.attList){
            attList.add(new Attachment(Name=att.attachmentName, 
                                      Body=EncodingUtil.base64Decode(att.blobString),
                                      ParentId=caseObj.Id
                                      ));
        }
        insert attList;
        
        return caseObj.Id;  
    }
    //Wrapper class
    global class caseRequest{
        String pdt {get; set;}
        String type {get; set;}
        String status {get; set;}
        String origin {get; set;}
        String subject {get; set;}
        
        List<attachmentRequest> attList {get; set;}
    }
    global class attachmentRequest{
        String attachmentName {get; set;}
        String blobString {get; set;}
    }
}

Any help to convert this into queueable interface?

Thanks in advance
ShirishaShirisha (Salesforce Developers) 
Hi Rahul,

Greetings!

In order to make the upload process as an asynchronous then you will have to implements the Queueable which enables you to add jobs to the queue and monitor them.

Please find the sample code along with the sample test class code:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Rahul Kumar DeyRahul Kumar Dey
@Shirisha, any help based on my class, not able to execute this....
EranVEranV

Does your implementation require response with the newly created case ID to remote system?
If it doesn't, I'd split the functionality into two different classes and do something like this:

 

@RestResource(urlMapping='/insertCaseWithAttachmentRestService/*')
global with sharing class insertCaseWithAttachmentRestAPI {
    //In this method we insert case with multiple attached files
   @httpPost
    global static void insertCase(caseRequest req){
      
        ReqToCaseService rcs = new ReqToCaseService(req);
      
        System.enqueueJob(rcs);
    }
}


public with sharing class ReqToCaseService implements Queueable
{
  private caseReqeust req;
  
  public ReqToCaseService(caseRequest req){
    this.req = req;
  }
  
  public void execute(QueueableContext qc){
    convertReqToCase();
  }

  public static Id convertReqToCase(){
        Case caseObj = new Case();
        caseObj.Product__c = req.pdt;
        caseObj.Type = req.type;
        caseObj.Status = req.status;
        caseObj.Origin = req.origin;
        caseObj.Subject = req.subject;
        insert caseObj;
        
        List<Attachment> attList = new List<Attachment>();
      
        for(attachmentRequest att : req.attList){
            attList.add(new Attachment(Name=att.attachmentName, 
                                      Body=EncodingUtil.base64Decode(att.blobString),
                                      ParentId=caseObj.Id
                                      ));
        }
        insert attList;
        
        return caseObj.Id;  
  }
  
  //Wrapper class
  public class caseRequest{
        String pdt {get; set;}
        String type {get; set;}
        String status {get; set;}
        String origin {get; set;}
        String subject {get; set;}
        
        List<attachmentRequest> attList {get; set;}
  }
  
  public class attachmentRequest{
        String attachmentName {get; set;}
        String blobString {get; set;}
  }
  
  
}
Rahul Kumar DeyRahul Kumar Dey
@EranV, first of all i need to send response with the newly created caseId, which I implemented as well. But the problem is, do you remember we have limitation to store maximum 6000000 alphanumeric characer in String. So, whenever I try to insert more than 6 MB 64 encoded file, its showing me "String limit exception" in my case String is "blobString".
Do you have any alternate solution?  
Fast Cloud ConsultingFast Cloud Consulting
Rahul , 

¿Did you find any solution about this issue?

I would appreciate if you answer me.

Greetings.
Rahul Kumar DeyRahul Kumar Dey
@FastCloudConsulting, 

We couldn't achieve that using any of the apex solutions including synchronous & asynchronous because of the maximum payload is 6 MB/ 6000000 alphanumeric characters (including record size and attachments).
So our solution approach was, we no more store any attachments in Salesforce instead we stored those to AWS, and in the payload we requesting for the URL of AWS to create records with attachments into Salesforce.

Hope this helps!

Thanks,
Rahul