+ Start a Discussion
chaitanya babu edigachaitanya babu ediga 

REST bulk delete

Hi,

I'm novice to coding and explicitely REST. Is there a way to delete a list of records using REST API. I see bulk insert and update is possible. But there is no proper info on bulk delete. Any help would be appreciated.

Thanks in Advance!
Best Answer chosen by chaitanya babu ediga
HARSHIL U PARIKHHARSHIL U PARIKH
You can also accomplish this using batch apex. Try using below code.
 
Public class QuoteLineItemDelete_BatchApex Implements Database.Batchable<SObject>{
	
    String ComingQuery;
    
    // Constructor
    Public QuoteLineItemDelete_BatchApex(String Q){
        ComingQuery = Q;
    }
        
    // Start Method
    Public Database.QueryLocator Start(Database.BatchableContext dBC){
        return Database.getQueryLocator('ComingQuery');
    }
    
    // Execute Methode
    Public Void Execute(Database.BatchableContext dBC, List<Quote> scope){
        
        List<ID> quoteIds = New List<ID>();
        
        For(Quote qt: scope){
            quoteIds.add(qt.ID);
        }
        
        List<QuoteLineItem> DelQLI= [SELECT Id, LineNumber from QuoteLineItem WHERE QuoteId =: quoteIds];
        
        If(!DelQLI.isEmpty())
        {
            delete DelQLI;
        }
    }
    
    // Finish Method
    Public Void Finish(Database.BatchableContext dBC){
        // finish logic.
    }
    
}
Execute batch via Execute Anonymous Window in Developer Console,
 
String queryStr = 'Select ID FROM Quote';

Id ApexBatchID = Database.executeBatch(New QuoteLineItemDelete_BatchApex(queryStr));
You may or may not find some syntex error or would have to change few minor things in above code targeting your own specific requirements, but this can be one of the approach. It's deletes in batches...

Hope this helps!


 

All Answers

chaitanya babu edigachaitanya babu ediga
Here is the code I tried but it returning null in place of Id:

@RestResource(urlMapping='/QLIDelete/*')
global with sharing class RESTQLIDelCon {
@HttpDelete
  global static String deleteQLI() {
    Id QuoteId = RestContext.request.params.get('Id');
    //System.debug('RestContext.request.params'+ RestContext);
    System.debug('Id======>' + QuoteId);
    Quote DelQuote = [ Select ID from Quote where Id = :QuoteId];
     
    List<QuoteLineItem> DelQLI= [SELECT Id, LineNumber from QuoteLineItem WHERE QuoteId =: DelQuote.Id];
    
    if(!DelQLI.isEmpty()){
    delete DelQLI;
    return 'Quote Line Items are deleted';
    }
    else{
    return 'Quote Line Items are not deleted due to some issues';
    }
  }
}
HARSHIL U PARIKHHARSHIL U PARIKH
You can also accomplish this using batch apex. Try using below code.
 
Public class QuoteLineItemDelete_BatchApex Implements Database.Batchable<SObject>{
	
    String ComingQuery;
    
    // Constructor
    Public QuoteLineItemDelete_BatchApex(String Q){
        ComingQuery = Q;
    }
        
    // Start Method
    Public Database.QueryLocator Start(Database.BatchableContext dBC){
        return Database.getQueryLocator('ComingQuery');
    }
    
    // Execute Methode
    Public Void Execute(Database.BatchableContext dBC, List<Quote> scope){
        
        List<ID> quoteIds = New List<ID>();
        
        For(Quote qt: scope){
            quoteIds.add(qt.ID);
        }
        
        List<QuoteLineItem> DelQLI= [SELECT Id, LineNumber from QuoteLineItem WHERE QuoteId =: quoteIds];
        
        If(!DelQLI.isEmpty())
        {
            delete DelQLI;
        }
    }
    
    // Finish Method
    Public Void Finish(Database.BatchableContext dBC){
        // finish logic.
    }
    
}
Execute batch via Execute Anonymous Window in Developer Console,
 
String queryStr = 'Select ID FROM Quote';

Id ApexBatchID = Database.executeBatch(New QuoteLineItemDelete_BatchApex(queryStr));
You may or may not find some syntex error or would have to change few minor things in above code targeting your own specific requirements, but this can be one of the approach. It's deletes in batches...

Hope this helps!


 
This was selected as the best answer
chaitanya babu edigachaitanya babu ediga
I resolved it with below code, but using Batch Apex is also a good idea.

@RestResource(urlMapping='/QLIDelete/*')
global with sharing class RESTQLIDelCon {
@HttpDelete
  global static String deleteQLI() {
    RestRequest req = RestContext.request;
    RestResponse res = RestContext.response;
    String QuoteId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1); 
    //Id QuoteId = RestContext.request.params.get('Id');
    System.debug('Id======>' + QuoteId);
    Quote DelQuote = New Quote();
    try{
        DelQuote = [ Select ID from Quote where PR_Number__c = :QuoteId];
         
        List<QuoteLineItem> DelQLI= new List<QuoteLineItem>();
        if(DelQuote!=NULL)
            DelQLI = [SELECT Id, LineNumber from QuoteLineItem WHERE QuoteId =: DelQuote.Id];
            delete DelQLI;
            return 'Quote Line Items are deleted';
    }catch (exception e) {
        res.responseBody = Blob.valueOf(e.getMessage());
        res.statusCode=400;
        return 'Error';
    }
  }
}