2

i wrote a rest service i am inserting 2 fields if i get any null value for any field i have to send the JSON response to the third party server can any one suggest me where i have to change my service to send the response

webservice :

@RestResource(urlMapping='/Account/*')
global with sharing class AccntController{
@HttpPost
global static String createaccnt(List<ACCaccnts>AccList) 
{
List<Account> AccountList = new List<Account>();
if(AccList!=null){
for(ACCaccnts acc: AccList){
    Account tc = new Account();
     tc.Name = acc.accName;
    tc.accJson__c = acc.json;
   AccountList.add(tc);

}
}
if(!AccountList.isempty())
     insert AccountList;
    return null;
 }
global class ACCaccnts{
     global String json;
    global String accName;
  }
} 
1

Return whatever you'd like to send, and it will be sent back to the caller. You may also want to set an appropriate content type in RestContext.response.headers to make sure the response is interpreted correctly. Example code follows.

if(accountList.isEmpty()) {
    return JSON.serialize(
        new Map<String, Object> { 
            'success' => false, 
            'message' => 'No accounts were received, so no accounts were created.'
        }
    );
}

Alternatively, you can set a response in RestContext.response.responseBody if you'd like, which would allow you to change your method to a void return type. It's up to you which design you prefer. Finally, if it's truly an error, you may also choose to set RestContext.response.statusCode to a non-200 value (400 would be a good choice, meaning "Bad Request"), but this will depend on your caller's needs.

  • i have to check the authorization check also.that means user is connecting to salesforce by using valid client id or not may i know how can i check such type of check - @sfdcfox – Sathya Oct 13 '15 at 9:13
  • @Sathya If you're not using Sites, they're already authenticated and therefore have access. This is one of the features of using salesforce. You can tell who they are logged in as using UserInfo.getUserId(), etc. If you are using sites, you can see if they're logged in as the guest user and reject the request, otherwise they're authenticated. – sfdcfox Oct 13 '15 at 20:32

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.