0

If i pass date i am able to execute first if condition but if parameter is not passed in URL from postman, i am getting Null pointer exception error.

RestRequest restReq = RestContext.request; 
           // Reading parametrs from URL
        dt = restReq.params.get('date');
           Date d = Date.parse(dt);          
 if(!String.isBlank(dt)){

            Record  =  [SELECT A,B,C,D WHERE  E >= :d];

           }

           if(String.isBlank(dt)){


                   Record  =  [SELECT A,B,C,D WHERE E >= :date.today()-7];

           }
1
  • are you sure that you are getting the error in this piece of code? It seems fine to me – Novarg Nov 20 '19 at 12:47
1

You can't pass null to Date.parse() method. So, you have to validate it before passing to the parse method.

RestRequest restReq = RestContext.request;
String dt = restReq.params.get('date');

if (String.isNotBlank(dt)) {
    Date d = Date.parse(dt);
    Record  =  [SELECT A,B,C,D WHERE  E >= :d];
    // OR
    Record  =  [SELECT A,B,C,D WHERE E >= :date.today()-7];
}
else {
    // handle validation
}
1
  • Great it working now. Thanks Raman, Cheers!! – videet boob Nov 21 '19 at 5:19

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.