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
Mikal HansonMikal Hanson 

Creating a Wrapper That Manages Two Objects

Hey there, everyone.

I've been stuck on a single assignment for a very long time, and it's no longer productive for me to struggle at it alone.

I'm trying to create a REST method that takes in two parameters (a temperature and a type(F or C)) and returns the appropriate converted temperature based on what is entered. As an example, if someone enters (32.0 F) as parameters, it should spit out (0.0), whereas if someone enters (100.0 C), you should get (212.0).

I know that I need to create a wrapper class to handle both data types and receive an output, but all posts that I've seen refer only to handling lists of objects, and I can't figure out how to convert that logic into what I need.

I would prefer detailed explanation so that I can see how the code truly works, as opposed to just getting the solution, as I feel like this topic would help a lot of people understand the construction of wrapper classes.
@RestResource(urlMapping='/RestTemp/v2/*')

global with sharing class RestTempV2 {
    @HttpPost
    global static Result PostTempv2(Decimal temp, String type){
        if(type =='F'){
        	Decimal cs = (temp - 32) * 5/9;      
        	Result r = new Result(String.valueOf(cs.setScale(2)));
        	return r;
        } else if(type =='C'){
        	Decimal fh = (temp * 9/5) + 32;
            Result r = new Result(String.valueOf(fh.setScale(2)));
            return r;
        } else if(type == null){
            Decimal cs = (temp - 32) * 5/9;      
        	Result r = new Result(String.valueOf(cs.setScale(2)));
        	return r;
        } else {
            system.debug('Please use a valid temperature type, either F or C.');
        }
    }
}
global class TempWrapper{
    global static Result PostF2Cv2(Decimal fh){        
        Decimal cs = (fh - 32) * 5/9;       
        Result r = new Result(String.valueOf(cs.setScale(2)));
        return r;
    }
    global static Result PostC2Fv2(Decimal cs){
        Decimal fh = (cs * 9/5) + 32;
        Result r = new Result(String.valueOf(fh.setScale(2)));
        return r;
    }    
    
    
}
I know my code is improper. This is why I'm asking for help.

Thank you all!