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
NervosaNervosa 

REQUIRED_FIELD_MISSING error

Greetings to everyone!

Here is a question. My APEX controller:

 

public class Fullfunctionality_2 {
 
   private List<Item__c> items;
   private String sortDirection = 'ASC';
   private String sortExp = 'name';
   private String searchStr = ' order by ';
   public Date searchDateCopy;
   public String searchTextCopy;
   public Boolean EndSearch { get; set; }
   public Boolean DelCopy { get; set; }
   public Boolean MakeCopy { get; set; }
   public String NewItemName {get; set;}
   public Integer NewItemQuantity { get; set; }
   public Integer NewItemPrice { get; set; }
   public String NewItemType { get; set; }
   public Date NewItemReleaseDate { get; set; }
   public String IdToDel { get; set; }
   public String searchText {get;set;}
   public Date searchDate { get; set; }
   public List<Item__c> searchResults {get;set;}
   
   public fullfunctionality_2() {

       ViewData();
   }

   public List<SelectOption> getTypes(){
       List<SelectOption> types = new List<SelectOption>();
       Schema.DescribeFieldResult fieldResult = Item__c.Item_Type__c.getDescribe();
       List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
       for( Schema.PicklistEntry f : ple)
           {
              types.add(new SelectOption(f.getLabel(), f.getValue()));
           }       
       return types;
    }
    
   public PageReference add(){

        Item__c NewItem = new Item__c(            
                Name = NewItemName,
                Item_Price__c = NewItemPrice,
                Items_Available__c = NewItemQuantity,
                Item_Type__c = NewItemType,
                Release_Date__c = NewItemReleaseDate
                );
                insert NewItem; 
                ViewData();
                PageReference curPage = ApexPages.currentPage(); 
                curPage.getParameters().put('success','true');
                curPage.getParameters().put('id',Apexpages.currentPage().getParameters().get('id'));
                curPage.setRedirect(true);
                NewItemName=null;
                NewItemQuantity=null;
                NewItemPrice = null;
                NewItemReleaseDate = null;
                return null; 
    }     
   public PageReference del() {

        Item__c ItemToDel = [SELECT id
                             FROM Item__c
                             WHERE id = :IdToDel];
        delete ItemToDel;   
               
        PageReference curPage = ApexPages.currentPage(); 
        curPage.getParameters().put('success','true');
        curPage.getParameters().put('id',Apexpages.currentPage().getParameters().get('id'));
        ViewData();     
        return curPage;          
    }    

   public String sortExpression
   {
     get
     {
        return sortExp;
     }
     set
     {
       if (value == sortExp)
         sortDirection = (sortDirection == 'ASC')? 'DESC' : 'ASC';
       else
         sortDirection = 'ASC';
       sortExp = value;
     }
   }

 public String getSortDirection()
 {
    if (sortExpression == null || sortExpression == '')
      return 'ASC';
    else
     return sortDirection;
 }

 public void setSortDirection(String value)
 {  
   sortDirection = value;
 }
  
   public List<Item__c> getItems() {
       return items;
   }

   public PageReference ViewData() {
       if (DelCopy == true){
           searchDateCopy = null;
           searchTextCopy = null;}

       if (MakeCopy == true) {
           searchDateCopy = searchDate;
           searchTextCopy = searchText;
       }    
       string sortFullExp = sortExpression  + ' ' + sortDirection;
       if(searchTextCopy != null) {
           searchstr = 'WHERE Name LIKE \'%' + searchTextCopy + '%\' order by ';}
       if(searchDateCopy != null) {
           searchstr = 'WHERE Name LIKE \'%' + searchTextCopy + '%\' and DAY_ONLY(CreatedDate) =:searchdatecopy order by ';}   
           
       items = Database.query('Select id, Name, Item_Price__c, CreatedDate from Item__c ' + searchstr + sortFullExp);
       

           searchText = null;
           searchDate = null;

       return null;
   }
}

 And here is a test class for controller:

 

@isTest
private class TestFullFunctionality_2 {

String NewItemName = 'NEWITEM';
Integer NewItemPrice = 100;
String NewItemType = 'Solid';
Integer NewItemQuantity = 10;

static testMethod void testAdd() {
TestFullFunctionality_2 ext;       
Item__c NewItem = new Item__c(name = 'NEWITEM', Item_Price__c = 100, Item_Type__c = 'Solid', Items_Available__c = 10 );
insert NewItem;
FullFunctionality_2 Con=new FullFunctionality_2();
Con.add();
Con.del();
Con.viewData();

}
}

 I still cannot understand - why do i get an error:

 

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

 Maybe someone can help me?

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
Janet GuoJanet Guo

I think it's because you aren't actually passing any values to the NewItem in the controller when you simply have:

Item__c NewItem = new Item__c(name = 'NEWITEM', Item_Price__c = 100, Item_Type__c = 'Solid', Items_Available__c = 10 );

in the test class. That's merely inserting an Item__c record directly into the database.

 

I take it you want that NewItem to be passed your VF controller, but right now, you aren't. This is probably what you want:

@isTest
private class TestFullFunctionality_2 {
	FullFunctionality_2 con = new FullFunctionality_2();
	// now pass values into the variables you have for the VF controller
	con.NewItemName = 'NEWITEM';
	con.NewItemQuantity = 10;
	con.NewItemPrice = 100;
	con.NewItemType = 'Solid';
	con.add();
	con.del();
	con.viewData();
}

 

Hope that helps!

All Answers

Coco_SdyneyCoco_Sdyney

FullFunctionality_2 Con=new FullFunctionality_2();
Con.add();

 

when you call add(), NewItemPrice is still null, then fail in below

Item__c NewItem = new Item__c(            
                Name = NewItemName,
                Item_Price__c = NewItemPrice,
                Items_Available__c = NewItemQuantity,
                Item_Type__c = NewItemType,
                Release_Date__c = NewItemReleaseDate
                );
                insert NewItem; 
Janet GuoJanet Guo

I think it's because you aren't actually passing any values to the NewItem in the controller when you simply have:

Item__c NewItem = new Item__c(name = 'NEWITEM', Item_Price__c = 100, Item_Type__c = 'Solid', Items_Available__c = 10 );

in the test class. That's merely inserting an Item__c record directly into the database.

 

I take it you want that NewItem to be passed your VF controller, but right now, you aren't. This is probably what you want:

@isTest
private class TestFullFunctionality_2 {
	FullFunctionality_2 con = new FullFunctionality_2();
	// now pass values into the variables you have for the VF controller
	con.NewItemName = 'NEWITEM';
	con.NewItemQuantity = 10;
	con.NewItemPrice = 100;
	con.NewItemType = 'Solid';
	con.add();
	con.del();
	con.viewData();
}

 

Hope that helps!

This was selected as the best answer
NervosaNervosa

Janet, thank you very much, it works!

 

But there is one more question - how do i get an ID of this NEWITEM ??? I need it to perform deletion by del() method.

NervosaNervosa

Finally I've done this!

 

@isTest
private class TestFullFunctionality_2 {
static testMethod void testAdd(){
    FullFunctionality_2 con = new FullFunctionality_2();
    // now pass values into the variables you have for the VF controller
    con.NewItemName = 'NEWITEM';
    con.NewItemQuantity = 10;
    con.NewItemPrice = 100;
    con.NewItemType = 'Solid';
    con.add();
    con.IdToDel = [SELECT ID FROM Item__c WHERE name = 'NEWITEM'].ID;
    con.del();
    con.viewData();
    }
}

 

Janet GuoJanet Guo

Hi Nervosa,

 

I'm glad I was able to help. 

 

As for getting an ID, your method is one way. However, I'd really advise against querying unless you really have to (though technically, in a unit test, it doesn't matter quite as much, but it's still good practice). In your case, you don't actually have to do any querying because you can get the ID of an item you've created for insertion after it has been inserted.

 

So, inside your add() method, it might look something like this

public PageReference add(){
	//...chunk of code
	
	insert NewItem;
	// get the newly inserted Item__c ID
	IdToDel = NewItem.Id;
	
	//...rest of code
}

 That way, you can get rid of the query altogether. Also, you know you won't get the wrong record by mistake (what if there is now than one record name "NEWITEM?" etc.)

 

Hope that helps!

Janet