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
asish1989asish1989 

Testmethod issue

Hi

  I have an apex class which is all about an Opportunity and Quote and Product informations.

 I mean ...Contact--->Opportunity---->Quote-->QuoteLineitem-- Product

 I have written a test method ... I have entered an record of Opportunity and it is already inserted . I have checked bu debur log... But still I am facing an exception...

  System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name, StageName, CloseDate]: [Name, StageName, CloseDate]

 

This is my class......

 

   

public class Pcontactdetailcontroller {

//All Getter and Setter Properties.....

public Contact contact {get;set;}
public List< OpportunityContactRole> optcList{get;set;}

public String visible {get;set;}
public String Enable{get;set;}
public OpportunityContactRole optc {get;set;}
public Opportunity opt{get;set;}
public Quote quote{get;set;}
public String opptId{get;set;}
public String table{get;set;}
public String asish{get;set;}
public String block{get;set;}
public Id optId{get;set;}
public Id Id{get;set;}
public Id quoteId{get;set;}
public Id lineitemId{get;set;}
public List<Quote> quotelist{get;set;}
public List<Opportunity> optlist{get;set;}
public PricebookEntry pbentry{get;set;}
public Pricebook2 pb{get;set;}
public Product2 product{get;set;}
public QuoteLineItem qutlineitem{get;set;}
public QuoteLineItem qutlineitem1{get;set;}
public Quote quote1{get;set;}
public Opportunity opt1{get;set;}


//constructor

public Pcontactdetailcontroller() {

Id = ApexPages.currentPage().getParameters().get('Id');
contact=[select Id,FirstName,LastName, name,Account.Name,MailingStreet,MailingState,MailingCity,MobilePhone,Email from Contact where Id=:Id];

optc=new OpportunityContactRole();
opt=new Opportunity();
opt1=new Opportunity();
quote=new Quote();
pb= new Pricebook2();
pbentry= new PricebookEntry(UseStandardPrice=true,UnitPrice=0.00);
product=new Product2();
qutlineitem=new QuoteLineItem();
qutlineitem1=new QuoteLineItem();
quote1 = new Quote();
}



//All getter method

public OpportunityContactRole getOpportunityContactRole() {

return optc;
}

public Opportunity getOpportunity() {

return opt;
}


public Opportunity getOpportunity1() {

return opt1;
}

public Quote getQuote() {

return quote;
}

public Contact getContact() {

return contact;
}

public Product2 getProduct2() {

return product;
}
public Pricebook2 getPricebook2() {

return pb;
}
public PricebookEntry getPricebookEntry() {

return pbentry;
}
public QuoteLineItem getQuoteLineItem() {

return qutlineitem;
}

public QuoteLineItem getQuoteLineItem1() {

return qutlineitem1;
}
public Quote getQuote1() {

return quote1;
}

//Editin Contact Details......

public PageReference tochange() {

Id Id = ApexPages.currentPage().getParameters().get('Id');
Enable='edit2';
System.debug('##############visible###############'+visible);
return null;

}

//Saving contact details
public PageReference toupdatecontact() {

Id Id = ApexPages.currentPage().getParameters().get('Id');
PageReference samepage=new PageReference('/apex/pcontactdetail?id='+id);
samepage.setRedirect(true);
update contact;
Enable=null;
return null;
}


//opportunity save

public PageReference tosaveOpportunity() {

opt.AccountId = contact.AccountId;
insert opt;
optc.OpportunityId=opt.id;
optc.ContactId=contact.id;
insert optc;
visible=null;
PageReference samepage= new PageReference('/apex/pcontactdetail?id='+id);
samepage.setRedirect(true);
return samepage;
}

 

Error is on insert opt  line....

 

My test method is.....

 

  

private static testmethod void t1() {
Id Id;
test.startTest();
Account act = new Account (Name = 'xyt');
insert act;
Contact con = new Contact( LastName = 'cghj' , AccountId = act.id);
insert con;
// Date myDate = date.newinstance(2018, 2, 17);

Opportunity opttest = new Opportunity(AccountId = act.Id,Name = 'test' , StageName = 'Closed Won', CloseDate = System.today() );
insert opttest;
ApexPages.currentPage().getParameters().put('Id',con.id);

System.debug('#########################OPPORTUNITY################'+opttest.Name);
System.debug('#########################OPPORTUNITY################'+opttest.AccountId);
System.debug('#########################OPPORTUNITY################'+opttest.CloseDate );
System.debug('#########################OPPORTUNITY################'+opttest.StageName );
OpportunityContactRole optctest = new OpportunityContactRole(ContactId = con.id , OpportunityId = opttest.id );
insert optctest ;

............

}

 

So wnat will I do ... Anyone have any suggession please help me... Its urgent...

 

 

Thanks

asish

 

 

 

yvk431yvk431

please paste your complete test method, by the looks of it I guess you should assign values to optc.Name, optc.Closedate before you call the method tosaveOpportunity()

 

 

--yvk

 

 

 

 

Praveen_K.ax1208Praveen_K.ax1208

Hi Ashish,

 

Also just verify the complete error message, you will have the line number at which insert statement you are getting the error.

If you get a chance just paste the complete error message and mark the insert statement at which line you are getting an error if possible.

 

Thanks,

Praveen K.

asish1989asish1989

Hi 

 This is my complete class.....

 

    public class Pcontactdetailcontroller {


       
       
   //constructor
   
       public Pcontactdetailcontroller() {
       
            Id = ApexPages.currentPage().getParameters().get('Id');
            contact=[select Id,FirstName,LastName, name,Account.Name,MailingStreet,MailingState,MailingCity,MobilePhone,Email from Contact where Id=:Id];
   
            optc=new OpportunityContactRole();
            opt=new Opportunity();
            opt1=new Opportunity();
            quote=new Quote();
            pb= new Pricebook2();
            pbentry= new PricebookEntry(UseStandardPrice=true,UnitPrice=0.00);
            product=new Product2();
            qutlineitem=new QuoteLineItem();
            qutlineitem1=new QuoteLineItem();
            quote1 = new Quote();
            
           }
     
     
      
       
      /
          
          //opportunity save
    
      public PageReference tosaveOpportunity() {
  
            opt.AccountId = contact.AccountId;
            insert opt;
            optc.OpportunityId=opt.id;
            optc.ContactId=contact.id;
            insert optc;
            visible=null;
            PageReference samepage= new PageReference('/apex/pcontactdetail?id='+id);
            samepage.setRedirect(true);
            return samepage;
          }
               
       //Adding Quote in a perticular Opportunity.....
        
       public PageReference toAddQuote() {
       
           optId = ApexPages.currentPage().getParameters().get('optId');
              visible='Step2';
              return null;
          }
         
        
        
     //Showing all opportunity of a perticular contact in a datatable
        
       public List<OpportunityContactRole> getOpportunityContactRoles() {
       
             optcList=[select Id,ContactId,OpportunityId,Opportunity.CloseDate,Opportunity.Name,Opportunity.OrderNumber__c from OpportunityContactRole  where ContactId=:Id order by createdDate desc];
              return optcList;
          }
         
          
     //Redirecting same page and opening Opportunity  section after clicking Add Opportunity  buttom  
     
       public PageReference forOpportunity() {
              visible='Step1';
              return null;
           }
       
    //Redirecting Listcontact  page 
    
       public PageReference golistcontact() {
              return Page.listcontact;
           }
    
    //saving Quote     
         
       public PageReference tosaveQuote() {
       
             quote.BillingName=contact.Account.Name;
             quote.ShippingName=contact.Account.Name;
             quote.ContactId =contact.Id; 
             quote.Phone=contact.MobilePhone;
             quote.Email= contact.Email;
             quote.OpportunityId=optId;
             insert quote;
             PageReference samepage= new PageReference('/apex/pcontactdetail?id='+id);
             samepage.setRedirect(true);
             visible=null;
             return samepage;
           } 
       
      //Adding Product In a perticular Quote.
         
         public PageReference toAddProduct2() {
         
             quoteId = ApexPages.currentPage().getParameters().get('quoteId');
             quote= [select Id,Name,GrandTotal,TotalPrice,Tax,Status,QuoteNumber,Pricebook2Id,BillingName,ShippingName,Description,(select Id,PricebookEntry.Product2.Name,UnitPrice,Quantity,Subtotal,Discount,TotalPrice From QuoteLineItems order by createdDate desc) from Quote where Id=:quoteId];
             visible='Step4';
             table=null;
             asish=null;
             return null;
           }
          // deleting product.      
          public PageReference todelproduct() {       
                 
              lineitemId = ApexPages.currentPage().getParameters().get('lineitemId');
              qutlineitem1 = [select Id,PricebookEntry.Product2.Name,UnitPrice,Quantity,Subtotal,Discount,TotalPrice From QuoteLineItem where Id=:lineitemId];
              delete qutlineitem1;
              Id Id = ApexPages.currentPage().getParameters().get('Id');
              PageReference samepage= new PageReference('/apex/pcontactdetail?id='+id);
              samepage.setRedirect(true);
              return  samepage;
              
            }
              
            // deleting Quote.    
          public PageReference todelQuote() { 
          
             quoteId = ApexPages.currentPage().getParameters().get('quoteId');
             quote1= [select Id,Name,GrandTotal,TotalPrice,Tax,Status,QuoteNumber,Pricebook2Id,BillingName,ShippingName,Description,(select Id,PricebookEntry.Product2.Name,UnitPrice,Quantity,Subtotal,Discount,TotalPrice From QuoteLineItems order by createdDate desc) from Quote where Id=:quoteId];
             delete quote1;
             PageReference samepage= new PageReference('/apex/pcontactdetail?id='+id);
             samepage.setRedirect(true);
             return  samepage;
            }
             
         public PageReference todelOpp() {   
         
             optId = ApexPages.currentPage().getParameters().get('optId');
             opt1=[select Id,Name,CloseDate,(select Id,Quote.Name,Quote.Status,Quote.QuoteNumber,Quote.ExpirationDate From Quotes order by createdDate desc) from Opportunity where Id=:optId];
             delete opt1;
             PageReference samepage= new PageReference('/apex/pcontactdetail?id='+id);
             samepage.setRedirect(true);
             return  samepage;
            }
             
             
            
       
    //saving Product   
    
    
         public PageReference tosaveProduct() {
 
             insert product;
             pbentry.Product2Id=product.Id;
             
             if (quote.Pricebook2Id==null){
             
                    pb.Name = 'Standard';
                    insert pb;
                    pbentry.Pricebook2Id=pb.Id;
                    insert pbentry;
                    quote.Pricebook2Id=pb.Id;
                    update quote;
                    qutlineitem.PricebookEntryId = pbentry.Id;
                    qutlineitem.quoteId=quoteId;
                    insert qutlineitem;
                  }
                   
             else 
                  {  
             
                   pbentry.Pricebook2Id=quote.Pricebook2Id;
                   insert pbentry;
                   qutlineitem.PricebookEntryId = pbentry.Id;
                   qutlineitem.QuoteId=quoteId;
                   insert qutlineitem;
                  }
               visible=null;
               Id Id = ApexPages.currentPage().getParameters().get('Id');
               PageReference samepage= new PageReference('/apex/pcontactdetail?id='+id);
               samepage.setRedirect(true);
               return  samepage;
          }
   
   // Saving Product by Save And New buttom
   
          public PageReference toaddmoreProduct() {
    
             insert product;
             pbentry.Product2Id=product.Id;
             
             if(quote.Pricebook2Id==null) {
                    pb.Name = 'Standard';
                    insert pb;
                    pbentry.Pricebook2Id=pb.Id;
                    insert pbentry;
                    quote.Pricebook2Id=pb.Id;
                    update quote;
                    qutlineitem.PricebookEntryId = pbentry.Id;
                    qutlineitem.quoteId=quote.Id;
                    insert qutlineitem;
       
                  }
                 
             else  {
                   pbentry.Pricebook2Id=quote.Pricebook2Id;
                   insert pbentry;
                   qutlineitem.PricebookEntryId = pbentry.Id;
                   qutlineitem.QuoteId=quote.Id;
                   insert qutlineitem;
                 
                  }
          
               pbentry= new PricebookEntry(UseStandardPrice=true,UnitPrice=0.00);
               product=new Product2();
               qutlineitem=new QuoteLineItem();
               visible='Step4';
               return null;
   
             }
   
  
//Showing All Quote of a perticular Opportunity in a datatable
  
          public PageReference forQuote() {
   
              optId = ApexPages.currentPage().getParameters().get('optId');
              opt=[select Id,Name,CloseDate,(select Id,Quote.Name,Quote.Status,Quote.QuoteNumber,Quote.ExpirationDate From Quotes order by createdDate desc) from Opportunity where Id=:optId];
              PageReference samepage= new PageReference('/apex/pcontactdetail?id='+id);
              table = 'Show'; 
              asish=null;
              return samepage;
  
            }
      
      
   //Showing All Product of a particular Quote in a datatable
   
          public PageReference forProduct() {
    
              quoteId = ApexPages.currentPage().getParameters().get('quoteId');
              quote= [select Id,Name,GrandTotal, TotalPrice,Tax,QuoteNumber,Pricebook2Id,BillingName,ShippingName,Description,(select Id,PricebookEntry.Product2.Name,UnitPrice,Quantity,Subtotal,Discount,TotalPrice From QuoteLineItems order by createdDate desc) from Quote where Id=:quoteId];
              table = null;
              asish = 'Show';
              return null;
            }

   
   
  //Opening next page Block for preview.    
       
         public PageReference forOrder() {
              block = 'invoice';
               return null;
             }  
      
    
  
            

 

}

 

 

 

Now my complete test method is....

 

 // test method
           
           private static testmethod void t1() {
               Id Id;
               test.startTest();
               Account act = new Account (Name = 'xyt');
               insert act;
               Contact con = new Contact( LastName = 'cghj' , AccountId = act.id);
               insert con;
               // Date myDate =  date.newinstance(2018, 2, 17);
       
               Opportunity opttest = new Opportunity(AccountId = act.Id,Name = 'test' , StageName = 'Closed Won', CloseDate = System.today()    );
               insert opttest;
                ApexPages.currentPage().getParameters().put('Id',con.id);
               
               System.debug('#########################OPPORTUNITY################'+opttest.Name);
               System.debug('#########################OPPORTUNITY################'+opttest.AccountId);
               System.debug('#########################OPPORTUNITY################'+opttest.CloseDate );
               System.debug('#########################OPPORTUNITY################'+opttest.StageName );
               OpportunityContactRole  optctest = new OpportunityContactRole(ContactId = con.id , OpportunityId = opttest.id );
               insert optctest ;
               Product2 prdcttest = new Product2(Name= 'prdtest');
               insert prdcttest;
               PriceBook2 pbtest = new PriceBook2(Name = 'Standard');
               insert pbtest;
               PricebookEntry pbentrytest = new PricebookEntry(UseStandardPrice=true,UnitPrice=0.00, Product2Id = prdcttest.id ,IsActive = true , Pricebook2Id = pbtest.id);
               insert pbentrytest ;
               Quote quttest = new Quote (Name = 'qoutetest' , OpportunityId = opttest.id , Pricebook2Id = pbtest.id );
               insert quttest ; 
               QuoteLineItem qutlineitemtest = new QuoteLineItem(QuoteId = quttest .id , Quantity = 3.00 , UnitPrice = 12 , PricebookEntryId = pbentrytest .id);
               insert qutlineitemtest ;
               
               Pcontactdetailcontroller ptest = new Pcontactdetailcontroller();
               ptest.tochange();
               ptest.toupdatecontact(); 
               ptest.getOpportunity();
            ptest.tosaveOpportunity();
                
               ptest.tosaveQuote();
               ptest.toAddQuote(); 
               ptest.getOpportunityContactRoles();
              
               }
    

Error details...

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name, StageName, CloseDate]: [Name, StageName, CloseDate]

 

stack trace:..

 

Class.Pcontactdetailcontroller.tosaveOpportunity: line 135, column 1 Class.Pcontactdetailcontroller.t1: line 438, column 1

 

I have provided all the information...I am waiting  for your response....

 

Thanks

asish

yvk431yvk431

My guess is correct then , you should have tried it before any ways

add the statements before your highlighted statement in the test method ptest.tosaveOpportunity();

 

ptest.optc.Name = 'test oppty';

ptest.optc.Closedate = System.Today();

 

 

--yvk

asish1989asish1989

Hi

   I have written the statement before highliting

 

  Opportunity opttest = new Opportunity(AccountId = act.Id,Name = 'test' , StageName = 'Closed Won', CloseDate = System.today()    );     

    insert opttest;

 

By this line all required field value is provided to Opportunity...

when I call to saveOpportunity () method I am getting same error

 

Please help me.... as soon s possible...

 

 

 

Thanks

asish

Praveen_K.ax1208Praveen_K.ax1208

Hi Asish,

 

remove those two lines and replave with the below code,

 

Opportunity opttest = new Opportunity();

opttest.Accountid=act.id;

opttest.Name='test';

opttest.StageName='Closed Won';

opttest.ClosedDate=System.Today();

Insert opttest;

 

try this and let me know if it works.

 

Thanks,

Praveen K.

yvk431yvk431

asish, i didnt mentioned to insert the opportunity , the dml error you got is not from the test method its in your actual controller.

 

 place the below statements and check

 

ptest.optc.Name = 'test oppty';

ptest.optc.Closedate = System.Today();

 

 

--yvk

Praveen_K.ax1208Praveen_K.ax1208

//opportunity save

public PageReference tosaveOpportunity() {

opt.AccountId = contact.AccountId;

opt.Name = 'test';

opt.StageName = 'Closed Won';

opt.ClosedDate = System.today();

insert opt;
optc.OpportunityId=opt.id;
optc.ContactId=contact.id;
insert optc;
visible=null;
PageReference samepage= new PageReference('/apex/pcontactdetail?id='+id);
samepage.setRedirect(true);
return samepage;
}

 

 

in the above code u missed the required fields name and stagename and closedate.

just verify the above marked code and assign those values.

 

Hope this will work. if it works for you please mark it as a solution, so that others can also be benefited.

 

Thanks,

Praveen K.

yvk431yvk431

My bad, got the instance variales wrong use the below

 

ptest.opt.AccountId = contact.AccountId;

ptest.opt.Name = 'test';


Praveen_K please understand that we wont give static values in a controller , the values will get assigned from a visual force page, thats why they are not there and which is why we need to pass them in a test method.

 

 

--yvk

 

 

 


asish1989asish1989

Hi Praveen

  All the other value StageName, Closedate, Name  are inserted from Page . If I will write your code then it will fixed that My Opportunity Name is 'test'. what will I do If I will have to create new Opportunity... I have  to change code.... it is not a good pratice.So any other suggession    ?

 please help me as soon as possible

 

 

Thanks

asish

asish1989asish1989

Hi Yvk

  I treid what you suggested . , But I am facing an error..


Error: Compile Error: Illegal assignment from Schema.SObjectField to Id

line -  ptest.opt.AccountId = contact.AccountId;

 

 

 

 

 

 

 

Thanks

asish