Skip to content

Instantly share code, notes, and snippets.

@ChuckJonas
Last active September 4, 2017 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChuckJonas/45fddd9fd248833d8f43fa520027ffda to your computer and use it in GitHub Desktop.
Save ChuckJonas/45fddd9fd248833d8f43fa520027ffda to your computer and use it in GitHub Desktop.
Little Heap of Horrors skill challenge
//replace with the id of uploaded image
ProductCreator.createProduct('[REPLACE]');
public class ProductCreator {
//cannot modify signiture
@future public static void createProduct(Id attchId){
Attachment attch = [SELECT Name, Body FROM Attachment WHERE ID = :attchId];
String image = EncodingUtil.base64Encode(attch.Body);
HorrorProduct product = new HorrorProduct(attch.name, image);
String body = JSON.serialize(product);
HttpRequest req = createRequest('https://example.com/newProduct', 'POST', body);
sendRequest(req);
}
private static HttpRequest createRequest(String endpoint, String method, String body){
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setBody(body);
req.setMethod(method);
return req;
}
public class HorrorProduct{
public String name {get; set;}
public String image {get; set;} //base64 encoded string
public HorrorProduct(String name, String image){
this.name = name;
this.image = image;
}
}
//DO NOT MODIFY ANYTHING BELOW THIS LINE. Everything above is fair game!
private static void sendRequest(HttpRequest req){
System.assert(Limits.getHeapSize() < Limits.getLimitHeapSize(), 'Heap overallocated by ' + ((Limits.getHeapSize() - Limits.getLimitHeapSize())/1000) + ' kb');
System.debug('Congratulations! You have ' + (Limits.getLimitHeapSize() - Limits.getHeapSize()) + ' bytes of heap memory remaining');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment