0

I am Giving the Request body in Postman like:

{"eventitems":
[{
    "whatid":"0012800000i92pd",
    "whoid":"0032800000TirUN",
    "OwnerId":"005280000010olH",
    "IsAllDayEvent":true,
    "Subject":"call",
    "StartDate":"2016-07-08T14:00:00.000+0000",
    "EndDate":"2016-07-08T15:00:00.000+0000",
   "Recurrence":false

},

{

    "whatid":"0012800000KQTwd",
    "whoid":"0032800000MLsdL",
    "OwnerId":"005280000010olH",
    "IsAllDayEvent":true,
    "Subject":"call",
    "StartDate":"2016-07-09T14:00:00.000+0000",
    "EndDate":"2016-07-09T15:00:00.000+0000",
    "Recurrence":false

}
]
}

For that I am Writing the Apex class

@RestResource(urlMapping='/create Event1/')
 global  with sharing class  createevent1
  {
  public List<eventitems> eventitems2 = new list<eventitems>();
  @Httppost
    global static list<event> Eventhelper()
     {
        savepoint sp = database.setsavepoint();
        list<event> returnlst = new list<event>();
        try
        {
         RestRequest req = RestContext.request;
         RestResponse res = RestContext.response;
         Blob body = req.requestBody;
         string body1=body.tostring();
         system.debug(body1);
         createevent1 obj = createevent1.parse(body1);
         system.debug(obj);
         system.debug(obj.eventitems2);
         }
        catch(Exception e)
        {
         database.rollback(sp);
         throw(e);
        }
return null;
    }
 public Class  eventitems{
      public id whatid;
      public id whoid;
      public id OwnerId;
      public boolean IsAllDayEvent;
      public string Subject;
      public DateTime StartDate;
      public DateTime EndDate;
      public boolean Recurrence;
      public datetime Recurrencestdate;
      public date Recurrenceenddate;
      public String RecType;
      public Integer Recintrvl;
      public Integer RDayofWeekMask;
      public Integer RDayofMonth;
      public string ReccurenceInstance;
      public string RecurrenceMonthYear;
      public id RecurrenceActivityId;
      public list<string> Invitees;
      }
  public static createevent1 parse(String json) {
         system.debug(json);

        return (createevent1)System.json.deserialize(json, createevent1.class);



    }    
    }

In the Above the Value is Which is Given in Postman it is Not Parsing the value is not getting in obj after Parsing in Parse Function and then why here we using Database Savepoint and Rollback,Anyone Share the Answer

  • There's a great tool for generating Apex wrapper classes from JSON - JSON2Apex – smukov Jul 8 '16 at 10:41
1

You are absolutely right. We are not doing any DML before or after parsing and hence we don't need Savepoint/Rollback here. BTW Transaction data automatically get rollback when an exception occurred. If you wanna log the raw log of request you can do DML of request at start of your method and now if you want that request to stay even if below operations fail,then you can use the savepoint.

You can read more about it here

Your class eventitems and method to parse are not proper.

Create a new class for Event wrapper and parsing.

public class EventWrapper{
    public eventitem [] eventitems;
    class eventitem {
        public String whatid;   //0012800000i92pd
        public String whoid;    //0032800000TirUN
        public String OwnerId;  //005280000010olH
        public boolean IsAllDayEvent;
        public String Subject;  //call
        public String StartDate;    //2016-07-08T14:00:00.000+0000
        public String EndDate;  //2016-07-08T15:00:00.000+0000
        public boolean Recurrence;
    }
    public static EventWrapper parse(String json){
        return (EventWrapper) System.JSON.deserialize(json, EventWrapper.class);
    }
}

And instead of calling your parse method use this code.

createevent1 obj = EventWrapper.parse(body1);

  • After Parsing the Obj Value is Not Getting why?Share the Answer – user31717 Jul 8 '16 at 10:31
  • return (createevent1)System.json.deserialize(json, createevent1.class); This Line is Not Working Why – user31717 Jul 8 '16 at 10:33
  • updated code.Please check – Pranay Jaiswal Jul 8 '16 at 10:42
  • ok, Why above my code is Not Working,can you suggest any Changes in My Code for Correct Answer – user31717 Jul 8 '16 at 10:54
  • Your parsing logic is not correct. I have suggested my changes..like replacing a line in your code and Creating a new class for Parsing the json. – Pranay Jaiswal Jul 8 '16 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy