function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Joshua RoodJoshua Rood 

Update CloseDate of Opportunity on eventDrop with FullCalendar

Been tinkering with FullCalendar the last few weeks and am now using it to set up an opportunity calendar based on close date. I have it set up so opportunities (events) can be dragged to a different date, and I'd like that action to update the close date of the opp. I've been digging around and trying various format and date methods, but have had no luck. Below is the pertinent snippet of code and I'd appreciate any input. I know there's a ton out there regarding string to date conversions and date formatting, but after hours of research, I just can't seem to find the right mixture.

For refernece, event.id and event.start are both Strings.

On VF Page:
eventDrop: function(event) {
    updateCD(event.id,event.start);
},

In Controller:
 
public PageReference updateCD(String id, String dropDate) {
    Date newDate = date.parse(dropDate);
    
    for(Opportunity o : [select Id, CloseDate from Opportunity where Id=:id]) {
        o.CloseDate = newDate;
        update o;
    }
return null;
}
Best Answer chosen by Joshua Rood
Gulshan_7890Gulshan_7890

Hello Joshua,

You can use a remote action method which executes asyncronously to perform the update.

On VF Page : 
 

eventDrop : function(event){
  ControllerClassName.updateCloseDate(event.id, event.start, function(result, event){
    if(event.status){
    }else{
      alert(event.message);
    }
  });
}
In Controller : 
@RemoteAction
public static void updateCloseDate(Id oppId, String closeDate){
  if(String.isNotBlank(oppId) && String.isNotBlank(closeDate)){
    update new Opportunity(Id = oppId, CloseDate = Date.valueOf(closeDate));
  }
}

Regards,
Gulshan.

All Answers

Gulshan_7890Gulshan_7890

Hello Joshua,

You can use a remote action method which executes asyncronously to perform the update.

On VF Page : 
 

eventDrop : function(event){
  ControllerClassName.updateCloseDate(event.id, event.start, function(result, event){
    if(event.status){
    }else{
      alert(event.message);
    }
  });
}
In Controller : 
@RemoteAction
public static void updateCloseDate(Id oppId, String closeDate){
  if(String.isNotBlank(oppId) && String.isNotBlank(closeDate)){
    update new Opportunity(Id = oppId, CloseDate = Date.valueOf(closeDate));
  }
}

Regards,
Gulshan.
This was selected as the best answer
Joshua RoodJoshua Rood
Thanks, Gulshan!

Implementing your code as-is kicked back this message:
User-added image

I had a feeling it was because event.start is formatted as 'EEE, d MMM yyyy HH:mm:ss', so I appended a .format() when passing that parameter to the updateCloseDate remote action method, like so:

OppCalendar_Controller.updateCloseDate(event.id, event.start.format(), function(result, event) ...

And it's working like a charm! Now it's time to do some research to figure out why that approach worked while the others I tried did not, and hopefully I understand it enough to know when to use it in the futue. Thanks again!
Gulshan_7890Gulshan_7890
Hello Joshua,

Thanks for marking the answer as 'Best Answer'.
The first approach did not work because FullCalendar being a JavaScript / jQuery based library, it is not compatible with Visualforce/Apex PageReference methods directly.
You could have used an <apex:actionFunction/> (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/apexcode/pages_compref_actionFunction.htm) to perform something similar while still using the original updateCD() PageReference method.
But I personally prefers RemoteAction (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_js_remoting.htm) methods which are simpler to implement.

Thanks,
Gulshan.