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
syed akramsyed 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);
}
}
Best Answer chosen by syed akram
Mudasir WaniMudasir Wani
Dear Syed,

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
 
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');
       record.Account_Name__c ='TestQuote';//Make it dynamic I am just giving the example
       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;
     }   
}

 

All Answers

Mudasir WaniMudasir Wani
Hello,

Change the below line in your test class
quoteId = ApexPages.currentPage().getParameters().get('quoteID');
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
ApexPages.currentPage().getParameters().put('quoteID', quoteID);

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.
syed akramsyed akram
after changing also same 0 % code coverage
 
syed akramsyed akram
where to check the code coverage? for particular test class
Mudasir WaniMudasir Wani
Developer console once you run the test class you need to go to the apex class for which it was written for.
Have you created the test data 
Then only it will show.
In the above test class there is no test data creatd.
Nirmal ChristopherNirmal Christopher

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
Nirmal ChristopherNirmal Christopher
you will create the test data inside the test class

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
syed akramsyed akram
i am inserting the record here

  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
Nirmal ChristopherNirmal Christopher
for this error try some thin like this in your test class

SalesOrder s= new SalesOrder (new ApexPages.StandardController(account))

Hope I answered your question. Hit best answer button if its helpful.

Regards,
Nirmal
Nirmal ChristopherNirmal Christopher
https://developer.salesforce.com/forums?id=906F00000008xsOIAQ
syed akramsyed akram
SalesOrder s=new SalesOrder();
 s.Dosave();  // getting error here as [Error] Error: Compile Error: Method must define a body at line 10 column 2
Nirmal ChristopherNirmal Christopher
Give me the complete code you have rigt now let me check.
syed akramsyed akram
@istest
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);
 }
}
syed akramsyed akram
updated code but same 0 % code coverage can anyone suggest me here ie code

@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);
 }
}
Mudasir WaniMudasir Wani
Hello,

Try the below code and let me know.
 
@istest
Private class TestSalesOrder{
	Static TestMethod void Dosave(){
		Test.startTest(); 
			PageReference pageRef = Page.SalesOrder;
			Test.setCurrentPageReference(pageRef);
			Account a=new Account(Name='test1',Phone='9458383336');   // creating account
			insert a;
			Contact c = new Contact(LastName='Test', AccountId=a.id);                  // 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;
			ApexPages.currentPage().getParameters().put('quoteID',q.Id );
			Order ord = new Order(AccountId=a.Id,Name='test',status='draft',EffectiveDate=system.today());
			Insert ord;
			ApexPages.standardcontroller std;
			SalesOrder sOrder = new SalesOrder(std);
			sOrder.dosave();
			sOrder.Cancel();
		Test.stopTest();
	}
	Static TestMethod void Cancel(){
		Test.StartTest();
		PageReference pageRef = Page.QuoteVfPage;
		Test.setCurrentPageReference(pageRef);
	 }
}

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.
syed akramsyed akram
Time Started 5/19/2015 5:11 AM
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
syed akramsyed akram
HI Mudasir wani i getting the above error if i run the test class
Mudasir WaniMudasir Wani
Dear Syed,

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
 
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');
       record.Account_Name__c ='TestQuote';//Make it dynamic I am just giving the example
       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;
     }   
}

 
This was selected as the best answer
syed akramsyed akram
Account name is mandatory while creating Order and  i am passing the name as well
 Order ord = newOrder(AccountId=a.Id,Name='test',status='draft',EffectiveDate=system.today());
Mudasir WaniMudasir Wani
Dude in your actual class code it is not there 
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.Name ='TestQuote';//Make it dynamic I am just giving the example

 
syed akramsyed akram
i have changed according to you
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
syed akramsyed akram
AccountId is the api name for Account name
 
Mudasir WaniMudasir Wani
Make it as name instead of accountId 
It is asking Quote name field not account name field
record.Name ='TestQuote';//Make it dynamic I am just giving the example

 
syed akramsyed akram
if change with Name i am getting this error
Error Message System.StringException: Invalid id: a.Id
Stack Trace Class.TestSalesOrder.Dosave: line 18, column 1
syed akramsyed akram
and for your reference this is vfpage
<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>
syed akramsyed akram
   <apex:inputField value="{!record.AccountId}" required="true" />