1

I have a scenario where I need to do a httpPost to save a value from the request parameter and then return some other fields for the same object fields using httpget.

So, my parameter would be something like this:

String opptyId = RestContext.request.params.get('opportunityId');
String nameOfComp = RestContext.request.params.get('newname'); 

Now, I need to update this field nameOfComp from the request param based on opportunityId from the req param.

Once the update is done, I need to get some more data from the same object including the nameOfComp which i have inserted.

Can this be done within one service and single URL mapping?

3
  • 1
    Hello welcome to stackexchange ! whats stopping you from doing it ? You can add additional apex code there? Apr 8, 2020 at 23:10
  • Thanks @MohithShrivastava .. So, am confused if I need to first create a httppost and then httpget? Or just a DML Update and the query to select other fields to be exposed as httpget. Is it allowed to do a DML update within httpget
    – Rajaravi
    Apr 8, 2020 at 23:25
  • @Rajaravi Please check the answer and let me know if thats what you were looking for Apr 8, 2020 at 23:37

1 Answer 1

0

You can perform all the apex operations in your @http post method and return response. The actions will be synchronously performed in salesforce.

You do not need another service unless you are running some asynchronous action. Note that below code is for demonstration purpose and has sudo code to help you figure it.

@RestResource(urlMapping='/OppService/v1/*')
 global with sharing class OppService {

 @HttpPost
 global static String oppService(){
    RestRequest req = RestContext.request;
    RestResponse res = Restcontext.response;
    String opptyId = RestContext.request.params.get('opportunityId');
    String nameOfComp = RestContext.request.params.get('newname'); 
    // perform your DML
    insert opp;
    // Execute SOQL
     Opportunity opp = [Select Id,Name FROM opportunity where Id=: opp.Id];
     // Additional query and logic
    // Return the response
      return opp;
   }
}

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