I have two objects patient__c and Doctor__c objects, in patient record i have look up of doctor. Now my question is I am writing trigger for after insertion of a record in patient object I want a value displayed in lookup field.

trigger patient on Patient__c (before insert) {
    List<doctor__c> doc= new List<doctor__c>();
    doc = [SELECT Id FROM doctor__c WHERE Id IN: Trigger.oldMap.keySet() LIMIT 1]; 

    for(Patient__c pat:doc) {
    insert doc;
  • How do you know which doctor should be assigned to each patient? Is there some field on the Patient record that can help you determine the right Doctor record to look up to?
    – IvanR
    Commented Nov 11, 2014 at 10:17
  • yes,here on patient record i have lookup field named doctor which refer to doctor object from that we can select the doctor for each patient Commented Nov 11, 2014 at 10:22
  • 1
    Lookup field just allows you to link Patient to Doctor, but in order to do that you need to know which particular Doctor record (out of many others that your system keeps track off) has to be linked to each of the patients. I don't see how you can make your trigger work without understanding the logic behind Doctor-to-Patient assignment.
    – IvanR
    Commented Nov 11, 2014 at 10:27
  • ya,this is just my practice to understand triggers,i just want any of the doctor record in the patient to display. Commented Nov 11, 2014 at 10:37

2 Answers 2


One of the reasons your code won't compile is following line:

for(Patient__c pat:doc) {

doc is List of Doctor__c records, and you want to go through every record in that list and assign it to the variable pat of type Patient__c.

Assuming you don't have any specific requirements regarding which doctor should be assigned to patients, you can solve the problem like this:

trigger Patient on Patient__c (before insert) {
    List<Doctor__c> doc= [SELECT Id FROM Doctor__c LIMIT 1]; //Returns a single arbitrary Doctor__c record

    for (Patient__c pat : Trigger.new) {
        pat.Doctor__c = doc[0].id;

Code above selects first arbitrary Doctor record (notice no WHERE clause in SOQL query), then loops through all Patients that are being inserted (Trigger.new list) and updates lookup field with Doctor ID.

You don't need to explicitly call insert method because this is before insert trigger and all changes you make to the records in special Trigger.new list will be saved for you.

You might find it helpful to familiarise yourself with Salesforce documentation and best practices on writing triggers.

  • thank you very much its working,but please tell when to use this query and what is the use of this query [SELECT Id FROM doctor__c WHERE Id IN: Trigger.oldMap.keySet() LIMIT 1 Commented Nov 11, 2014 at 11:27
  • That query doesn't make much sense. First of all, Trigger.oldMap is not available in before insert trigger context because you have not inserted any records at the time when trigger runs and hence there are no 'old' values. If you were writing and after update trigger on Patient__c object, then Trigger.oldMap would return map of record values before update. Trigger.oldMap.keySet() would give you set of IDs only. See this document for list of all available trigger context variables.
    – IvanR
    Commented Nov 11, 2014 at 11:45

If you wanted to assign a random doctor when a patient record is created then you could use metadaddys answer from this question to do the following:

trigger patient on Patient__c (before insert) {
    Integer count = [SELECT COUNT() FROM Doctor__c];
    if (count > 0) {
        Integer rand = Math.floor(Math.random() * count).intValue();
        Doctor__c randomDoctor = [SELECT Id FROM Doctor__c LIMIT 1 OFFSET :rand];

        for(Patient__c pat : Trigger.new) {
            pat.Doctor__c = randomDoctor.Id;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .