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
Varsha D 9Varsha D 9 

on This Statement ....request.params.get('id') i m getting system.NullPointerException: Attempt to de-reference a null object

@HttpGet
     global static void sendMail(){
        RestRequest request = RestContext.request;
        String accountId = request.params.get('id');//error
        system.debug(accountId);
        Account objAccount = [SELECT Id,Name,(SELECT Id,Name FROM Contacts) FROM Account WHERE Id = :accountId LIMIT 1];
        
    
   // global static void sendMail(){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[]{'varsha.rcr37@gmail.com'}; 
 mail.setToAddresses(toAddresses);
     
//Contact con = [SELECT id, firstname FROM Contact LIMIT 1];
EmailTemplate et = [SELECT id FROM EmailTemplate WHERE developerName = 'gmail_salesforce'];
 

 
mail.setTargetObjectId(objAccount.id); 
mail.setTemplateId(et.id);
        
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
     }
}
Ashish Singh SFDCAshish Singh SFDC
Hi Varsha,

Can you double-check the id your supplying from workbench or Postman is not ID or Id? It's basically case-sensitive. 

How to avoid such error?

Even before grabbing a value from Map of Params, Transform all KeySet in lowercase in new Map and then grab value from this new Map by using Key.

Also, make sure to add null check-in to your web service.

Thanks,
Ashish Singh.
 
ravi soniravi soni
hi varsha,
you have to set your id before exacuting it.from following way you can set it.
RestContext.request = new RestRequest();
RestContext.request.params.put('id', acc.Id);
try it  and let me know if your problam is resolved.
avobe answer is only for this question.
Thank you
 
Varsha D 9Varsha D 9
hii veena ,
 
may i know how are you setting it i mean the acc.id in rest request
ravi soniravi soni
hy varsha,
are you testing it from test class?
if yes, then simply you have to apply following way.
@isTest
    public static void testHttpMethod(){
        account acc = new account();
        acc.name = 'test';
        insert acc;
        
        test.startTest();
        RestContext.request = new RestRequest();
RestContext.request.params.put('id', acc.Id);
    httpGetClassRef.sendMail();
        test.stopTest();
    }
let me know if it helps you.
Thank you