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
Egor PalatovEgor Palatov 

Master-detail assignment

Hi, i have  a master-detail relation ship between Car__c(master) and Car_detail__c(detail) and i need a  picklist to choose to every record it's car__c(relationsheep) field.

i tried this code but have " A value cannot be stored to Car__c in type Car_Detail__c" problem 

public List<SelectOption> getCar(){ 
	List<SelectOption> options = new List<SelectOption>(); 
	List<Car__c> carList = new List<Car__c>(); 
	carList = [Select Id, Name FROM Car__c]; 
	options.add(new SelectOption('--None--','--None--'));
    
	for (Car__c cars : carList) 
{ 
	options.add(new SelectOption(cars.Id,cars.Name));
    Car_detail__c.Car__c = cars.Id;    
}          
	return options; 
}
Ginny MahantGinny Mahant
Hi,

    You need to query your Car_detail__c record first. On VF page, to the record of Car_detail__c object add the picklist(dropdown) containing all Car_c records and select the related parent car record in dropdown(not set the M-D relationship field as in code above)

    If you are displaying multiple Car_detail__c records then you can use a list of wrapper (wrapper class object containing these members - Car_detail__c ,List<SelectOption> ).

   Hope this helps. If you could explain the use case a bit more in detail like what is your VF page displaying (I am assuming you a VF page for this) , I will be able to help you out better.