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
BAGELBAGEL 

How to modify Product Revenue Schedule Date Fields by AJAX Toolkit?

Hi all, I am new to salesforce.com. However I already need to make some modification, and I am not sure where to begin. If anyone can give me a hand, that would be great. What I need to do is to able to modify schedule dates on revenue schedule for each of the products on the opportunity programmically. If you have an idea/solution, please let me know. Thanks.

TJGAGTJGAG

This might help.  I had to recalculate the schedule dates when the opportunity closed date changed so that the first shedule date is the closed date and all the others follow with the same monthly interval.  This assumes that the schedule has been created with monthly intervals.

The routine gets passed the opportunity id and an HTML element (I use a DIV) where it writes any messages.

Code:

function updateSchedules(opId, HTMLElement)
{
    // OpportunityLineItemSchedules must begin with the close date.
    // This function checks for this condition and recalculates the schedule
    // dates if there is a difference.
 HTMLElement.innerText = "Checking dates on line items...";

 var queryResult = sforceClient.Query("Select CloseDate from Opportunity Where Id = '" + opId + "'"); 
 var opObj = queryResult.records[0];
 var opScheduleStart = new Date(opObj.get("CloseDate"));


 // get address info for account
 queryResult = sforceClient.Query("Select Id from OpportunityLineItem Where OpportunityId = '" + opId + "'"); 
  
    for (var iOpLines = 0; iOpLines < queryResult.records.length;iOpLines++)
    {
  var opLine = queryResult.records[iOpLines];
  var opLineId = "";
  HTMLElement.innerText = HTMLElement.innerText + "*.";
  opLineId = opLine.get("Id");

  var queryResult2 =sforceClient.Query("Select Id, ScheduleDate from OpportunityLineItemSchedule Where OpportunityLineItemId = '" + opLineId + "'"); 
        var scheduleStart = new Date(9999,12,31);
  for (var i=0;i<queryResult2.records.length;i++)
  //  Find earliest date to - use to calculate offset
  {
   var opSchedInst = queryResult2.records[i];
   var newDate = new Date(opSchedInst.get("ScheduleDate"));
   if (newDate < scheduleStart)
   {
       scheduleStart = newDate
   }
  }

  //new array to hold results of updates
  var updateObjects = new Array();

  var dateDiff =  scheduleStart - opScheduleStart ;  // Is there a change?
  if (!(dateDiff == 0))
  {
   // go through schedules recalculating date offset
   for (var i=0;i<queryResult2.records.length;i++)
   //
   {
    HTMLElement.innerText = HTMLElement.innerText + "+-";
    var opSchedInst = queryResult2.records[i];

    d = new Date(opScheduleStart);  // Calculate offset from closed date...
    dy = d.getDate();
    d.setMonth(d.getMonth() + i);   // ...by a number of months
    ndy = d.getDate();
    if (!(ndy == dy))               // If the day numbers are different the date has rolled into
                                    // the following month
    {
     d.setDate(d.getDate() - d.getDate());  // Bring date back to end of current month
    }
    opSchedInst.set("ScheduleDate",d);
    // have to push updated record into new array for update process
    updateObjects.push(opSchedInst);
   }
   // submit new array with updated schedules
            var updResult = new updateSFObjects(updateObjects);
            if (!updResult.success)
         {
    HTMLElement.innerText = "Error occurred with updates.  Please note result and contact support: " + updResult.saveResults.toString();
    HTMLElement.className="red";
                return false;
   }
  }
 }
 return true;
}



updateSFObjects.prototype.saveResults = new Array() ;
updateSFObjects.prototype.returnMessage = "";
updateSFObjects.prototype.success = false;
function updateSFObjects(arSFObj)
{
 try
 {
  this.saveResults = sforceClient.Update(arSFObj);
 }
    catch (e)
    {
     this.returnMessage = e.message + " :---: " + arSFObj.toString() + " :---: " + this.saveResults.toString();
     return ;
    }
 if (!(Sforce.Util.dltypeof(this.saveResults) == "SaveResults" || Sforce.Util.dltypeof(this.saveResults) == "array"))
 {
     this.returnMessage = this.saveResults.toString();
     return ;
    }
    else
    {
        for (ei = 0; ei < this.saveResults.length; ei++)
        {
            var saveResult = this.saveResults[ei];
            if (saveResult.success == false)
            {
       this.returnMessage = this.saveResults.toString();
                return ;
            }
        }
    }
    this.success = true;
    
}