+ Start a Discussion
Kashona DotsonKashona Dotson 

Illegal assignment from List to Id

I'm getting this error from the following second-to-last line
RIM__c.name is an autonumber field; but when I try to run this test it is failing due to a required field not being completed. The RIM_Item.RIM__c field is required and I had it pulling the RIM__c id, then realized that it should be the value from the RIM__c.name field....
 
RIM__c  rtm = new RIM__c ();
rtm.Customer_Name__c ='Test' ; 
rtm.Available_Account_Balance__c = 100 ;
insert rtm ;
    
        
RIM_Item__c item = new RIM_Item__c(); 
item.Item_Purpose__c = 'Test' ; 
item.Transaction_Type__c = 'Check' ;
item.Transaction_Amount_Detail__c = 500 ;
item.RIM__c = [select name from RIM__c where id = :rtm.id] ;
    
insert item ;

 
Best Answer chosen by Kashona Dotson
Raj VakatiRaj Vakati
Add this two lines to test class
controller.setAccounts(new List<RIM_Item__c>{item ,item }) ; 

controller.getRIMitems();

 

All Answers

Raj VakatiRaj Vakati
Modify as like this 
RIM__c  rtm = new RIM__c ();
rtm.Customer_Name__c ='Test' ; 
rtm.Available_Account_Balance__c = 100 ;
insert rtm ;
    
        
RIM_Item__c item = new RIM_Item__c(); 
item.Item_Purpose__c = 'Test' ; 
item.Transaction_Type__c = 'Check' ;
item.Transaction_Amount_Detail__c = 500 ;
item.RIM__c = rtm.Id  ;
    
insert item ;

 
Kashona DotsonKashona Dotson
When I run the test as modified, I get this error
"System.DmlException: Upsert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [RIM__c]: [RIM__c]"
stack trace: "Class.multiAddRIM.save: line 20, column 1
Class.multiAddRIMTest.testcasePositive: line 29, column 1"
Raj VakatiRaj Vakati
Can you share complete code . 
Kashona DotsonKashona Dotson
@isTest
private class multiAddRIMTest{
private static testmethod void testcasePositive(){
    
RIM__c  rtm = new RIM__c ();
rtm.Customer_Name__c ='Test' ; 
rtm.Available_Account_Balance__c = 100 ;
insert rtm ;
    
        
RIM_Item__c item = new RIM_Item__c(); 
item.Item_Purpose__c = 'Test' ; 
item.Transaction_Type__c = 'Check' ;
item.Transaction_Amount_Detail__c = 500 ;
item.RIM__c = rtm.id ;
    
insert item ;

PageReference pageRef = Page.addRIMitems;
        Test.setCurrentPage(pageRef);
      
        multiAddRIM controller = new multiAddRIM();
        ApexPages.currentPage().getParameters().put('Id', rtm.id);
		
controller.reset();
controller.getRIMitems();
controller.add();
    
controller.save();


}

}

Also, I'm trying for 75% coverage and only getting 68
Raj VakatiRaj Vakati
Share your class also . The issue with class not with test class 
Kashona DotsonKashona Dotson
public class multiAddRIM {
    
    List <RIM_Item__c> RIMitemList;
      public Id rID = ApexPages.currentPage().getParameters().get('Id');
      public Id getID {get; set;}
    
    public PageReference reset() {
      RIMitemList = [select name, RIM__c, Transaction_Type__c, Check_Number__c, Transaction_Amount_Detail__c, 
                     Fee_Detail__c, Decision__c from RIM_Item__c where RIM__c =: rID ];
      return null; }
    
    public List <RIM_Item__c> getRIMitems() {
      if(RIMitemList == null) reset();
      return RIMitemList;}
    
    public void setAccounts(List <RIM_Item__c> items) {
      RIMitemList = items;}
    
    public PageReference save() {//upsert records on save
      upsert RIMitemList;
      ApexPages.Message myMsg = new ApexPages.message(ApexPages.Severity.Info, 'Records Saved Successfully'); //show confirmation message on save
      ApexPages.addMessage(myMsg);
      return null;}
    
    public PageReference add() {
      RIMitemList.add(New RIM_Item__c(RIM__c = rID)); //add records to RIM Item and associate with current RIM Record
      return null; }
}

 
Raj VakatiRaj Vakati
public class multiAddRIM {
    
    public List <RIM_Item__c> RIMitemList;
      public Id rID = ApexPages.currentPage().getParameters().get('Id');
      public Id getID {get; set;}
    
    public PageReference reset() {
      RIMitemList = [select name, RIM__c, Transaction_Type__c, Check_Number__c, Transaction_Amount_Detail__c, 
                     Fee_Detail__c, Decision__c from RIM_Item__c where RIM__c =: rID ];
      return null; }
    
    public List <RIM_Item__c> getRIMitems() {
      if(RIMitemList == null) reset();
      return RIMitemList;}
    
    public void setAccounts(List <RIM_Item__c> items) {
      RIMitemList = items;}
    
    public PageReference save() {//upsert records on save
      If(RIMitemList.size()>0){
	  upsert RIMitemList;
	  }
	  
      ApexPages.Message myMsg = new ApexPages.message(ApexPages.Severity.Info, 'Records Saved Successfully'); //show confirmation message on save
      ApexPages.addMessage(myMsg);
      return null;}
    
    public PageReference add() {
      RIMitemList.add(New RIM_Item__c(RIM__c = rID)); //add records to RIM Item and associate with current RIM Record
      return null; }
}



@isTest
private class multiAddRIMTest{
private static testmethod void testcasePositive(){

RIM__c  rtm = new RIM__c ();
rtm.Customer_Name__c ='Test' ; 
rtm.Available_Account_Balance__c = 100 ;
insert rtm ;

	
RIM_Item__c item = new RIM_Item__c(); 
item.Item_Purpose__c = 'Test' ; 
item.Transaction_Type__c = 'Check' ;
item.Transaction_Amount_Detail__c = 500 ;
item.RIM__c = rtm.id ;

insert item ;

PageReference pageRef = Page.addRIMitems;
	Test.setCurrentPage(pageRef);
  
	multiAddRIM controller = new multiAddRIM();
	ApexPages.currentPage().getParameters().put('Id', rtm.id);
	

controller.save();
controller.add();
controller.reset();
 

}

}

 
HARSHIL U PARIKHHARSHIL U PARIKH
Try to make following changes if applicable,

 initialize the List <RIM_Item__c> RIMitemList at line 3 on Class
List <RIM_Item__c> RIMitemList = New List<RIM_Item__c>();

 
Kashona DotsonKashona Dotson
I used both of your comments and the test succeeded! However, I'm down to 66% coverage now :(
Raj VakatiRaj Vakati
Add this two lines to test class
controller.setAccounts(new List<RIM_Item__c>{item ,item }) ; 

controller.getRIMitems();

 
This was selected as the best answer
HARSHIL U PARIKHHARSHIL U PARIKH
Which line of the code is not covered in class?
Kashona DotsonKashona Dotson
That drops me down to 47% and error "System.ListException: Before Insert or Upsert list must not have two identically equal elements"
Kashona DotsonKashona Dotson
Lines 5-10; lines 24-30
 
public class multiAddRIM {
    
    List <RIM_Item__c> RIMitemList = New List<RIM_Item__c>();
      public Id rID = ApexPages.currentPage().getParameters().get('Id');
      public Id getID {get; set;}
    
    public PageReference reset() {
      RIMitemList = [select name, RIM__c, Transaction_Type__c, Check_Number__c, Transaction_Amount_Detail__c, 
                     Fee_Detail__c, Decision__c from RIM_Item__c where RIM__c =: rID ];
      return null; }
    
    public List <RIM_Item__c> getRIMitems() {
      if(RIMitemList == null) reset();
      return RIMitemList;}
    
    public void setAccounts(List <RIM_Item__c> items) {
      RIMitemList = items;}
    
    public PageReference save() {//upsert records on save
      If(RIMitemList.size()>0){
	  upsert RIMitemList;
	  }
	  
      ApexPages.Message myMsg = new ApexPages.message(ApexPages.Severity.Info, 'Records Saved Successfully'); //show confirmation message on save
      ApexPages.addMessage(myMsg);
      return null;}
    
    public PageReference add() {
      RIMitemList.add(New RIM_Item__c(RIM__c = rID)); //add records to RIM Item and associate with current RIM Record
      return null; }
}

 
Kashona DotsonKashona Dotson
Test was successful, coverage is now 95%

THANK YOU SO MUCH!

It started out so simple and then the test requirement threw me off.