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
Bilal 25Bilal 25 

REST API - Change set Error

Class
 
@RestResource(urlMapping='/referralplatformopportunity/*')
global with sharing class ReferralOpportunity {
  
    @HttpGet
    global static Opportunity doGet() {
        
        RestRequest request = RestContext.request;
        String oppId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
       /* opportunity result = [SELECT name,stagename,New_Referral_Reward__c,Lead_Id__c,Account_Type__c,Payment_Journal_Reference_Date__c,Product_Family__c,Submission_Opportunity__c,Approval_Opportunity__c,Commission_Opportunity__c FROM opportunity WHERE Lead_Id__c = :oppId LIMIT 1];
        if(result != null)
            return result; */
        Opportunity lateAccount = null;
        List<opportunity> results = [SELECT name,stagename,New_Referral_Reward__c,Lead_Id__c,Account_Type__c,Payment_Journal_Reference_Date__c,Product_Family__c,Submission_Opportunity__c,Approval_Opportunity__c,Commission_Opportunity__c FROM opportunity WHERE Lead_Id__c = :oppId LIMIT 1];
	   if (results.size() > 0)
       {lateAccount = results.get(0);}
        return lateAccount;
              
    }
    
   @HttpPut
   global static String doPut(String accountname, String accountnumber, string bankname,string swiftcode, string iban,string bankcity,string bankbranch,string bankstreet,string bankcountry) {
   RestRequest request = RestContext.request;
   String oppId = request.requestURI.substring(request.requestURI.lastIndexOf('/')+1);
   	   Opportunity thisopportunity = [ Select id,name,Lead_Id__c from opportunity where Lead_Id__c =:oppId];
       thisopportunity.Bank_Account__c = accountname;
       thisopportunity.Account_Number__c = accountnumber;
       thisopportunity.Bank_Name__c = bankname;
       thisopportunity.SWIFT_Code__c = swiftcode;
       thisopportunity.IBAN__c = iban;
       thisopportunity.Bank_City__c = bankcity;
       thisopportunity.Bank_s_Branch__c = bankbranch;
       thisopportunity.Bank_Street__c = bankstreet;
       thisopportunity.Bank_Country__c = bankcountry;
       update thisopportunity;
       return 'Banking details updated';
    }  
}

Test Class
 
@isTest
private class ReferralOpportunityTest {

    @testSetup
    static void dataSetup() {
        
        Account acc = new Account();
        acc.name = 'Bilal Account';
        acc.type = 'prospect';
        insert acc;
        
        Opportunity opp = new Opportunity();
		opp.Name = 'Bilal-Opp';
        opp.CloseDate= System.Today()+30;
        opp.StageName='Engaged';
        opp.AccountId = acc.id;
        insert opp;
        
        
    }

    static testMethod void testGet() {
        Opportunity opp = [ SELECT Id,name FROM Opportunity WHERE Name = 'Bilal-Opp' LIMIT 1 ];
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();             
        req.requestURI = '/services/apexrest/referralplatformopportunity/' + opp.Id;
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response= res;
        Opportunity oppResp = ReferralOpportunity.doGet();
        system.assertEquals(oppResp.Name, 'Bilal-Opp');
    }
    
    static testMethod void testPut() {
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();             
        req.requestURI = '/services/apexrest/referralplatformopportunity/';
        req.httpMethod = 'PUT';
        RestContext.request = req;
        RestContext.response= res;
        String oppId = ReferralOpportunity.doPut('Mohamed Bilal', 'ABC12567890', 'ABC Bank', 'GH789JA89','ABC780688','Dubai','Downtown','Sheik Zayed St','UAE');
        Opportunity opp = [ SELECT Id, Bank_Account__c, Account_Number__c FROM Opportunity WHERE Id =: oppId ];
        
        system.assertEquals(opp.Bank_Account__c, 'Mohamed Bilal');
        system.assertEquals(opp.Bank_Name__c, 'ABC Bank');
        
        Opportunity opp1 = new Opportunity();
        opp1.New_Referral_Reward__c = 3000;
        opp1.Lead_Id__c = '00Q0C000002m5pmUAA';
        opp1.Bank_Account__c = 'Mohamed Bilal';
        opp1.Account_Number__c = 'ABC12567890';
        opp1.Bank_Name__c = 'ABC Bank';
        opp1.SWIFT_Code__c = 'GH789JA89';
        opp1.IBAN__c = 'ABC780688';
        opp1.Bank_City__c = 'Dubai';
        opp1.Bank_s_Branch__c = 'Downtown';
        update opp1;
    }
    

}

This class passed in sandbox with 95 percentage. But when moved to production shows below error. Please help!!!!


ReferralOpportunityTesttestGetSystem.NullPointerException: Attempt to de-reference a null object
Stack Trace: Class.ReferralOpportunityTest.testGet: line 31, column 1
ReferralOpportunityTesttestPutSystem.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.ReferralOpportunityTest.testPut: line 42, column 1


Thanks in Advance!!!
SwethaSwetha (Salesforce Developers) 
HI Mohamed ,
Recommend you to check https://help.salesforce.com/articleView?id=000335222&type=1&mode=1

Also, the approaches mentioned in https://salesforce.stackexchange.com/questions/76647/code-coverage-of-93-in-sandbox-but-only-61-in-production-wont-deploy/76650 are worth considering

From your code, I see that you are not running the tests querying org's data(i.e., not using seealldata).Are you running tests in sandbox & deploying with different users/profile?

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
Bilal 25Bilal 25
Thanks Swetha for your quick response. 

I deployed with same user. Is there any problem in the code now when i run the test class again in the sandbox it shows below error.
 
Time Started	05/11/2020 10:00
Class	ReferralOpportunityTest
Method Name	testGet
Pass/Fail	Fail
Error Message	System.NullPointerException: Attempt to de-reference a null object
Stack Trace	Class.ReferralOpportunityTest.testGet: line 32, column 1

How to resove this?