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
Sahil YadavSahil Yadav 

Attempt to deference a Null Value Issue

Hello Community Developers ,
I am working on one rest api service class which  will accept  payload from external system and send it to our salesforce and will created data in Salesforce.

 I am getting this issue while running my test classes.
Apex Class :-


@RestResource (urlMapping = '/createContact/*')
global with sharing class communityUsersWebService {
 @HttpPost
    global static ResponseWrapper createContacts(){
        Boolean success = false;
        String message = '';
        Set<Id> accountIds = new Set<Id>();
        List<Contact> newContactRecords = new List<Contact>();
        RestRequest request = RestContext.request;
        RestResponse response = RestContext.response;
        if(request != null){
            System.debug('request is not equal to null');
            Blob body = request.requestBody;
            String requestBody = body.toString();
            if(requestBody != null){
                try{
                    Map<String, Object> deserializeRequestBody = (Map<String , Object>)JSON.deserializeUntyped(requestBody);
                    System.debug(' deserializeRequestBody Map :-' +deserializeRequestBody);
                    Map<String , Object> storedeserializeRequestBody = new Map< String , Object>();
                    for(String s : deserializeRequestBody.keySet()){
                        storedeserializeRequestBody.put(s , deserializeRequestBody.get(s));
                        System.debug(' storedeserializeRequestBody Map :-' +storedeserializeRequestBody);
                    }
                    if(!storedeserializeRequestBody.isEmpty()){
                        String DDI_Industry_c = String.valueOf(storedeserializeRequestBody.get('DDI_Industry__c'));
                        String RecordTypeId =  String.valueOf(storedeserializeRequestBody.get('RecordTypeId'));
                        String Firstname =  String.valueOf(storedeserializeRequestBody.get('Firstname'));
                        String LastName = String.valueOf(storedeserializeRequestBody.get('Lastname'));
                        String Email = String.valueOf(storedeserializeRequestBody.get('Email'));
                        String DDI_Contact_Role_c =  String.valueOf(storedeserializeRequestBody.get('DDI_Contact_Role__c'));
                        String DDI_Decision_role_c =  String.valueOf(storedeserializeRequestBody.get('DDI_Decision_role__c'));
                        String Contact_Type_c =  String.valueOf(storedeserializeRequestBody.get('Contact_Type__c'));
                        String Account_Name_c  = String.valueOf(storedeserializeRequestBody.get('Account_Name__c'));
                        
                        List<Contact> getContacts = [Select id , FirstName , LastName , Email , RecordTypeId from Contact 
                                                     Where RecordTypeId =: RecordTypeId
                                                     AND FirstName =: Firstname
                                                     AND LastName =: LastName];
                        if(getContacts != null && getContacts.size() > 0){
                            System.debug('You Cannot Create Contacts due to Duplicacy !!!');
                            success = true;
                            message  = 'Contact creation is not Proceesed Succesfully since Duplicates Contacts in Salesforce ';
         
                               
                            
                        }
                        else{
                            List<Account> getAccounts = [Select id ,  Name   from Account Where Name=: Account_Name_c Limit 1];
                            if(getAccounts != null && getAccounts.size() > 0){
                                for(Account account : getAccounts){
                                    accountIds.add(account.Id);
                                    
                                }
                                
                            }
                            if(accountIds != null && accountIds.size() == 1 ){
                                Id mapToAccountId ;
                                for(Id id : accountIds){
                                    mapToAccountId = id;
                                }
                                Contact newContact = new Contact();
                                newContact.DDI_Industry__c = DDI_Industry_c;
                                newContact.RecordTypeId = RecordTypeId;
                                newContact.FirstName = Firstname;
                                newContact.LastName = Lastname;
                                newContact.Email = Email;
                                newContact.DDI_Contact_Role__c = DDI_Contact_Role_c;
                                newContact.DDI_Decision_role__c = DDI_Decision_role_c;
                                newContact.Contact_Type__c = Contact_Type_c;
                                newContact.AccountId = mapToAccountId;
                                //newContact.Account_Name__c = Account_Name_c;
                                newContactRecords.add(newContact);
                                
                            }
                            if(newContactRecords != null && newContactRecords.size() > 0){
                                insert newContactRecords;
                                success = true;
                                message =  'Contact creation request is processed Succcessfully !!!!';
                               
                            }
                            
                            
                        }
                        
                        
                        
                        
                    }
                    
                    
                }
                Catch(Exception ex){
                    System.debug('The Exception is : '+ex.getMessage());
                    success = true;
                    message = ex.getMessage();
                    
                }
            }
        }
        
        ResponseWrapper responseWrapper = new ResponseWrapper();
        responseWrapper.message = message ;
        responseWrapper.success = success;
        return responseWrapper;
        
    }
    
    
    global class ResponseWrapper{
           global String message ; 
           global Boolean success;
    }


}
 
Test Class :-


@isTest
public with sharing class communityUsersWebServiceTest {
    
    @isTest
    public static void testContactCreation(){
        Account account = new Account();
        account.name = 'Test Account';
        Id RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('DDI').getRecordTypeId();
        account.RecordTypeId = RecordTypeId;
        insert account;
        Contact contact = new Contact();
        contact.FirstName = 'Raghav';
        contact.LastName = 'Thakur';
        contact.Email = 'raghav.thakue+33@gmail.com';
        Id contactRecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('DDI').getRecordTypeId();
        contact.RecordTypeId = contactRecordTypeId;
        contact.AccountId = account.Id;
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        req.requestURI = '/services/apexrest/subscriptionUpdate/';
        String jsonMessage = '{"DDI_Industry": "Other", "RecordTypeId": "0123g000000kDbrAAE", "Firstname": "Raghav", "Lastname": "Thakur", "Email": "raghav.thakue+33@gmail.com","DDI_Contact_Role__c": "Point of Contact","DDI_Decision_role__c": "Other", "Contact_Type__c": "Partner", "Account_Name__c": "'+account.Name+'" }';
        if(jsonMessage != null){
            req.requestBody = Blob.valueOf(jsonMessage);
            req.httpMethod = 'POST';
            RestContext.request = req;
            RestContext.response = res;
           
            
        }
        
         communityUsersWebService.createContacts();
        
        
        
    }

}

In my test class the line which i made as bold showing an issue !!

Any Help really thankfull !!!!
 
Best Answer chosen by Sahil Yadav
Arawind_MundankarArawind_Mundankar
Hello Sahil

please remove below code
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
req.requestURI = '/services/apexrest/subscriptionUpdate/';
String jsonMessage = '{"DDI_Industry": "Other", "RecordTypeId": "0123g000000kDbrAAE", "Firstname": "Raghav", "Lastname": "Thakur", "Email": "raghav.thakue+33@gmail.com","DDI_Contact_Role__c": "Point of Contact","DDI_Decision_role__c": "Other", "Contact_Type__c": "Partner", "Account_Name__c": "'+account.Name+'" }';
if(jsonMessage != null){
req.requestBody = Blob.valueOf(jsonMessage);
req.httpMethod = 'POST';
RestContext.request = req;
RestContext.response = res;

Updated code
String jsonMessage = '{"DDI_Industry": "Other", "RecordTypeId": "0123g000000kDbrAAE", "Firstname": "Raghav", "Lastname": "Thakur", "Email": "raghav.thakue+33@gmail.com","DDI_Contact_Role__c": "Point of Contact","DDI_Decision_role__c": "Other", "Contact_Type__c": "Partner", "Account_Name__c": "'+account.Name+'" }';
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI ='/services/apexrest/subscriptionUpdate/';
req.httpMethod = 'POST';
req.requestBody = Blob.valueof(jsonMessage);
RestContext.request = req;
RestContext.response= res;


If this helps you mark as best answer.

Thanks,
Arawind Mundankar
 

All Answers

Arawind_MundankarArawind_Mundankar
Hello Sahil

please remove below code
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
req.requestURI = '/services/apexrest/subscriptionUpdate/';
String jsonMessage = '{"DDI_Industry": "Other", "RecordTypeId": "0123g000000kDbrAAE", "Firstname": "Raghav", "Lastname": "Thakur", "Email": "raghav.thakue+33@gmail.com","DDI_Contact_Role__c": "Point of Contact","DDI_Decision_role__c": "Other", "Contact_Type__c": "Partner", "Account_Name__c": "'+account.Name+'" }';
if(jsonMessage != null){
req.requestBody = Blob.valueOf(jsonMessage);
req.httpMethod = 'POST';
RestContext.request = req;
RestContext.response = res;

Updated code
String jsonMessage = '{"DDI_Industry": "Other", "RecordTypeId": "0123g000000kDbrAAE", "Firstname": "Raghav", "Lastname": "Thakur", "Email": "raghav.thakue+33@gmail.com","DDI_Contact_Role__c": "Point of Contact","DDI_Decision_role__c": "Other", "Contact_Type__c": "Partner", "Account_Name__c": "'+account.Name+'" }';
RestRequest req = new RestRequest();
RestResponse res = new RestResponse();
req.requestURI ='/services/apexrest/subscriptionUpdate/';
req.httpMethod = 'POST';
req.requestBody = Blob.valueof(jsonMessage);
RestContext.request = req;
RestContext.response= res;


If this helps you mark as best answer.

Thanks,
Arawind Mundankar
 
This was selected as the best answer
Sahil YadavSahil Yadav
Hello Arawind , 
Thank you for your inputs even later I could also able to get mine mistake which I did previously . We need to prepare  the request were we need to create an instance first for the request. Once again Thank you so much !!!