3

I want to Know how Database Savepoint and Rollback is Working?,I have tried in My Apex class like

@RestResource(urlMapping='/create Event1/')
 global   class  createevent1
  {
  public List<eventitems> eventitems = new list<eventitems>();
  @Httppost

    global static list<event> Eventhelper(){
    list<event> returnlst = new list<event>();
    savepoint sp = database.setsavepoint();
    system.debug(sp);
     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.eventitems);
         return null;
        }
        catch(Exception e)
        {
            database.rollback(sp);
            throw(e);
        } 

        }   
        public static list<event> EventCreationhelper(createevent1 Cevent)
        {
         for(eventitems ei:Cevent.eventitems)
           {
             If(ei.RecurrenceActivityId == null)
              {
             Event ee=new Event();
             ee.OwnerId = ei.Ownerid;
             ee.StartDateTime = ei.StartDate;
             ee.EndDateTime = ei.EndDate;
             ee.whatId = ei.whatid;
             ee.WhoId= ei.whoid;
             if( ei.IsAllDayEvent!= null)
               {
                    ee.IsAllDayEvent = ei.IsAllDayEvent;
               }

             ee.Subject = ei.Subject;
             if(ei.Recurrence != null)
               {
                    ee.IsRecurrence  = ei.Recurrence;
               }
             if(ei.IsAllDayEvent!=null && ei.IsAllDayEvent)
               {
                    DateTime sdT = ei.Recurrencestdate;
                    system.debug('SDT'+sdT);
                    DateTime Recurrencestdate = date.newinstance(sdT.year(),sdT .month(),sdT.day());
                    system.debug('Recurrencestdate'+Recurrencestdate);
                    ee.RecurrenceStartDateTime = Recurrencestdate;

               }
              else
                {
                    ee.RecurrenceStartDateTime = ei.Recurrencestdate;
                }
                DateTime dT = ei.Recurrenceenddate;
                Date Recurrenceenddate= date.newinstance(dT.year(), dT.month(), dT.day());
                ee.RecurrenceEndDateOnly   = Recurrenceenddate;
             ee.RecurrenceDayOfMonth = ei.RDayofMonth;
             ee.RecurrenceDayOfWeekMask = ei.RDayofWeekMask;
             ee.RecurrenceDayOfMonth = ei.RDayofMonth;
             ee.RecurrenceEndDateOnly = ei.Recurrenceenddate;
             ee.RecurrenceInstance = ei.ReccurenceInstance;
             ee.RecurrenceMonthOfYear = ei.RecurrenceMonthYear;
             ee.RecurrenceStartDateTime = ei.Recurrencestdate;
             ee.RecurrenceType = ei.RecType;
             ee.RecurrenceInterval =ei.Recintrvl;
             insert ee;

               /*if(ei.Invitees.size() != 0)
                 {
                  for(id contid :ei.Invitees)
                    {
                      EventRelation er = new EventRelation();
                      er.RelationId = contid;
                      er.EventId = ee.id;
                      insert er;

                     }
                  }*/
          }

    }
   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);
}    
    }

what is the use of Database Savepoint and Rollback in above Code ,Can anyone Explain the Answer for above Questions with Practical Manner,How to Get the System.debug(sp) value should be get in Apex class,which type of input to be given to get the savepoint Value

3
  • quick description: allow to save state of your database at some point of time in execution context, and then quickly revert all the changes since that moment via rollback method. Practical sense -- in case if you are doing some dml, and there are bunch of triggers which are doing some extra dmls, you would be sure that in case of rollback those are reverted.
    – kurunve
    Jul 11 '16 at 9:29
  • In My above Class, I did Not do any Dml Operations ,please explain with the Some basic code ,I am not able to Understand
    – user31717
    Jul 11 '16 at 9:32
  • when i will get the Value for system.debug(sp);in my above code
    – user31717
    Jul 11 '16 at 9:34
8

For the current code shown in the question the database.setsavepoint() and database.rollback() calls have no functional value and could be removed entirely for the same result. You aren't doing any DML operations, so there is nothing to rollback on.

If the code once called EventCreationhelper there would have been some functional value to using the manual transaction statements. Most of what you need to know about using them is in Transaction Control. The following extract provides the best summary:

Sometimes during the processing of records, your business rules require that partial work (already executed DML statements) be “rolled back” so that the processing can continue in another direction. Apex gives you the ability to generate a savepoint, that is, a point in the request that specifies the state of the database at that time. Any DML statement that occurs after the savepoint can be discarded, and the database can be restored to the same condition it was in at the time you generated the savepoint.

3
  • How to get the save point Value , can you share the input for particular Request body Via Postman
    – user31717
    Jul 11 '16 at 9:49
  • What do you mean by wanting the save point value? They don't really have meaningful values for you to use. Are you actually after how to call the POST method defined in Apex? Because that is something completely different. Jul 11 '16 at 9:55
  • 1
    Post it here is there is something meaningful about the question or answer so that others can see it as well. I'm about to sign off for the day. Jul 11 '16 at 9:58

Your Answer

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