You need to sign in to do that
Don't have an account?
Filipe Baradelli
How to execute Apex code with a Visualforce Page?
I've created an Apex class to do a test and I need to execute it. I would like to know how to execute it with a button for example (I've created a button that is related with a Visualforce page).
Here is the code that I want to test:
Here is the code that I want to test:
public class Teste { public void teste1(){ // Insert Product Product2 pr = new Product2(); pr.Name='Moto - G1'; pr.isActive=true; insert pr; // Insert Pricebook PriceBook2 customPriceBook = new PriceBook2(); customPriceBook.Name='Custom Pricebook'; customPriceBook.IsActive=true; insert customPriceBook; // Query Standard and Custom Price Books Pricebook2 customPriceBookRec=[select Id from Pricebook2 where id=:customPriceBook.Id]; Id stdPriceBookRecId = Test.getStandardPricebookId(); // Create Standard PriceBookEntry PriceBookEntry stdPriceBookEntry = new PriceBookEntry(); stdPriceBookEntry.Product2Id=pr.Id; stdPriceBookEntry.Pricebook2Id=stdPriceBookRecId; stdPriceBookEntry.UnitPrice=2000; stdPriceBookEntry.IsActive=true; insert stdPriceBookEntry; // Create Custom PriceBookEntry PriceBookEntry customPriceBookEntry = new PriceBookEntry(); customPriceBookEntry.Product2Id=pr.Id; customPriceBookEntry.Pricebook2Id=customPriceBookRec.Id; customPriceBookEntry.UnitPrice=5000; customPriceBookEntry.IsActive=true; insert customPriceBookEntry; // Create Opportunity Opportunity opp = new Opportunity(); opp.Name = 'Test'; opp.CloseDate= System.Today(); opp.StageName='Prospecting'; insert opp; // Add product and Pricebook to the particular opportunity using OpportunityLineItem OpportunityLineItem oppLineItem = new OpportunityLineItem(); oppLineItem.OpportunityId = opp.Id; oppLineItem.PricebookEntryId = customPriceBookEntry.Id; oppLineItem.UnitPrice = 7000; oppLineItem.Quantity = 5; insert oppLineItem; } }Could you help me please?
use command button tag and call method
<apex:form>
<apex:commandbutton value="something" action="{!methodname}"/>
</apex:form>
I put this: But does'nt works. It is because of the Visualforce sentence or the cause is the code in apex?
I can't create a pricebook and pricebookentry because of the "test".
Thanks by the help, if you can help me in this part, I'll be glad too.