+ Start a Discussion
Patricia LimPatricia Lim 

Display name instead of ID when inserting records from VF page

I have a custom controller behind a VF page that sets the value of one of the fields to be the one that the user inputs in another field:
public void saveScholarship(){
        rec = new recipient__c(School__c=sch.School__c);
        insert rec;
        sch.Recipient__c = rec.Id;
        insert sch;
        sch = new scholarship_award__c(recipient__c=rec.Id);
    }
}

However when the record is saved to my org, the ID is saved in the `recipient__c` name field instead of the name that was inputted by the user. How can I get the name to save and display instead of the ID?
ShirishaShirisha (Salesforce Developers) 
Hi Patricia,

Greetings!

If you have field name called "Name" on the Object Recipient__c then you can simply display the name as below:

rec.Name(if it is standard field) or else rec.Name__c;

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri

 
Patricia LimPatricia Lim
Thanks. But issue here is that I have fields: first and last name, so the only way to lookup the recipient object is by ID.. How can I set the `recipient__c` field on scholarship award as the recipient that was just created, and put the the first and last name that was inputted by the user in the `Name` and `Last_name__c` fields on the recipient record?
Patricia LimPatricia Lim
Edit: Replaced it with `rec.name` and received an error that recipient field was missing. 

Attemped using a SOQL query to lookup the recipient record:
 
public void saveScholarship(){
        rec = new recipient__c(School__c=sch.School__c);
        insert rec;
        sch.Recipient__c = [SELECT Name, Last_Name__c FROM recipient__c WHERE Name=rec.Name AND Last__Name__c=rec.Last_Name__c]
        insert sch;
        sch = new scholarship_award__c(recipient__c=rec.Id);
    }
}