You need to sign in to do that
Don't have an account?
syed akram
how to write test class ? below is the code.
public class SalesOrder {
public Order record{get;set;}
public SalesOrder (ApexPages.standardcontroller std)
{
record = new Order();
}
public pagereference dosave(){
record.QuoteId = ApexPages.currentPage().getParameters().get('quoteID');
insert record;
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
pagereference page=new pagereference('/apex/SalesOrder');
return page;
}
public pageReference Cancel(){
pagereference page = new pageReference('/apex/QuoteVfPage');
page.setRedirect(true);
return page;
}
}
here is the test class i have written correct me. i am new to salesforce
@istest
Private class TestSalesOrder{
Static TestMethod void Dosave(){
Test.startTest();
PageReference pageRef = Page.SalesOrder;
Test.setCurrentPageReference(pageRef);
String quoteId;
if(!test.isrunningtest()) {
quoteId = ApexPages.currentPage().getParameters().get('quoteID');
Account a=new Account(Name='test1',Phone='9458383336');
insert a;
Order ord=new Order(AccountId=a.id,Name='test',status='draft',EffectiveDate=system.today());
insert ord;
}
else
{
}
Test.stopTest();
}
Static TestMethod void Cancel(){
Test.StartTest();
PageReference pageRef = Page.QuoteVfPage;
Test.setCurrentPageReference(pageRef);
}
}
public Order record{get;set;}
public SalesOrder (ApexPages.standardcontroller std)
{
record = new Order();
}
public pagereference dosave(){
record.QuoteId = ApexPages.currentPage().getParameters().get('quoteID');
insert record;
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
pagereference page=new pagereference('/apex/SalesOrder');
return page;
}
public pageReference Cancel(){
pagereference page = new pageReference('/apex/QuoteVfPage');
page.setRedirect(true);
return page;
}
}
here is the test class i have written correct me. i am new to salesforce
@istest
Private class TestSalesOrder{
Static TestMethod void Dosave(){
Test.startTest();
PageReference pageRef = Page.SalesOrder;
Test.setCurrentPageReference(pageRef);
String quoteId;
if(!test.isrunningtest()) {
quoteId = ApexPages.currentPage().getParameters().get('quoteID');
Account a=new Account(Name='test1',Phone='9458383336');
insert a;
Order ord=new Order(AccountId=a.id,Name='test',status='draft',EffectiveDate=system.today());
insert ord;
}
else
{
}
Test.stopTest();
}
Static TestMethod void Cancel(){
Test.StartTest();
PageReference pageRef = Page.QuoteVfPage;
Test.setCurrentPageReference(pageRef);
}
}
You need to modify the class code as It is saying that there is one required field on Order object with name as "Account Name" if API name is Account_Name__c
Then you need to modify the code below see the line 9 added
All Answers
Change the below line in your test class Say you have a quote created and the Id of that quote is in "quoteID"
say Your created quote is myQuote
quoteID = myQuote .Id;
Replace it with below line
Don't forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
Have you created the test data
Then only it will show.
In the above test class there is no test data creatd.
You have not Invoked the class SalesOrder in your whole test class just add a line something like this inside your test method
SalesOrder s=new SalesOrder (); s. dosave(); s.cancel();
Hit like and best answer if its helpful.
Code coverage can bbe checked for each and every line using developer console. Click your name and developer console. File -->open test class and run the test.
Thanks,
Nirmal
http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
Akram look the above link and do as it's done in the example.
Regards,
Nirmal
Account a=new Account(Name='test1',Phone='9458383336');
insert a;
Order ord=new Order(AccountId=a.id,Name='test',status='draft',EffectiveDate=system.today());
insert ord;
is it not the required data
SalesOrder s= new SalesOrder (new ApexPages.StandardController(account))
Hope I answered your question. Hit best answer button if its helpful.
Regards,
Nirmal
s.Dosave(); // getting error here as [Error] Error: Compile Error: Method must define a body at line 10 column 2
Private class TestSalesOrder{
SalesOrder s=new SalesOrder();
s.Dosave();
Static TestMethod void Dosave(){
Test.startTest();
PageReference pageRef = Page.SalesOrder;
Test.setCurrentPageReference(pageRef);
String quoteId;
if(!test.isrunningtest()) {
quoteId = ApexPages.currentPage().getParameters().put('quoteID', quoteID);
Account a=new Account(Name='test1',Phone='9458383336');
insert a;
Order ord=new Order(AccountId=a.id,Name='test',status='draft',EffectiveDate=system.today());
insert ord;
}
else
{
}
Test.stopTest();
}
Static TestMethod void Cancel(){
Test.StartTest();
PageReference pageRef = Page.QuoteVfPage;
Test.setCurrentPageReference(pageRef);
}
}
@istest
Private class TestSalesOrder{
Static TestMethod void Dosave(){
Test.startTest();
PageReference pageRef = Page.SalesOrder;
Test.setCurrentPageReference(pageRef);
String quoteId;
Account a=new Account(Name='test1',Phone='9458383336'); // creating account
insert a;
Contact c = new Contact(LastName='Test'); // creating contact
insert c;
// creating opportunity
Opportunity opp = new Opportunity(
Name='Test Opp',
AccountId=a.Id,
StageName='closedwon',
CloseDate=Date.Today()
);
insert opp;
Quote q=new Quote(Name='testquote',opportunityId=opp.Id);
insert q;
if(!test.isrunningtest()) {
Order ord=new Order(AccountId=a.Id,Name='test',status='draft',EffectiveDate=system.today());
ord.quoteId = ApexPages.currentPage().getParameters().put('q', quoteID);
insert ord;
}
else
{
}
Test.stopTest();
}
Static TestMethod void Cancel(){
Test.StartTest();
PageReference pageRef = Page.QuoteVfPage;
Test.setCurrentPageReference(pageRef);
}
}
Try the below code and let me know.
Don't forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help.
Class TestSalesOrder
Method Name Dosave
Pass/Fail Fail
Error Message System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Account Name:You must enter a value: []
Stack Trace Class.SalesOrder.dosave: line 9, column 1
Class.TestSalesOrder.Dosave: line 21, column 1
You need to modify the class code as It is saying that there is one required field on Order object with name as "Account Name" if API name is Account_Name__c
Then you need to modify the code below see the line 9 added
Order ord = newOrder(AccountId=a.Id,Name='test',status='draft',EffectiveDate=system.today());
See the code of your class SalesOrder
I have added line 9 update the same with the API name and see what will happen
If the name is just the API name then line 9 in your class should be modified to
record.AccountId='TestQuote';
Error Message System.StringException: Invalid id: TestQuote
Stack Trace Class.SalesOrder.dosave: line 9, column 1
Class.TestSalesOrder.Dosave: line 22, column 1
It is asking Quote name field not account name field
Error Message System.StringException: Invalid id: a.Id
Stack Trace Class.TestSalesOrder.Dosave: line 18, column 1
<apex:page standardController="Order" tabStyle="Order" extensions="SalesOrder">
<apex:form >
<apex:pageMessages ></apex:pageMessages>
<div align="center" draggable="false" >
<apex:commandButton style="left:-220px;position:relative;" value="Save" action="{!dosave}" onclick="return confirm('are you want to submit');"/>
<apex:commandButton style="left:-200px;position:relative;" value="Cancel" action="{!Cancel}"/>
</div>
<apex:pageblock title="Order Information">
<apex:pageBlockSection columns="2" >
<apex:inputField value="{!record.AccountId}" />
<apex:inputField value="{!record.name}"/>
<apex:inputField value="{!record.Status}"/>
<apex:inputField value="{!record.EffectiveDate}"/>
<apex:inputField value="{!record.Type}"/>
<apex:inputField value="{!record.ShipToContactId}"/>
<apex:inputField value="{!record.CompanyAuthorizedById}"/>
<apex:inputField value="{!record.ShippingCity}"/>
<apex:inputField value="{!record.BillingStreet}"/>
<apex:inputField value="{!record.ShippingState}"/>
<apex:inputField value="{!record.BillingCity}"/>
<apex:inputField value="{!record.BillingState}"/>
<apex:inputField value="{!record.ShippingCountry}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>