In the rest service class, Is it possible to redirect to a record detail page of a object using an record?(assume that the record will get in class) see below code

@RestResource(urlmapping = '/test/*')

global without sharing class test {

 global static String doPost() {

  RestRequest req = RestContext.request;
  RestResponse res = RestContext.response;
  Blob body = req.requestBody;

  contact c = new contact();
  c.lastname = 'Test';
  insert c;
  return null;

Is it possible to Redirect contact record detail page, please any one help on this.


You can generate the link for your record and can use it for redirection. Try something like this in your code.

String RecordId = c.Id;
String Recordlink = URL.getSalesforceBaseUrl().toExternalForm()+'/'+RecordId;

To answer your question for the redirection to a record detail page little vague. You actually can't use the REST service to redirect to a page. All you can get from a service is a response. Response could be anything. If you are returning a link to a record detail page it will still be a response and the caller will decide if you have to redirect now or not.

So by using above snippet you can generate a link but can't achieve a redirection directly. Its the caller's job not the service.

  • let me explain clearly. I have implemented rest service and accessing API via force.com site and external system invoking this service with site URL. Whenever external system send the response with service URL we are creating or updating few objects. After execution is completed then it should be redirect to Opportunity or Contact Detail page. Any solution for this requirement? Please let me know on this. – SFDCLearneR Nov 18 '18 at 10:28
  • @ sfdcLynx please see above comment and reply me – Test userADG23 Nov 18 '18 at 10:29
  • 1
    @SFDCLearneR implementation will remain same. Your service in the force.com platform should generate a URL as I showed you above in the snippet. You can return this generated RecordLink as a response for your external system. And, once you received a response from service. You can redirect to that link. But the service can't do something like this that you update some records and then automatically redirected to some url. This is not possible with service or you can say not the function of any service. – sfdcLynx Nov 18 '18 at 10:37
  • @sfdcLynx Thank you for your help. I understand that we can't able to return to salesforce record detail page after DML operation in REST API. We can able to do this in SOAP API? Can you please let me know on this – Test userADG23 Nov 19 '18 at 10:37

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.