0

I have a class with a single method that creates some account records and related opportunities. These records are clones from another environment that I JSON serialized and I want to execute the class by calling a REST endpoint to create the records in the target environment.

    @RestResource(urlMapping='/testdata')
    global with sharing class TestDataSetup{ 
        //@HttpGet what rest verb should I use here...?
        global static void init(){
            String parentAccount = '', childOpp = '';
            SObject[] records = new SObject[0];
            records.add(JSON.deserialize((Account)parentAccount, Account.Class));
            records.add(JSON.deserialize((Opportunity)childOpp, Opportunity.Class));
            Database.insert(records);
        }   
    }

I have created a site and added the class via the site settings. I just need the class to execute in the target environment but unsure of what rest method to use.

2

I want to execute the class by calling a REST endpoint to create the records in the target environment

//@HttpGet what rest verb should I use here...?

While you can use any annotation here, but because you want to receive a request and then create some records based on the request, you should use a @HttpPost method here.

The @HttpPost annotation is used at the method level and enables you to expose an Apex method as a REST resource. This method is called when an HTTP POST request is sent, and creates a new resource.

So your method should look something like this:

@HttpPost
global static void init() {
    ... all my code here ...
}

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.