[SalesForce] REST Webservice @HttpGet

I am having trouble compiling a REST webservice class that I have created.

@RestResource(urlMapping='/LeadSAP/*')
global with sharing class SAPLeadWS
{

@HttpPost
global static String createNewLead(
                                    String firstName,
                                    String lastName,
                                    String title,
                                    String email,
                                    String workPhone,
                                    String mobilePhone,
                                    String comments,
                                    String street,
                                    String city,
                                    String state,
                                    String postalCode,
                                    String country
                                  )
{


Lead l = new Lead();
l.RecordTypeId = '01290000000hP9lAAE';
l.FirstName = firstName;
l.LastName = lastName;
l.title = title;
l.email = email;
l.Work_Phone__c = workPhone;
l.MobilePhone = mobilePhone;
l.Description = comments;
l.street = street;
l.city = city;
l.state = state;
l.postalCode = postalCode;
l.country = country;
insert l;

return l.Id;

}


@HttpGet
global static String getLead(String SAPId)
{

Lead l = [SELECT Id, FirstName,LastName,title,email,Work_Phone__c,MobilePhone,Description,street,city,state,postalcode,country from Lead WHERE Id = :SAPId ];
if(l == null)
{

return 'Lead Does Not Exist';

}
else
{

String s = 'Id = '+SAPid+' FirstName = '+l.FirstName+' LastName = '+l.LastName+' title = '+l.title+' email = '+l.email+' WorkPhone = '+l.Work_Phone__c+' MobilePhone = '+l.MobilePhone+' Description = '+l.description+' Street = '+l.street+' city = '+l.city+' state = '+l.state+' postalcode = '+l.postalcode+' country = '+l.country;
return s;

}

}

}

I was able to compile and test successfully the @HttpPost method but when I added the code for @HttpGet I am having compile errors.

The error says that

Error   Error: Compile Error: Invalid type: HTTP GET/DELETE methods do not support parameters at line 45 column 22  

Without a parameter to pass I am not sure how @HttpGet can be built because my intention is to pass id and then return a string depending on whether there was a successful retrieval or not.

UPDATED GET SECTION

public static final String SAP_ID_PARAM = 'SAPId';


@HttpGet
global static String getLead()
{

//String jsonInstance = RESTContext.request.requestBody.toString();
//Payload p = JSON.deserialize(jsonInstance,Payload.class());

Id SAPId = RestContext.request.params.get(SAP_ID_PARAM);



Lead l = [SELECT Id, FirstName,LastName,title,email,Work_Phone__c,MobilePhone,Description,street,city,state,postalcode,country from Lead WHERE Id = :SAPId ];
if(l == null)
{

return 'Lead Does Not Exist';

}
else
{

String s = 'Id = '+SAPid+' FirstName = '+l.FirstName+' LastName = '+l.LastName+' title = '+l.title+' email = '+l.email+' WorkPhone = '+l.Work_Phone__c+' MobilePhone = '+l.MobilePhone+' Description = '+l.description+' Street = '+l.street+' city = '+l.city+' state = '+l.state+' postalcode = '+l.postalcode+' country = '+l.country;
return s;

}

}

Can someone tell me how to actually test this in Workbench as I am not sure how to send this parameter as part of restrequest ?

enter image description here

Best Answer

You need to use make use of the RestContext.request. The way you do so will vary based on the Http method in question.

POST

Here you will need to parse the RestContext.request.requestBody. Usually this will be a JSON payload which you deserialize. For example:

public class PostPayload
{
    public String firstName;
    public String lastName;
    public String etc;
}

@HttpPost
global static void String createLead()
{
    String jsonInstance = RestContext.request.requestBody.toString();
    PostPayload payload = JSON.deserialize(jsonInstance), PostPayload.class);
    // do stuff
}

This payload can be set in your tests as follows:

MyRestService.PostPayload payload = new MyRestService.PostPayload;
payload.firstName = 'Road';
payload.lastName = 'Runner';
RestContext.request.requestBody = Blob.valueOf(JSON.serialize(payload));

GET

Here you will need to pull values out of RestContext.request.params. I tend to make my keys constants so that I can test against them more easily.

public static final String SAP_ID_PARAM = 'sapId';

@HttpGet
global static void String getLead()
{
    Id sapId = RestContext.request.params.get(SAP_ID_PARAM);
    if (String.isBlank(sapId))
    {

    }
    // do stuff
}

This value can be set in your tests as follows:

RestContext.request.params.put(MyRestService.SAP_ID_PARAM, 'Some Value');
Related Topic