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
azimuazimu 

Button that manipulates a date field on a record

Hi all,

Was hoping I could get some help with programming a button! I've got a custom object "service" which has a field expirydate__c (date) and a field billfrequency__c (number). 

This sounds easy but for some reason I'm having a really hard time doing it - I'd like to have a 'renew' button which calls a class which simply updates the expiry date to previous expiry date + billfrequency.

Any help much appreciated! 
Best Answer chosen by azimu
MithunPMithunP
Hi Azim Husain,

Create a Javascript Button and use following script in button and create an apex class.

Button Code
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}


var RecId ='{!service__c.Id}';
sforce.apex.execute("MyClass","myMethod",{objId:RecId});
location.reload(true);
alert("Success");

Apex Class Code
global class MyClass
{
    webservice static void myMethod(Id objId) 
    { 
         if(objId != null){
              service__c servicetest = [Select Id,ExpiryDate__c,Billfrequency__c From service__c Where id =: objId];
              Integer days = Integer.ValueOf(servicetest.Billfrequency__c);
              servicetest.ExpiryDate__c = servicetest.ExpiryDate__c.adddays(days);
              Update servicetest;
         }
    }
}
Let me know if you face any issues.

Best Regards,
Mithun.
 

All Answers

MithunPMithunP
Hi Azim Husain,

Create a Javascript Button and use following script in button and create an apex class.

Button Code
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}


var RecId ='{!service__c.Id}';
sforce.apex.execute("MyClass","myMethod",{objId:RecId});
location.reload(true);
alert("Success");

Apex Class Code
global class MyClass
{
    webservice static void myMethod(Id objId) 
    { 
         if(objId != null){
              service__c servicetest = [Select Id,ExpiryDate__c,Billfrequency__c From service__c Where id =: objId];
              Integer days = Integer.ValueOf(servicetest.Billfrequency__c);
              servicetest.ExpiryDate__c = servicetest.ExpiryDate__c.adddays(days);
              Update servicetest;
         }
    }
}
Let me know if you face any issues.

Best Regards,
Mithun.
 
This was selected as the best answer
azimuazimu
Incredible - thank you! That did it :)