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
uma52551.3972270309784705E12uma52551.3972270309784705E12 

Creating a custom button and calling the apex code to insert some products

  My team would like to be able to add the some productsby using the Add Products  button through the UI. The button is placed on the account detail page. By clicking the button it should open a VF page to show the products that I want to add and a button called Add Products. So my Account Detail page button should call the Apex code to insert some products into the account. Can any one help me on this? Thanks
rohitsfdcrohitsfdc
Hello there,
Products are added to opportunity as per standard behavior. Do you want to add it on accounts?

What kind of help are you looking for?
uma52551.3972270309784705E12uma52551.3972270309784705E12
Hi rohit,

Thanks for your reply. Actually we are having a custom object called Products__c which will contains all the products. so by creating a button on the account detail page I have to add products into the Products__c. I need help on writing the apex code to add products into the custom object through the button or calling the apex code to accomplish this. thanks!
rohitsfdcrohitsfdc
Uma,
i understand your requirement, but i wont be able to provide you the whole code.. may be you can try creating the code by yourself, and let me know if you stuck anywhere.

Or, i can help you writing you the whole code, but the services would be chargeable. 
uma52551.3972270309784705E12uma52551.3972270309784705E12
Hi Rhoit,

I am having the code to add products. I want to link this code to the button created by me. I mean I need the process. thanks!
rohitsfdcrohitsfdc
okay.

Create a URL type button, and in this URL provide the name of your vf page like /apex/vfpagename

In the page's controller, call the apex code that you have written.

Hope this helps you
uma52551.3972270309784705E12uma52551.3972270309784705E12
Hi Rhoit,

Actually my requriment was to add the custom button on the detail page of the account and when we clicks the detail page button it should directly call the apex code I have written and insert the records into the object.

For the custom button we are having 4 Content Source selections:
1.URL
2.S-Control
3. On- Click java script
4. Visual force page

To accomplish my requriement which option I have to choose.

Note: When I clicks the custom button it should not go to other page and display another button. It has to directly add the records into my object. I am having apex code to insert the records but how to call the controller class from the custom button is little tricky for me. Please give me some Idea.

Thanks In Advance!
rohitsfdcrohitsfdc
Uma,
Go with the URL type button. In the URL box, write 
/apex/vfpagename

And write you logic in the constructor of the vfpage's controller.