+ Start a Discussion
AKallAKall 

Conversion Error Setting Values

First let me begin by stating that I am a beginner. Apex is the first language I have tried to learn, and I have only been trying for a few months.

I am trying to create a controller class for a page that allows the user to create records of packages that were sent via mail. When creating a new mail package the user will need to be able to select from the multiple contracts that could be included in the package. I want to override the save button so that when a new package is saved the files that are included in the package are automatically created in a Mailed Package File object. I know that my save button is working because it works if I do not select any files for the package. However, if I try to select one or more files I receive the error "Conversion Error Setting Values."  This confuses me because I thought I was working with IDs. I tried to convert the data type to string but received the same error. Any help would be much appreciated
Code:
public class Mailed_Package_Controller {
 
 public Mailed_Package_Controller(ApexPages.StandardController Controller){
  this.MP = (MailedPackage__c)controller.getRecord();
 }
  
 
 public MailedPackage__c MP;
 
 public List<ID> selectedFiles {get;set;}
 
 public List<SelectOption> getFileOptions(){
  List<SelectOption> Filefeed = new List<SelectOption>();
  File__c[] A = [select ID, Name from File__c Order By LastModifiedDate];
  for (File__c f : A){
   Filefeed.add(new SelectOption (f.ID, f.Name));
  }  
  return Filefeed;
  }
 
   
 /* next you need a save method that overrides the default save method
 it needs to create the Mailer Package
 retrieve the id of the mailer package 
 then iterate through the list of selected file IDs
 and for each file ID create a !!! mailed Package File record !!!
 where the package ID is that of the mailer package
 and the file ID is the current ID in the list of selected file IDs
 */
   
  
 public List<MailedPackageFile__c> MPFs = new List <MailedPackageFile__c>();
 
 public void newMPF(){
  MailedPackageFile__c MPF = new MailedPackageFile__c();
  for (ID c : selectedFiles){
   MPF.File__c = c;
   MPF.MailedPackage__c = this.MP.Id;
   MPFs.add(MPF);
  }
 }
   
 public PageReference save(){
  try { 
   insert this.MP;
  } catch (Exception e){}
   
  newMPF();
  insert MPFs;
  PageReference MPpage = new PageReference('/'+MP.ID);
  MPpage.setRedirect(true);
  return MPpage; 
    
 }
 
 
}

 

Best Answer chosen by Admin (Salesforce Developers) 
CaroReineckeCaroReinecke
I received a similar error. Turns out that I did not declare the variable correctly in my class. Insert the following line in your class to fix your conversion error.
Code:
List<ID> selectedFiles = new List<ID>();

 

All Answers

AKallAKall
I have now figured out that the file ids that are being assigned to the selectedFiles variable are not being delimited.
For example, if three files are chosen in the VF page I get the following error:
Conversion Error setting value 'a0dR0000000EfxvIAC a0dR0000000Efy0IAC a0dR0000000EiLFIA0' for '#{selectedFiles}'.

All three of those ideas are lumped together within the same quotations. How can I get those IDs to parse seperately?
CaroReineckeCaroReinecke
I received a similar error. Turns out that I did not declare the variable correctly in my class. Insert the following line in your class to fix your conversion error.
Code:
List<ID> selectedFiles = new List<ID>();

 
This was selected as the best answer
aamDevaamDev

I'm running across a similar issue and adding the suggested code to declare the variable gives me a "Duplicate Variable" error in Eclipse.  Here's the pertinent piece of the controller:

 

List<Id> selectedNumbers = new List<Id>();
	
public List<Id> selectedNumbers { get; set; }

 I could really use somebody's help, I'm extremely frustrated by this conversion error. Thanks in advance.

Keith987Keith987

Use the normal lazy initialization pattern:

public List<Id> selectedNumbers {
    get {
        if (selectedNumbers == null) {
            selectedNumbers = new List<Id>();
        }
        return selectedNumbers;
    }
    set;
}