+ Start a Discussion
DevmenDevmen 

add records from lookup and display in visualforce page

 Hi,
im creating a record in object (Sample) using visualforce page.There is a field name testname which is a lookup to an another object named (Test).Im trying to add record from lookup and display in VF page. Its is displaying Id not name.When i add more records it is adding same record IDs which i last entered.Test name should create as seperate object(TestSample) record  in instance.Now it is creating Test sample record.But instead of name it is displaying id.
 
public class AddSampleCls
{
    public Visit_Samples__c visitSmaple{get;set;}
    public Boolean allowSampleCreation{get;set;}
    public Boolean isMobile{get;set;}
    public Event currentEve{get;set;}
    public list<wrapPm> wrp1{get;set;}
    public boolean testflag{get;set;}
    public map<id,string> testlist{set;get;}
    public string selectedtest{set;get;}
    public Sample__c sampleRec{get;set;}
    public Test_Sample__c sampleTest{get;set;}
    public List<Sample__c> sampleReclist{get;set;}
    public List<SelectOption> list_AllsampleTypes{get;set;}
    public List<SelectOption> list_businessSegments{get;set;}
    public transient Blob sampleImage{get;set;}
    public List<String> list_SampleTypesSelected{get;set;}
    public List<String> list_BusinessSegSelected{get;set;}
    
    public AddSampleCls(ApexPages.StandardController controller) 
    {
        wrp1=new list<wrapPm>();
        
        isMobile = UserInfo.getUiTheme() == 'Theme4t';
        
        if(!String.IsBlank(ApexPages.CurrentPage().getParameters().get('Id')))
        {
            currentEve = [SELECT Id,WhoId,Visit_Type__c,Subject,Visit_Samples__c,StartDateTime,EndDateTime FROM Event WHERE Id =: ApexPages.CurrentPage().getParameters().get('Id')];
            
            if(currentEve.WhoId != Null && currentEve.WhoId.getSObjectType() == Contact.sObjectType)
            {
                if(currentEve.Visit_Type__c=='Sample Collection')
                {
                    if(currentEve.Visit_Samples__c != Null)
                    {
                        visitSmaple = [SELECT Id,Name,From_Date__c,To_Date__c FROM Visit_Samples__c WHERE Id =: currentEve.Visit_Samples__c];
                        system.debug('VVVVVVVVVVVVVVisitSample'+visitSmaple);
                    }
                    else
                    {
                        visitSmaple = new Visit_Samples__c();
                    }
                    sampleRec = new Sample__c(Doctor__c=currentEve.WhoId,Visit_Samples__c=visitSmaple.Id);
                   
                    allowSampleCreation = true;
                    list_AllsampleTypes = new List<SelectOption>();
                    list_businessSegments = new List<SelectOption>();
                    
                    list_SampleTypesSelected = new List<String>();
                    list_BusinessSegSelected = new List<String>();
                    
                    Schema.DescribeFieldResult fieldResult = Sample__c.Sample_Type__c.getDescribe();
                    List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
                    
                    for( Schema.PicklistEntry f : ple)
                    {
                        list_AllsampleTypes.add(new SelectOption(f.getLabel(), f.getValue()));
                    }
                    
                    Schema.DescribeFieldResult fieldResult1 = Sample__c.Business_Segments__c.getDescribe();
                    List<Schema.PicklistEntry> ple1 = fieldResult1.getPicklistValues();
                    for( Schema.PicklistEntry f : ple1)
                    {
                        list_businessSegments.add(new SelectOption(f.getLabel(), f.getValue()));
                    }
                }
                else
                {
                    allowSampleCreation = false;
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,Label.Visit_Type_Sample_Collection_Validation));
                }
            }
            else
            {
                allowSampleCreation = false;
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error,Label.Doctor_s_Visit_Validation));
            }
          
        }
    }
    
 
    public void addTest()
    {
        wrapPm w=new wrapPm();
        
        w.sampleRec1=sampleRec;
        wrp1.add(w);
        sampleTest =new Test_Sample__c();
       selectedtest=null;
        
    }
    
    Public void removetest()
    {
        Integer indexVal = Integer.valueof(system.currentpagereference().getparameters().get('index'));
        wrp1.remove(indexVal - 1);
        
    }
    
    
    public class wrapPm
    {
        public Sample__c sampleRec1{get;set;}
        public String name{get; set;}
        public Test_Sample__c sampleTest1{get;set;}
        //public String Test_Code__c {get;set;}
        
    }
    public pageReference saveAll()
    {
        Savepoint sp = DataBase.setSavePoint();
        try
        {
            if(currentEve.Visit_Samples__c != Null)
            {
                sampleRec.Visit_Samples__c = visitSmaple.Id;
                
            }
            else
            {
                visitSmaple.Name = currentEve.Subject+'- Samples';
                visitSmaple.From_Date__c=currentEve.StartDateTime.date();
                visitSmaple.To_Date__c=currentEve.EndDateTime.date();  
                insert visitSmaple;
                currentEve.Visit_Samples__c = visitSmaple.Id;
                Update currentEve;
            }
            
            sampleRec.Sample_Type__c = String.join(list_SampleTypesSelected,';');
            sampleRec.Business_Segments__c = String.join(list_BusinessSegSelected,';');
            sampleRec.Visit_Samples__c = visitSmaple.Id;
            Insert sampleRec;
            
            if(sampleImage != Null)
            {
                Attachment att = new Attachment();
                att.Name = 'Sample';
                att.ContentType = 'image/jpeg';
                att.Body = sampleImage;
                att.ParentId = sampleRec.Id;
                Insert att;
            }
            if(sampleTest != Null)
            {
                Test_Sample__c ts=new Test_Sample__c();
                ts.Name=sampleRec.Test_Code__r.Test_Name__c;
                
                ts.Test_Name__c=sampleRec.Test_Code__c;
                ts.Sample__c=sampleRec.id;
                ts.Visit_Samples__c=visitSmaple.Id;
                insert ts;
                      }
            return new PageReference('/'+currentEve.Id);
        }
        catch(exception e)
        {
            ApexPages.addMessages(e);
            DataBase.RollBack(sp);
            return null;
        }
    }
}

Canyone pls help me to resolve this

Thanks
Devmen