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
bharath kuppalabharath kuppala 

can we provide test class these code

@RestResource(urlMapping='/ContactCRDID/*')
global with sharing class Ebuy_API
{
  @HttpGet
   global static List<Opportunity> doGet()
   {
      RestRequest request = RestContext.request;
        Integer ConCRD= Integer.valueof(request.requestURI.substring(request.requestURI.lastIndexOf('/')+1));
        List<Opportunity> result =  [Select StageName, PrimaryBPCRDID__c, Opportunnity_Source__c, OpportunityNumber__c, Name, 
                                     LastModifiedDate, Id, CreatedDate, Amount, Account.Name, CloseDate From Opportunity  
                                     where PrimaryBPCRDID__c =: ConCRD AND (((NOT StageName  LIKE '9-Closed%') AND 
                                     (NOT StageName  LIKE '8-Closed%')) OR (StageName  LIKE '8-Closed%' AND 
                                     CloseDate > LAST_N_MONTHS:1 )) ];  
       
       return result;
   }
 
}
Best Answer chosen by bharath kuppala
v varaprasadv varaprasad
Hi Bharath,

Please check once below sample code and add all-fields in the account and opportunity records.
 
@isTest
public class restTest{

public static testmethod void testService(){
 RestRequest req = new RestRequest();
 RestResponse res = new RestResponse();

req.requestURI = '/services/apexrest/ContactCRDID';  //Request URL
req.httpMethod = 'GET';//HTTP Request Type
RestContext.request = req;
RestContext.response= res;

//Insert Accountdata

Account acc = new account();
acc.name = 'test';
//add remain fields here
insert acc;

opportunity op = new opportunity();
op.name = 'testopp';
//add remaing fields
opp.accountid = acc.id;
insert op;


List<Opportunity> opps = Ebuy_API.doGet();

}
}


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1




 

All Answers

v varaprasadv varaprasad
Hi Bharath,

Please check once below sample code and add all-fields in the account and opportunity records.
 
@isTest
public class restTest{

public static testmethod void testService(){
 RestRequest req = new RestRequest();
 RestResponse res = new RestResponse();

req.requestURI = '/services/apexrest/ContactCRDID';  //Request URL
req.httpMethod = 'GET';//HTTP Request Type
RestContext.request = req;
RestContext.response= res;

//Insert Accountdata

Account acc = new account();
acc.name = 'test';
//add remain fields here
insert acc;

opportunity op = new opportunity();
op.name = 'testopp';
//add remaing fields
opp.accountid = acc.id;
insert op;


List<Opportunity> opps = Ebuy_API.doGet();

}
}


Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1




 
This was selected as the best answer
Raj VakatiRaj Vakati
try this
@IsTest
private class Ebuy_APITEst {


@isTest
private static void Ebuy_APITest() {
 Test.startTest();
 Opportunity o = new Opportunity();
 o.Name='Test';
 //add other fields
 
 insert o ;
 
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/ContactCRDID'; 
        req.params.put('ConCRD', '9876543210');
        req.httpMethod = 'GET';
        req.addHeader('Content-Type', 'application/json'); 
        RestContext.request = req;
        RestContext.response = res;
      Test.stopTest(); 
}
}

 
bharath kuppalabharath kuppala
above code didnot excuted
can plz provide 
v varaprasadv varaprasad
@IsTest
private class Ebuy_APITEst {


@isTest
private static void Ebuy_APITest() {
 Test.startTest();
 Opportunity o = new Opportunity();
 o.Name='Test';
 //add other fields
 o.PrimaryBPCRDID__c = '9876543210';
 o.closedate = System.Today().adddays(-40);
 
 insert o ;
 
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/ContactCRDID'; 
        req.params.put('ConCRD', '9876543210');
        req.httpMethod = 'GET';
        req.addHeader('Content-Type', 'application/json'); 
        RestContext.request = req;
        RestContext.response = res;
		
		List<Opportunity> opps = Ebuy_API.doGet();

      Test.stopTest(); 
}
}

 
Raj VakatiRaj Vakati
@IsTest
private class Ebuy_APITEst {


@isTest
private static void Ebuy_APITest() {
 Test.startTest();
 Opportunity o = new Opportunity();
 o.Name='Test';
 //add other fields
 o.PrimaryBPCRDID__c = '9876543210';
 o.closedate = System.Today().adddays(-40);
 o.StageName='Prospecting';
 insert o ;
 
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/ContactCRDID'; 
        req.params.put('ConCRD', '9876543210');
        req.httpMethod = 'GET';
        req.addHeader('Content-Type', 'application/json'); 
        RestContext.request = req;
        RestContext.response = res;
		
		List<Opportunity> opps = Ebuy_API.doGet();

      Test.stopTest(); 
}
}

 
bharath kuppalabharath kuppala
@IsTest
private class Ebuy_APITest {
    
@isTest

private static void Ebuy_APITest() {

 Test.startTest();
    
    Account acc = new Account();
    acc.Name = 'Japara Healthcare';
    acc.Street1__c = '1 main street';
    acc.ZipCode__c = '75001';
    acc.Country__c = 'france';
    acc.City__c = 'paris';
    acc.Verticals__c ='unknown';
    acc.AccountClassification__c = 'unknown';
    acc.Type = 'prospect';
    acc.CurrencyIsoCode = 'USD';
        
     insert acc;


 Opportunity opt = new Opportunity();

 opt.Name ='Test2';
 opt.CurrencyIsoCode = 'USD';
 opt.Account.Name = acc.Name;
 opt.CloseDate = '15/2/2018'; // its a mandatory field i am getting the error :: Illegal assignment from String to Date//
 opt.StageName = '3-Solution Positionning';
 opt.ALUProbability__c ='0';
    
 insert opt ;

  
        RestRequest req = new RestRequest();

        RestResponse res = new RestResponse();

        req.requestURI = '/services/apexrest/ContactCRDID';

        req.params.put('ConCRD', '1346');

        req.httpMethod = 'GET';

        req.addHeader('Content-Type', 'application/json');
    
        RestContext.request = req;

        RestContext.response = res;
    
     List<Opportunity> opps = Ebuy_API.doGet();


      Test.stopTest();

}