0

This question came to my mind out of curiosity.

I have an apex class in which I want to create a record. I can use a simple insert DML operation to create it. Although I know that REST API services are usually meant to be consumed by external systems, but here I can even make a REST API call to create a record in Salesforce.

Which one should I prefer? What are the advantages and limitations of one over the other?

.

3 Answers 3

4

Don't use callouts unless you have to.

  • It's extremely inefficient to use an API and Http protocols when you have a direct route to the platform.
  • Using a callout also gives you much more awkward failure paths, and cannot handle partial failures as gracefully.
  • REST calls are impossible to unit test directly. You need a mock.

I find that last point most compelling, and the difference is illustrated below.

With DML

@IsTest static void ensureRecordInsert()
{
    Test.startTest();
        insert new My_Object__c();
    Test.stopTest();

    system.assertEquals(1, [SELECT count() FROM My_Object__c], 'A record should be inserted');
    // this test will pass
}

With Callout

@IsTest static void ensureRecordInsert()
{
    Test.startTest();
        // insert record via callout
    Test.stopTest();

    system.assertEquals(1, [SELECT count() FROM My_Object__c], 'A record should be inserted');
    // this test will fail
}
3
  • Not impossible... you could mock the callout and insert the record... but that's a very roundabout method of doing so.
    – sfdcfox
    Commented Sep 21, 2018 at 16:59
  • You're not testing the callout at that point. Your mock can do whatever you wish, and does not have any guarantee it will behave the same as OOTB REST API.
    – Adrian Larson
    Commented Sep 21, 2018 at 16:59
  • 2
    Eh, you have to write a proper mock to get the proper results. I personally wouldn't want to write a mock just to emulate the API. And I would make my mock verify that it's running against a specific API version to prevent regression problems.
    – sfdcfox
    Commented Sep 21, 2018 at 17:04
2

I've done both (assuming you mean the AJAX Api). It just depends on the circumstances.

If I'm doing a very UI intense, JS heavy page I'd probably use the AJAX API and manipulate the DOM myself.

If I'm doing something more standard/simpler I would use DML.

In terms of advantage, I think the ajax API path provides a more responsive UI but it it requires you do all the work in JS in terms of record manipulation which theoretically could open up some holes for malicious users. For instance, we have a page that allows you to add quote items and auto calculates prices and applies uplifts etc, a savy user could modify the calculations in the JS and change the prices. This also means you wont have access to all the Salesforce provided classes.

Doing things on the back end VIA DML is a bit more secure although slower. It also poses some challenges when trying to send data from JS to the controller. There are plenty of workarounds to these challenges that you can find online.

Rest from Apex would be terribly inefficient. I would not recommend this approach.

3
  • 2
    You would make a REST API call in Apex to create a record?
    – sfdcfox
    Commented Sep 21, 2018 at 16:54
  • No, using the AJAX API I assume is what the user is asking about. If they are talking about rest from APEX that would be kind of silly. I guess I just assumed they were smarter than that.
    – gNerb
    Commented Sep 21, 2018 at 16:56
  • OP is asking about DML vs. REST API to say create a record from Apex, that's what I get from the question.
    – Jayant Das
    Commented Sep 21, 2018 at 20:39
1

Which one should I prefer? What are the advantages and limitations of one over the other?

If you are writing something in your Apex class and that you want to perform a DML operation, I wouldn't even think of using REST APIs. If I can write something as :

insert act;
update act;

Why would I even go to the route where I will need to create request and handle response, etc.? I will just introduce lot of "boiler-plate" code here without that being required. And that performing a DML within my boundary (Apex) with all contracts (SOQL, DML statements) already available is the obvious and without any question route here.

You will use anything around that if at all there's no way of directly doing it. And from DML perspective, I don't think you will ever face that scenario at all. Remember APIs are meant to provide an interface to a non-conforming application which is definitely not a case when you are within your (Salesforce's) own boundary.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .