+ Start a Discussion
Reshmi SmijuReshmi Smiju 

Issues with Rest Resoucre.

Hi,
Below is my Rest Resource code to search  Opportunities based on particlar Stage Name. But, when I tried to search in Workbench, I am getting the below error. Actually for any url, the error is same.  Pls let me know, what is the issue with my code.

User-added image
User-added image

Below is my code :
@RestResource(urlMapping = '/Opp/Search/*')
global with sharing class SearchOppty{
    @HttpGet
    global static opp_Wrap searchopp(){
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        opp_Wrap response = new opp_Wrap();
        String text = req.requestURI.Substring(req.requestURI.lastIndexof('/')+1);
        if(doSearch(text))
        SearchOppByStageName(req,res,response);
        return response;
}
public static Boolean doSearch(String text){
    if(text == 'StageName')
        return true;
    else
        return false;
}
public static void SearchOppByStageName(RestRequest req, RestResponse res, Opp_Wrap response){
    String finaltxt = req.params.get('StageName');
    if(finaltxt == null && finaltxt == ''){
        response.Status = 'Error';
        response.Message ='Enter in correct format';
    }
    else{
        List<Opportunity> opps = [select Name, CloseDate, Amount, StageName from Opportunity
                    where StageName =:finaltxt];
        if(opps!= null && opps.size()>0){
            response.opp = opps;
            response.Status = 'Success';
            response.Message = opps.size() + ' Opportunity';
        }
        else{
            response.opp = null;
            response.Status = 'Error';
            response.Message = opps.size() + ' Opportunity';    
        }
    }
}
global class opp_Wrap{
   public List<Opportunity>opp;
    public String Status;
    public String Message;
  public opp_Wrap(){}
}   
}
Best Answer chosen by Reshmi Smiju
RAM AnisettiRAM Anisetti

Hi,

try to modifie your code like below and verify by below rest call 

/services/apexrest/Opp/Search?StageName=Qulification

 

@RestResource(urlMapping = '/Opp/Search')
global with sharing class SearchOppty{
    @HttpGet
    global static opp_Wrap searchopp(){
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        opp_Wrap response = new opp_Wrap();
       
        if(doSearch())
        SearchOppByStageName(req,res,response);
        return response;
}
public static Boolean doSearch(){

String havstage = req.params.get('StageName');
    if(havstage !=null && havstage != ''){
    
        return true;
		}
    else{
	return false;
	}
        
}
public static void SearchOppByStageName(RestRequest req, RestResponse res, Opp_Wrap response){
    String finaltxt = req.params.get('StageName');
    if(finaltxt == null && finaltxt == ''){
        response.Status = 'Error';
        response.Message ='Enter in correct format';
    }
    else{
        List<Opportunity> opps = [select Name, CloseDate, Amount, StageName from Opportunity
                    where StageName =:finaltxt];
        if(opps!= null && opps.size()>0){
            response.opp = opps;
            response.Status = 'Success';
            response.Message = opps.size() + ' Opportunity';
        }
        else{
            response.opp = null;
            response.Status = 'Error';
            response.Message = opps.size() + ' Opportunity';    
        }
    }
}
global class opp_Wrap{
   public List<Opportunity>opp;
    public String Status;
    public String Message;
  public opp_Wrap(){}
}   
}
 


 

All Answers

RAM AnisettiRAM Anisetti

Hi,

try to modifie your code like below and verify by below rest call 

/services/apexrest/Opp/Search?StageName=Qulification

 

@RestResource(urlMapping = '/Opp/Search')
global with sharing class SearchOppty{
    @HttpGet
    global static opp_Wrap searchopp(){
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        opp_Wrap response = new opp_Wrap();
       
        if(doSearch())
        SearchOppByStageName(req,res,response);
        return response;
}
public static Boolean doSearch(){

String havstage = req.params.get('StageName');
    if(havstage !=null && havstage != ''){
    
        return true;
		}
    else{
	return false;
	}
        
}
public static void SearchOppByStageName(RestRequest req, RestResponse res, Opp_Wrap response){
    String finaltxt = req.params.get('StageName');
    if(finaltxt == null && finaltxt == ''){
        response.Status = 'Error';
        response.Message ='Enter in correct format';
    }
    else{
        List<Opportunity> opps = [select Name, CloseDate, Amount, StageName from Opportunity
                    where StageName =:finaltxt];
        if(opps!= null && opps.size()>0){
            response.opp = opps;
            response.Status = 'Success';
            response.Message = opps.size() + ' Opportunity';
        }
        else{
            response.opp = null;
            response.Status = 'Error';
            response.Message = opps.size() + ' Opportunity';    
        }
    }
}
global class opp_Wrap{
   public List<Opportunity>opp;
    public String Status;
    public String Message;
  public opp_Wrap(){}
}   
}
 


 

This was selected as the best answer
Reshmi SmijuReshmi Smiju
Thank You for your prompt response :)  Regards.