+ Start a Discussion
syed akramsyed akram 

Error Message System.StringException: Invalid id: q.Id?in test class?how to solve this.

here my Controller class

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 test class

@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 with only mandatory Fields
            insert a;
            Contact c = new Contact(LastName='Test',AccountId=a.Id);  // creating contact with only mandatory Fields
            insert c;
           // creating opportunity with only mandatory Fields
            Opportunity opp = new Opportunity(Name='Test Opp',AccountId=a.Id,StageName='closedwon',CloseDate=Date.Today());
            insert opp;
            // Creating Quote with only mandatory Fields
            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);
     }
}

when i am running my test class i am getting error 
Best Answer chosen by syed akram
Mohit Bansal6Mohit Bansal6
It is not error Syed.....When you are creating order record, you have to add all the mandatory fields like we added AccountID.
 
public pagereference dosave(){
      record.QuoteId = ApexPages.currentPage().getParameters().get('quoteID');
      Quote quoterec = [Select id, Opportunity.accountID from Quote where id=:  record.QuoteId]; 
      record.AccountID = quoterec.Opportunity.accountID;
      record.effectiveDate = System.today().date();
      record.status = 'Add Picklist value which you want to set';

       insert record;
       ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
       pagereference page=new pagereference('/apex/SalesOrder');         
       return page;         
    }

 

All Answers

Mohit Bansal6Mohit Bansal6
Hi Syed

In below line:
Order ord = new Order(AccountId='a.Id',Name='test',status='draft',EffectiveDate=system.today()); 
You are asign string to Account ID. It should be:

Order ord = new Order(AccountId=a.Id,Name='test',status='draft',EffectiveDate=system.today()); 

Kindly replace above line in your code and try.
syed akramsyed akram
now i am getting this error
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 10, column 1
Class.TestSalesOrder.Dosave: line 23, column 1
Mohit Bansal6Mohit Bansal6
Syed

In your class code:

public pagereference dosave(){
      record.QuoteId = ApexPages.currentPage().getParameters().get('quoteID');
       // Check if you have Account Name field under your Order object, and then assign the value to it as well
       insert record;
       ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
       pagereference page=new pagereference('/apex/SalesOrder');         
       return page;         
    }  

Regards
Mohit Bansal


 
syed akramsyed akram
yes in Order object Account name field(Mandatory field) is there.
Mohit Bansal6Mohit Bansal6
Syed

Please add Bold Line in your code and it will work perfectly fine.


public pagereference dosave(){
      record.QuoteId = ApexPages.currentPage().getParameters().get('quoteID');
       // Check if you have Account Name field under your Order object, and then assign the value to it as well
       record."API Name of field" = assign any value;

       insert record;
       ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
       pagereference page=new pagereference('/apex/SalesOrder');         
       return page;         
    }  



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
now i am getting Error Message System.StringException: Invalid id: TestQuote
Stack Trace Class.SalesOrder.dosave: line 9, column 1
Class.TestSalesOrder.Dosave: line 23, column 1
 
syed akramsyed akram
record.AccountId ='TestQuote';

AccountId is the api name.
 and how to bold a line in salesforce?
Mohit Bansal6Mohit Bansal6
public pagereference dosave(){
      record.QuoteId = ApexPages.currentPage().getParameters().get('quoteID');
      Quote quoterec = [Select id, Opportunity.accountID from Quote where id=:  record.QuoteId]; 
      record.AccountID = quoterec.Opportunity.accountID;

       insert record;
       ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
       pagereference page=new pagereference('/apex/SalesOrder');         
       return page;         
    }

Just replace your dosave() function with above code and it will work fine for you.

Regards
Mohit Bansal
syed akramsyed akram
Again It showing Error

Error Message System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [EffectiveDate, Status]: [EffectiveDate, Status]
Stack Trace Class.SalesOrder.dosave: line 22, column 1
Class.TestSalesOrder.Dosave: line 22, column 1
Mohit Bansal6Mohit Bansal6
It is not error Syed.....When you are creating order record, you have to add all the mandatory fields like we added AccountID.
 
public pagereference dosave(){
      record.QuoteId = ApexPages.currentPage().getParameters().get('quoteID');
      Quote quoterec = [Select id, Opportunity.accountID from Quote where id=:  record.QuoteId]; 
      record.AccountID = quoterec.Opportunity.accountID;
      record.effectiveDate = System.today().date();
      record.status = 'Add Picklist value which you want to set';

       insert record;
       ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Record Created Successfully.Thank you!'));
       pagereference page=new pagereference('/apex/SalesOrder');         
       return page;         
    }

 
This was selected as the best answer
Mohit Bansal6Mohit Bansal6
Is it working fine Syed?

Regards
Mohit Bansal
syed akramsyed akram
thanks Mohit It is Working Fine now.Thanks alot.
syed akramsyed akram
can you help me to solve this

https://developer.salesforce.com/forums?state=id#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=OPENQUESTIONS&id=906F0000000BIifIAG