Custom Button for Cloning Opportunity without items.

Hi Developers !!. This post is regarding the  Custom Button  on Opportunity Object which  will help us to create clone opportunities without Line items with default values.

Please follow the below steps:

1. Go to setup.
2. Create new custom button for Opportunity object.
3. Give the Label for button.
4. Set behavior as 'Execute JavaScript' and Content Source as 'OnClick JavaScript' .

Paste the below snippet of code in your window.


try {
    {
        !REQUIRESCRIPT("/soap/ajax/14.0/connection.js")
    }
    // Below query will helps you to copy all the fields from your opportunity.
    var result = sforce.connection.query("Select opp.Type, opp.StageName,
                                         opp.Amount, opp.AccountId,opp.
                                         CloseDate,opp.ForecastCategoryName
                                         From Opportunity 
                                         opp WHERE opp.Id = '{!Opportunity.Id}'");
    var cloneopp = result.getArray("records");
    // Reset the Opp Id and reset fields to default values 
    cloneopp[0].Id = '';
    cloneopp[0].Name = "{!Opportunity.Name}";
    // Add new values to the fields
    cloneopp[0].Description = "This is my new value for cloned opportunity";
    var saveResult = sforce.connection.create(cloneopp);
    if (saveResult[0].getBoolean("success")) {
        cloneopp[0].id = saveResult[0].id;
    } else {
        alert("Failed to create clone: " + saveResult[0]);
    }
    location.replace('/' + cloneopp[0].id + '/e');
    location.replace('/' + cloneopp[0].id + '/e?retURL=' + cloneopp[0].id);
} catch (error) {
    alert(error.description);
}
5. Go to opportunity layout and add new button in the layout.


For Reference:




Click on  'Clone opportunity without Products' button.



6. Save the Opportunity.


                                                    Enjoy Coding !!!

Comments

  1. Thanks for the code .. still good after all this time. Any idea how to simply change the Close Date to TODAY + 7 for the Cloned Opportunity?
    Cheers John

    ReplyDelete

Post a Comment

Popular posts from this blog

Test-Driven Development in Salesforce

Setting a default 'From' address when sending an email from Cases

Email Messaging from Salesforce