function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
alex_from_75015alex_from_75015 

"Changing return type" of a custom REST Webservice

Hello

I have a simple webservice that returns opportunities link to a field of a given account (id of account is sent in request)

I would like to return list of opportunities if account id is correct and an error message 'Incorrect account id' if account id sent in request in incorrect

How can I do that knowing that return of WS is specified in definition

Here would be my code

 

@RestResource(urlMapping='/xxxx/*')
global with sharing class MyWLResource {
  @HttpPost
    global static list <Opportunity> doPost(String accountid)
    {
        Account myaccount;
        list <Opportunity> myresult;
        try
        {
            myaccount = [select website from Account where id=:accountid];
            myresult=[select id from Opportunity where website__c=:myaccount.website limit 10];
            return myresult;
        }
        catch (Exception e)
        {
            return 'Incorrect account id';
        }
    }
}

 

Thanks for your help

Is there a simple solution without building a new class object ?

Regards

Best Answer chosen by Admin (Salesforce Developers) 
Pat PattersonPat Patterson

Hi Alex,

 

You can change the return type of your web service method to void, then set the response body with any string you like. In your case, this would be the JSON encoding of the opportunity list or the error message as appropriate:

 

@RestResource(urlMapping='/xxxx/*')
global with sharing class MyWLResource {
  @HttpPost
    global static void doPost(String accountid) 
    {
        Account myaccount;
        list <Opportunity> myresult;
        try
        {
            myaccount = [select website from Account where id=:accountid];
            myresult=[select id from Opportunity where website__c=:myaccount.website limit 10];

            RestContext.response.addHeader('Content-Type', 'application/json');
            RestContext.response.responseBody = Blob.valueOf(JSON.serialize(myresult));
        }
        catch (Exception e) 
        {
            RestContext.response.addHeader('Content-Type', 'text/plain');
            RestContext.response.responseBody = Blob.valueOf('Incorrect account id');
        }
    }
}

 

Cheers,

 

Pat

All Answers

Pat PattersonPat Patterson

Hi Alex,

 

You can change the return type of your web service method to void, then set the response body with any string you like. In your case, this would be the JSON encoding of the opportunity list or the error message as appropriate:

 

@RestResource(urlMapping='/xxxx/*')
global with sharing class MyWLResource {
  @HttpPost
    global static void doPost(String accountid) 
    {
        Account myaccount;
        list <Opportunity> myresult;
        try
        {
            myaccount = [select website from Account where id=:accountid];
            myresult=[select id from Opportunity where website__c=:myaccount.website limit 10];

            RestContext.response.addHeader('Content-Type', 'application/json');
            RestContext.response.responseBody = Blob.valueOf(JSON.serialize(myresult));
        }
        catch (Exception e) 
        {
            RestContext.response.addHeader('Content-Type', 'text/plain');
            RestContext.response.responseBody = Blob.valueOf('Incorrect account id');
        }
    }
}

 

Cheers,

 

Pat

This was selected as the best answer
alex_from_75015alex_from_75015

Thanks : that's perfect

Regards

Amel MECHALIKHAmel MECHALIKH
great !