0

I have written a web service in Salesforce, that when executed by a 3rd party, would create a Task in Salesforce.

When I am trying to execute it using Apigee (https://snap.apigee.com/1ShlySU), I am getting the following error.

{
    "message":"Unexpected character ('s' (code 115)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at [line:1, column:2]",
    "errorCode":"JSON_PARSER_ERROR"
}

The service I have written is:

@RestResource(urlMapping='/CreateActivity/*')
global class CreateActivity_API
{
global static boolean isNotNullOrEmpty(string str)
{
    return str!=null || !String.isBlank(str); 
}

    @HttpPost
    global static Task createTask(string status, string priority) 
    {
        if(isNotNullOrEmpty(status) && isNotNullOrEmpty(priority))
        {
            Task newTask = new Task();
            newTask.whoId = '00Q28000001Pr51';
            newTask.status = 'Not Started';
            newTask.priority = 'Normal';
            newTask.subject = 'From Web';
            newTask.OwnerId = '00528000000eGrl';
            return newTask;         
       }
       else 
       {
           System.Debug('Required field values are not provided here');
           return null;
       }
   }
}

Any suggestions either in code or in the way of executing the service is appreciated.

  • 5
    The error is saying that the string you are posting is not valid JSON. It just needs to be of this form {"status": "xxx", "priority": "yyy"}. Are you missing the quotes around the field names? It's best to generate JSON using a well tried framework rather than built the string manually; that avoids syntax problems (including the handling of embedded delimiters and line feeds). – Keith C Jun 15 '15 at 11:34
  • Hey @KeithC I am kinda new to this services and am exploring them. So, could you suggest me the best possible ways to write a standout one, like the one you mentioned "generate JSON using a well tried framework" – Sapthagiri Jun 16 '15 at 5:36
  • I mean something like JsonObject in Java or the JSON class in Apex; basically find the classes for whatever language you are using to generate and parse JSON. – Keith C Jun 16 '15 at 7:37
1

Here is a working version of the code. I called this rest service from WorkBench and Apigee and from both the task is created tasks successfully.

VF Page

@RestResource(urlMapping= '/CreateActivity/*')
global with sharing class CreateActivity_API {
    global static boolean isNotNullOrEmpty(string str) {
        return str != null || !String.isBlank(str);
    }

    @HttpPost
    global static Task createTask(String status, String priority) {
        System.debug('status: '+status);
        System.debug('priority: '+priority);    
        if (isNotNullOrEmpty(status) && isNotNullOrEmpty(priority)) {
            Task newTask = new Task();
            newTask.whoId = '003i000001TPlOT';// Set Scheduling Contact
            newTask.status = status;
            newTask.priority = priority;
            newTask.subject = 'From Web';
            newTask.WhatId = '001i000000gDY4a';// Refer to Account Object
            try{
                insert newTask;
                return newTask;
            }catch(System.DMLException e){
                System.debug(e.getMessage());
                return null;
            }            
        } else {
            System.Debug('Required field values are not provided here');
            return null;
        }
    }
}

Apigee screenshot: In Apigee, first use Salesforce as Authentication provider and pass the parameters as JSON.

enter image description here enter image description here

WorkBench Screenshot

enter image description here

If this helps you, mark it as answer.

  • Thanks @SarojBera this did work. I did figure out my mistake, I was sending the "POST" parameters in a wrong manner in the body and thus was facing that issue. – Sapthagiri Jun 16 '15 at 5:33

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.