I'm trying to insert a record but it is displaying this error: Invalid id: Name of record . can anyone check it?


public class NightFlexList2Con {
 public string movieid{set; get;}
public string MovieAndWatchlist{set; get;}
public list<Nightflex__c>moviepopup{set;get;}

 public pagereference addmovie(){ 
    movieid = apexpages.currentPage().getParameters().get('id');
    moviepopup = [select id,name from nightflex__c limit 5]; 
    return null; 
public List<SelectOption> getWatchlist() {
    List<SelectOption> options = new List<SelectOption>();
    List<Watchlist__c>Watch_List = [Select Name from Watchlist__c];
    options.add(new SelectOption('','--Select a Watchlist-- '));   
    for(Watchlist__c WL: Watch_List)
    options.add(new SelectOption(WL.ID, WL.Name));    
    return options;

public PageReference doUpsert(){
    Movie_Watchlist__c objdlt = new Movie_Watchlist__c();
    objdlt.Movie__c = movieid;
    objdlt.Watchlist__c = MovieAndWatchlist;
    insert objdlt;
    return Move();

VF page

 <h3>Movie Title: {!movieid}</h3>
<apex:selectList multiselect="false" size="1" onchange="Select(this.value);">
                    <apex:SelectOptions value="{!Watchlist}"/>
<apex:commandbutton >
                <apex:commandButton value="Add Movie" action="{!doUpsert}">

It appears that movieid contains a String value:

 <h3>Movie Title: {!movieid}</h3>

It's not clear from the supplied code whether MovieAndWatchList is a String or an Id value; it is typed as a String. You may wish to consider binding your <apex:selectList>'s value to a property in your controller to be clear; I don't know what your JavaScript method is doing.

You're using both variables to populate what appear to be relationship fields:

    objdlt.Movie__c = movieid;
    objdlt.Watchlist__c = MovieAndWatchlist;
    insert objdlt;

You must populate an Id value in a relationship field.

  • sorry i am new in salesforce. can you show me an example where I can insert or update records from multiple parents in the child object? cos that's what I am trying to do. – xoxo4yaps Jan 13 at 16:19
  • I would suggest completing the Apex Basics and Database module on Trailhead, which covers this topic. – David Reed Jan 13 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.