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
Nuno.CarvalhoNuno.Carvalho 

Button to extend a date

Hi guys,
I need to create a button onclick to update a date field on contracts , all i want to do is to extend the contract 2 years. 
I´m new to creating buttons , so if you can explain to me how to do it i would be very appreciated.
User-added image
Final do Contrato means Contract expiration date.
That´s de field i want to update, 
Regards,
Nuno.
Best Answer chosen by Nuno.Carvalho
Shruti SShruti S
Firstly you will have to create an Apex class.
Goto --> Setup --> Develop --> Apex Class --> Click "New"

In the Apex Class, you can put the below code - 
global class ContractServices {
    global class Response {
        webservice Boolean status;
        webservice String message;
        
        public Response( Boolean status, String message ) {
            this.status      = status;
            this.message     = message;
        }
    }
    
    webservice static Response extendContractDate( String contractId ) {
        try {
            Contract contractToEdit = new Contract();
            
            contractToEdit = [
                SELECT  Name
                        ,EndDate
                FROM    Contract
                WHERE   Id = :contractId
            ];
            
            contractToEdit.EndDate = contractToEdit.EndDate.addYears( 2 );
            
            UPDATE contractToEdit;
            
            return new Response( TRUE, 'Successfully Updated' );
        }
        catch( Exception ex ) {
            return new Response( FALSE, ex.getMessage() );
        }
    }
}
In the Apex class we have created a webservice which accepts the Id of the Contract as parameter and would query for the End Date and Update it after adding 2 years to the current value. If the update was successful and if no errors were encountered, then it would return a True response. Else if any error has occured, it would return a False response and a message stating the error.

Now lets create the button.
Goto --> Setup --> Contract --> Buttons, links and Actions section 
Then click on new New Button or Link. Enter the name of the button and select the type as Detail Page Button. And as the behaviour select Execute JavaScript. The Content-Source should have been set to On-Click Javascript automatically. If not, select it.

Now enter the below code in the button - 
{!REQUIRESCRIPT("/soap/ajax/39.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/39.0/apex.js")}

if( {!NOT(ISBLANK(Contract.EndDate))} )  {
    var response = sforce.apex.execute( "ContractServices", "extendContractDate", { "contractId" : "{!Contract.Id}" } );

    if( response[0].status === "true" ) {
        window.location.reload();
    }
    else {
        alert( response[0].message );
    }
}
In the JavaScript, we send the Id of the selected Contract to the webservice method which we have defined in the Apex class. If we get a true response, we reload the page and if an error has occured, the error message will be displayed as an alert.

All Answers

Shruti SShruti S
Firstly you will have to create an Apex class.
Goto --> Setup --> Develop --> Apex Class --> Click "New"

In the Apex Class, you can put the below code - 
global class ContractServices {
    global class Response {
        webservice Boolean status;
        webservice String message;
        
        public Response( Boolean status, String message ) {
            this.status      = status;
            this.message     = message;
        }
    }
    
    webservice static Response extendContractDate( String contractId ) {
        try {
            Contract contractToEdit = new Contract();
            
            contractToEdit = [
                SELECT  Name
                        ,EndDate
                FROM    Contract
                WHERE   Id = :contractId
            ];
            
            contractToEdit.EndDate = contractToEdit.EndDate.addYears( 2 );
            
            UPDATE contractToEdit;
            
            return new Response( TRUE, 'Successfully Updated' );
        }
        catch( Exception ex ) {
            return new Response( FALSE, ex.getMessage() );
        }
    }
}
In the Apex class we have created a webservice which accepts the Id of the Contract as parameter and would query for the End Date and Update it after adding 2 years to the current value. If the update was successful and if no errors were encountered, then it would return a True response. Else if any error has occured, it would return a False response and a message stating the error.

Now lets create the button.
Goto --> Setup --> Contract --> Buttons, links and Actions section 
Then click on new New Button or Link. Enter the name of the button and select the type as Detail Page Button. And as the behaviour select Execute JavaScript. The Content-Source should have been set to On-Click Javascript automatically. If not, select it.

Now enter the below code in the button - 
{!REQUIRESCRIPT("/soap/ajax/39.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/39.0/apex.js")}

if( {!NOT(ISBLANK(Contract.EndDate))} )  {
    var response = sforce.apex.execute( "ContractServices", "extendContractDate", { "contractId" : "{!Contract.Id}" } );

    if( response[0].status === "true" ) {
        window.location.reload();
    }
    else {
        alert( response[0].message );
    }
}
In the JavaScript, we send the Id of the selected Contract to the webservice method which we have defined in the Apex class. If we get a true response, we reload the page and if an error has occured, the error message will be displayed as an alert.
This was selected as the best answer
Nuno.CarvalhoNuno.Carvalho
Hi Shruti ,
Thank you very much, you helped a lot, not only with the code but understanding it,
Best Regards,
Nuno.