1

I have written a Rest Get method where I get a response based on id like, /services/apexrest/invoices/a000X000015ABc2

How do I go about if I want to use this same method for following calls:

- GET <BASE_URL>/invoices/?customerId=<someId>&month=1
- GET <BASE_URL>/invoices/?customerId=<someId>&filter=shop&month=3
- GET <BASE_URL>/invoices/?customerId=<someId>&addressId=8212BJ154

Do I need to write if statements like if month!= null, if filter!= null and use this in WHERE Id = :invoiceId or month__c = month or is there a general pattern for this

Get method:

@HttpGet
    global static void getinvoiceById() {
        RestRequest request = RestContext.request;
        // grab the invoiceId from the end of the URL
        String invoiceId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
        List<Invoice__c> result =  [SELECT customerId__c , addressId__c , invoiceId__c , invoiceType__c , invoiceTypeLocalized__c  , invoiceDate__c  ,
                                    paymentDueDate__c , invoiceNumber__c  , startDate__c , endDate__c , periodDescription__c , amount__c , 
                                    vatAmount__c , totalAmount__c 
                                    FROM invoice__c
                                    WHERE Id = :invoiceId];

...
3

You may build a dynamic query using if --- else if --- else, depending on the parameters in your URI.

The structure will be something like this:

@HttpGet
global static void getinvoiceById() {
    RestRequest request = RestContext.request;
    Map<String, String> reqParamsMap = request.params;

    String strQuery = 'SELECT customerId__c , addressId__c , invoiceId__c , invoiceType__c , invoiceTypeLocalized__c  , invoiceDate__c  ,
                                paymentDueDate__c , invoiceNumber__c  , startDate__c , endDate__c , periodDescription__c , amount__c , 
                                vatAmount__c , totalAmount__c 
                                FROM invoice__c WHERE ';

    try {
        if(reqParamsMap.containsKey('customerId')){              
            String invId = reqParamsMap.get('customerId');
            strQuery += ' Id =\''+invId+'\'';

        }
        else if (reqParamsMap.containsKey('month')){
            String month = reqParamsMap.get('month');
            strQuery += ' month = .......';
        }
        else if (other condition) {
            //append condition to query
        }

        //be careful with WHERE / AND / OR conditions
        //debug your query to see if it's well built before executing it
        System.debug('## query : ' + strQuery); 
        List<Invoice__c> result = database.Query(strQuery);

    }
    catch(Exception ex){
        //catch errors
    }

}

You may amend the code accordingly.

  • I get an error on String invId = reqParamsMap.containsKey('customerId'); should this be keyvalue? – Thomas Jun 23 '18 at 13:13
  • Debug reqParamsMap and see what you it contains. – N.. Jun 23 '18 at 13:22
  • I cant save the class because of this error : Illegal assignment from Boolean to String on the mentioned line – Thomas Jun 23 '18 at 13:45
  • 1
    @N.B. Figured out this can be done more simple by: String customerId = RestContext.request.params.get('customerId'); String month = RestContext.request.params.get('month'); – Thomas Jun 23 '18 at 15:21
  • 1
    @N.B. String month = reqParamsMap.containsKey('month'); should be String month = reqParamsMap.get('month'); Thanks for your help. – Thomas Jun 23 '18 at 16:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.