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
Vic PrabhuVic Prabhu 

Update related object field on custom button click on parent object

Hi All,

I have the following requirement. When cutom button is clicked on Opportunity detail page, i need to update the schedule date for all products associated with that opportunity to match Opportunity close date.

Below is my code, i'm getting Script error on button click, what am i missing?

Button Click Javascript:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}

// Call Apex Web service

var result = sforce.apex.execute(
"ProductScheduleDateUpdate", // class
"updateProductDate", // method
{iDate:"{!Opportunity.CloseDate}", // method arguments
iOppId:"{!Opportunity.Id}"});

}


Apex Class code

global class ProductScheduleDateUpdate
{

    WebService static String updateProductDate(Date iDate, String iOppId){
        List<OpportunityLineItem> oliList = [SELECT Id, OpportunityId FROM OpportunityLineItem
                                        WHERE OpportunityId= : iOppId];

        if(oliList.size() == 0){
            return 'No Opportunity Products to Update';
        }

        List<OpportunityLineItemSchedule> olisList = new List<OpportunityLineItemSchedule>();
        Map<Id, String> oppLineItemIdIdOppIdMap = new Map<Id, String>(); // it is for setting opportunity access level

       
        for(OpportunityLineItemSchedule olis : [SELECT Id, ScheduleDate, OpportunityLineItemId
                                                FROM OpportunityLineItemSchedule
                                                WHERE OpportunityLineItemId IN : oppLineItemIdIdOppIdMap.keySet()]) {
            olis.ScheduleDate = iDate;
            olisList.add(olis);
        }
       
        if(olisList.size() > 0){
            update olisList;
        }
   
        return 'All Product Schedule Date updated to match Opportunity Close Date';
    }
}
NishBNishB
Hi,
    There is an extra closing bracket in the end  ' } ' in the javascript code


Vic PrabhuVic Prabhu
Thanks NishB.

I figured that '}" in the javascript this morning. It's whierd that clicking on Check Syntax Errors did not catch it.

Now i have a issue with setting the Close Date to Schedule date, i get following error

"Faultstring: '12/2/2013' is not a valid value for type xsd:date time"

I figure i need to use some kind of date function to convert date to salesforce date type, but i'm not sure what function and where to use it.

Any help?
NishBNishB
I think the date gets passed as a String so try declaring it has String and then convert the string to Date type in the class. i.e updateProductDate(String iDate, String iOppId)
Vic PrabhuVic Prabhu

Thanks Nisha.

I resolved the date issue by using the Date() function in the Button Click javascript

code is below, hope it will help someone else:


{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}

var OppCloseDate = "{!Opportunity.CloseDate}";

// Call Apex Web service
var result = sforce.apex.execute(

 "ProductScheduleDateUpdate",   // class

 "updateProductDate",           // method

 {iDate: new Date(OppCloseDate),   // method argument

  iOppId:"{!Opportunity.Id}"});

window.location.reload();
alert(result);