0

I still can't send to my Callout right status code.

I have an Item object in Org2 that I can post from Org1 (in Org1 is Tool object that passes its fields to THIS (Item) object). Item has ExternalID field that takes the ID from Tool. How can I send an error to Callout if Item with this ExternalID is already posted?

My code:

    @HttpPost
    global static Item__c post() {
    RestRequest req = RestContext.request;          
    Item__c item = new Item__c();
    //Here we get our Item record with ExternalID already:
    String body = req.requestBody.toString();
    RestResponse res = new RestResponse();
    RestContext.response = res;

    try{
        it = (Item__c)JSON.deserialize(body, Item__c.class);
        insert item;
        res.responseBody = Blob.valueOf(JSON.serialize(item));
        res.statusCode = 200;
        return item;
    } catch(JSONException e) {
        res.statusCode = 400;
    } catch(Exception e) {
        res.statusCode = 500;
    } 
    return null;
}

The best previous answer but it's not work:

    @HttpPost
    global static void post() {
    RestRequest req = RestContext.Request;          
    String body = req.requestBody.toString();
    Item__c item = (Item__c)JSON.deserialize(body, Item__c.class);
    RestResponse response = new RestResponse();
    //First problem - we haven't Id before insert anyway:
    if([select Id from Item__c where ExternalID__c = :item.ExternalID__c limit 1].isEmpty()){
        insert item;
        response.statusCode = 200;
        response.responseBody = Blob.valueOf(JSON.serialize(item));
    }else{
        response.statusCode = 409;
        response.responseBody = Blob.valueOf('Item with External Id ' + item.ExternalID__c + 'already exists');
    }
    //Second problem - I need try-catches, not just if/else, to catch a few Exceptions
    RestContext.response = response;
    //And I always become there code 200. I don't know why
}

Without if-else, just with try-catches, my code works great. But I still haven't an error for situation, when Item already exists.

2

External ID and Unique ID both are different. You can have duplicate values in External ID field if its not clicked as Unique.

Src: https://help.salesforce.com/articleView?id=000005395&language=en_US&type=1

That being said, you have to make your external ID field as unique from Object Setup.

If you try to insert with same UnqiueID field you will get DML exception and not JSON exception.

@HttpPost
    global static void post() {
    RestRequest req = RestContext.request;          
    Item__c item = new Item__c();
    //Here we get our Item record with ExternalID already:
    String body = req.requestBody.toString();
    RestResponse res = new RestResponse();
    RestContext.response = res;

    try{
        it = (Item__c)JSON.deserialize(body, Item__c.class);
        insert item;
        res.responseBody = Blob.valueOf(JSON.serialize(item));
        res.statusCode = 200;

    } catch(DMLException e) {
        res.statusCode = 400;
    } catch(Exception e) {
        res.statusCode = 500;
    } 

}
  • 1
    +1 but the return type should be void. – sfdcfox Oct 29 '18 at 17:48
  • 1
    And no return null; :D – sfdcfox Oct 29 '18 at 17:51
  • I still can't get error status code from this DMLEx. – MichaelLev19 Oct 30 '18 at 13:50
1
//First problem - we haven't Id before insert anyway:

If you already have a previous record, you'll have an ID.

//Second problem - I need try-catches, not just if/else, to catch a few Exceptions

You can put a try-catch inside an if statement. Nothing's stopping you from doing so.

//And I always become there code 200. I don't know why

Because the logic worked as written. You need to have a record already inserted to get the 409, and you need an invalid JSON to get your 400 error; you'll need your try-catch block to make sure you catch invalid JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.