I am working on a rest API using http post to get the params and create a record. After creating the record I need to send a response with the details of created record along with a status code and message in the response body. I am facing issues while implementing the code. below is my code:
@RestResource(urlMapping='/events/*')
global class createEvents {
@HttpPost
global static void createEvent(String subject, Datetime startDateTime, Datetime endDateTime, String psaRequestedBy, String recurs,Datetime recStartDateTime, Date recEndDate, Integer recurrenceDayOfWeekMask){
RestRequest req= RestContext.request;
RestResponse res = RestContext.response;
String accountId= req.requestURI.substring(req.requestURI.lastindexOf('/')+1);
List<Event> events = [select ShowAs, Disable_Alarm__c, WhatId, Subject, ActivityDate, StartDateTime, EndDateTime, PSA_Requested_by__c, OwnerId, IsRecurrence, RecurrenceStartDateTime, RecurrenceEndDateOnly, RecurrenceType, RecurrenceDayOfWeekMask, RecurrenceInterval from Event
where WhatId= :accountId limit 10];
system.debug('events list***'+events);
Event e;
try{
if(events.size() ==0){
e = new Event();
e.ShowAs = 'Busy';
e.Disable_Alarm__c = true;
e.WhatId = accountId;
e.Subject = subject;
e.StartDateTime = startDateTime;
e.EndDateTime = endDateTime;
e.PSA_Requested_by__c = psaRequestedBy;
e.OwnerId = UserInfo.getUserId();
insert e;
}
else if(events.size() >0){
for(Event eve : events){
if((startDateTime <= eve.StartDateTime && endDateTime >= eve.StartDateTime && endDateTime <= eve.EndDateTime) ||
(startDateTime <= eve.StartDateTime && endDateTime >= eve.StartDateTime && endDateTime >= eve.EndDateTime) ||
(startDateTime >= eve.StartDateTime && startDateTime <= eve.EndDateTime)){
// return 'already PSA exist';
// return '{\'status\':\'failure\',\'status_msg\':\'already PSA exist\'}';
RestContext.response.statusCode = 500;
// return res;
}
else{
e = new Event();
e.Type = 'Planned Site Activity';
e.ShowAs = 'Busy';
e.Disable_Alarm__c = true;
e.WhatId = accountId;
e.Subject = subject;
e.StartDateTime = startDateTime;
e.EndDateTime = endDateTime;
e.PSA_Requested_by__c = psaRequestedBy;
e.OwnerId = UserInfo.getUserId();
}
}
insert e;
}
}catch(Exception ex){
system.debug('Error in creating Event'+ex.getMessage());
}
}
}
Thank you in advance for the support.