• Shiva Chandel
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
Class :
@RestResource(urlMapping='/api/fetch_status/*')
global class RestDemo{

    @HttpGet
    global static Account getResult(){
        
        Account account;
        String accountId = '';
        RestRequest restReq = RestContext.request;
        RestResponse restRes = RestContext.response;
        
        // reading url
        try{
                //accountId = restReq.params.get('accountId');
                accountId = restReq.requestURI.substring(restReq.requestURI.lastIndexOf('/') + 1);        
                
                account = [SELECT Id, Name FROM Account WHERE Id = :accountId Limit 1];
                
                if(account != null){                                       //checked whether any record is returned or not
                        restRes.responseBody = Blob.valueOf(JSON.serialize(account));
                        restRes.statusCode = 200;
                        
                  }else{    
                       /* String account_not_found= '{"message":"Not Found"}';
                          restRes.responseBody = Blob.valueOf(account_not_found);  */ 
                          restRes.statusCode = 404; 
                    }                 
            }
        catch(Exception ex){
            restRes.responseBody = Blob.valueOf(ex.getMessage());
            restRes.statusCode = 500;
            }   
     return account;
    }
}

Test Class:
@isTest
private class RestDemoTest {

    @testSetup
    static void dataSetup() {
        Account acc = new Account(Name = 'Testing5');
        insert acc;
    }
    

    static testMethod void testGet() {
    //case 1 when the id is valid
        Account acc = [ SELECT Id FROM Account LIMIT 1 ];
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();             
        req.requestURI = '/services/apexrest/api/fetch_status/' + acc.Id;
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response= res;
        Account acct1 = RestDemo.getResult();
        system.assertEquals(acct1.Name, 'Testing5');
        
     // case 2 when the id is not present 
        String str_id = '0012x000004UjZX';
        Id id = Id.valueOf(str_id);
        req.requestURI = '/services/apexrest/api/fetch_status/' + id;
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response= res;
        Account acct2 = RestDemo.getResult();
        system.assertEquals(acct2, null); 
   
   // case 3 when the id is invalid 
        req.requestURI = '/services/apexrest/api/fetch_status/'+ 0125412548;
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response= res;
        Account acct3 = RestDemo.getResult();
        system.assertEquals(acct3, null);
      
        
    }
}

The thing is test case 2 and 3 are not working.
Hi,
So there is the scenario

I want to send the http get request from post man (it contains 2 strings in url to evaluate ) to Salesforce app and at the Salesforce end the request from postman is evaluated through http protocol and thn the 2 strings of the url are concatenated and send back as a response.

I don't know how to do this (specifically do it with http protocol).
Can you give an example for this ?
Class :
@RestResource(urlMapping='/api/fetch_status/*')
global class RestDemo{

    @HttpGet
    global static Account getResult(){
        
        Account account;
        String accountId = '';
        RestRequest restReq = RestContext.request;
        RestResponse restRes = RestContext.response;
        
        // reading url
        try{
                //accountId = restReq.params.get('accountId');
                accountId = restReq.requestURI.substring(restReq.requestURI.lastIndexOf('/') + 1);        
                
                account = [SELECT Id, Name FROM Account WHERE Id = :accountId Limit 1];
                
                if(account != null){                                       //checked whether any record is returned or not
                        restRes.responseBody = Blob.valueOf(JSON.serialize(account));
                        restRes.statusCode = 200;
                        
                  }else{    
                       /* String account_not_found= '{"message":"Not Found"}';
                          restRes.responseBody = Blob.valueOf(account_not_found);  */ 
                          restRes.statusCode = 404; 
                    }                 
            }
        catch(Exception ex){
            restRes.responseBody = Blob.valueOf(ex.getMessage());
            restRes.statusCode = 500;
            }   
     return account;
    }
}

Test Class:
@isTest
private class RestDemoTest {

    @testSetup
    static void dataSetup() {
        Account acc = new Account(Name = 'Testing5');
        insert acc;
    }
    

    static testMethod void testGet() {
    //case 1 when the id is valid
        Account acc = [ SELECT Id FROM Account LIMIT 1 ];
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();             
        req.requestURI = '/services/apexrest/api/fetch_status/' + acc.Id;
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response= res;
        Account acct1 = RestDemo.getResult();
        system.assertEquals(acct1.Name, 'Testing5');
        
     // case 2 when the id is not present 
        String str_id = '0012x000004UjZX';
        Id id = Id.valueOf(str_id);
        req.requestURI = '/services/apexrest/api/fetch_status/' + id;
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response= res;
        Account acct2 = RestDemo.getResult();
        system.assertEquals(acct2, null); 
   
   // case 3 when the id is invalid 
        req.requestURI = '/services/apexrest/api/fetch_status/'+ 0125412548;
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response= res;
        Account acct3 = RestDemo.getResult();
        system.assertEquals(acct3, null);
      
        
    }
}

The thing is test case 2 and 3 are not working.

Hi all,

I spotted 2 issues with Trailhead "Build a Suggestion Box App" badge.

The first one I managed to sort out but will post it here just in case someone gets into same issue:
- Step: "Modify the User Experience"
- Got error an while veryfing the challenge
- Managed to find (using Debug Log) that challenge verification process is looking for a "test" pick list value which isn't on the picklist challenge wants us to build
- This one was solved.
The second one I believe is a little bit trickier...
- Step: "Adding Business Logic"
- Create formula field "Number of Days Open" as requested
- Got error when verifying challenge: 
"Challenge Not yet complete... here's what's wrong: 
The 'Number of Days Open' custom formula field does not exist. Tip: check for typos in the field name.
"

But everything seems to be OK on configuration size...

Any ideas?

Thanks in advance for your support.
Pedro