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
DmonikaDmonika 

how to retrieve lookup relation values from soql

I have a lookuprelation in obj1(Registration__c), Now on insert i need to create a record on obj2(OutPatient__c) with lookup relation object values.

here is my code.
trigger Outrecord on Registration__c (after insert) {
 public string DName;
    List<OutPatient__c> lop = new List<OutPatient__c>();
    Doctor__c doc = new Doctor__c();
    for(Registration__c reg : trigger.new)
    {
        system.debug(reg.name);
        if(reg.OutPatient__c==true)
        {
             OutPatient__c op = new OutPatient__c();
            op.Name=reg.Name__c;
            op.RegNumber__c=reg.Name;
             DName= [select Doctor__r.Name__c from Registration__c rgt where rgt.Doctor__r.Name =:reg.DoctorName__c];//this query is not working.
            op.DoctorName__c = DName;
            lop.add(op);
        }
        insert lop;
        
    }

Error:Didn't understand relationship 'Doctor__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
    
Registration__c is custom object having doctor__c has lookuprelation.
I required Name__c from Doctor__c in obje2(outpatient__c) while i insert a record in Registration__c

Thanks in advance.
Best Answer chosen by Dmonika
ManojjenaManojjena
Hi Monika ,

Try with below code it will help !!
trigger Outrecord on Registration__c (after insert) {
List<OutPatient__c> outPatientList = new List<OutPatient__c>();
  for( Registration__c reg :[SELECT Doctors__r.Name,OutPatient__c,Name__c FROM Registration__c WHERE  Id IN :Trigger.new){
     if(reg.OutPatient__c==true){
		OutPatient__c op = new OutPatient__c();
		op.Name=reg.Name__c;
		op.RegNumber__c=reg.Name;
		op.DoctorName__c =reg.Doctors__r.Name ;
		outPatientList.add(op);
	 }
   }
    if(!outPatientList.isEmpty()){
	  try{
	    Insert outPatientList;
	  }catch(DmlException de ){
	    System.debug(de);
	  }
	}
}

Let me know if it  helps 
Thanks 
Manoj

All Answers

Prabhat Kumar12Prabhat Kumar12
Hi Monica,

There are some thing you need to check

1. Your query has rgt after Registration__c , please remove this.
2. Make sure you have name__c field on doctor object (You can use Doctor__r.Name only in your query.)
3. Make sure Doctor__c is parent of registraion__C.

 
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Monica, 
 try this code ,
 
trigger Outrecord on Registration__c (after insert) {
 public string DName;
    List<OutPatient__c> lop = new List<OutPatient__c>();
    Doctor__c doc = new Doctor__c();
	List<String> regDocNameList = new List<String>();
	
    for(Registration__c reg : trigger.new) 
		regDocNameList.add(reg.DoctorName__c);
	Map<Id,String> docNameMap = new Map<Id, String>([SELECT Doctor__r.Name FROM Registration__c WHERE Doctor__r.Name IN :regDocNameList]);
	
	for(Registration__c reg : trigger.new)
    {
        system.debug(reg.name);
        if(reg.OutPatient__c==true)
        {
             OutPatient__c op = new OutPatient__c();
            op.Name=reg.Name__c;
            op.RegNumber__c=reg.Name;
            op.DoctorName__c = docNameMap.get(reg.Id);
            lop.add(op);
        }
        insert lop;
        
    }

Thanks
Prosenjit
DmonikaDmonika
Thank you all for quite response.

1. Doctor__c is parent of registraion__C.(I am abit confused plz clear me, in Registration object i created a custom field lookup with object Doctor.So it confirms that Doctor__c is parent  of Registration right).

@Prosenjit

I just copied your code even though i am getting the same error like
Didn't understand relationship 'Doctor__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.

Need a bit more help
Thanks in advance.

 
ManojjenaManojjena
Hi Monika ,

Try with below code it will help !!
trigger Outrecord on Registration__c (after insert) {
List<OutPatient__c> outPatientList = new List<OutPatient__c>();
  for( Registration__c reg :[SELECT Doctors__r.Name,OutPatient__c,Name__c FROM Registration__c WHERE  Id IN :Trigger.new){
     if(reg.OutPatient__c==true){
		OutPatient__c op = new OutPatient__c();
		op.Name=reg.Name__c;
		op.RegNumber__c=reg.Name;
		op.DoctorName__c =reg.Doctors__r.Name ;
		outPatientList.add(op);
	 }
   }
    if(!outPatientList.isEmpty()){
	  try{
	    Insert outPatientList;
	  }catch(DmlException de ){
	    System.debug(de);
	  }
	}
}

Let me know if it  helps 
Thanks 
Manoj
This was selected as the best answer
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi monica,
Check the relationship name of doctor and registration. You can see that in this way.
Setup -> Create -> Object -> Registration.
Go to Doctor Field .
Check relationship name.
Now please elaborate your requirement a bit more that what you want to do actually ?
Thanks
Prosenjit.
 
Prosenjit Sarkar 7Prosenjit Sarkar 7
And check the Api of Doctor field in registratio also and tel us
DmonikaDmonika
@ Prosenjit,Manoj &
Prabhat Kumar12 Thank you all for encouraging me.
This is my Registration object details.
This is my Registration Object with custom fields( DoctorName__c is the lookup field)
here DoctorName__c takes the autonumber(Standardfield)of the Doctor__c object.
Now on insert of Registration object with outPatient == true i need to create a outpatient record.
 like this .
User-added image

The thing,I am able to create the record but in the field DoctorName__c.I require the exact DoctorName not the ID of the particular Doctor.

Plz can any one explain me how the relations are taken into account while we are querying..I am totally confused.
Thanks in advance.
ManojjenaManojjena
Hi Monika ,

Try with below code it will help .
trigger Outrecord on Registration__c (after insert) {
List<OutPatient__c> outPatientList = new List<OutPatient__c>();
  for( Registration__c reg :[SELECT Doctors__r.Id,OutPatient__c,Name__c FROM Registration__c WHERE  Id IN :Trigger.new){
     if(reg.OutPatient__c==true){
		OutPatient__c op = new OutPatient__c();
		op.Name=reg.Name__c;
		op.RegNumber__c=reg.Name;
		op.DoctorName__c =reg.Doctors__r.Id ;
		outPatientList.add(op);
	 }
   }
    if(!outPatientList.isEmpty()){
	  try{
	    Insert outPatientList;
	  }catch(DmlException de ){
	    System.debug(de);
	  }
	}
}

 
DmonikaDmonika
Doctors__c is not a valid API name. @ Manoj
ManojjenaManojjena
Try to replace Doctor__r with Doctors__r 
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Monica,
I have done some silly mistakes in previous codes :P

please try with this ,
 
trigger Outrecord on Registration__c (after insert) {
 public string DName;
    List<OutPatient__c> lop = new List<OutPatient__c>();
    Doctor__c doc = new Doctor__c();
	List<String> regDocNameList = new List<String>();
	
    for(Registration__c reg : trigger.new) 
		regDocNameList.add(reg.DoctorName__c);
	List<Registration__c> docNameList = [SELECT DoctorName__r.Name FROM Registration__c WHERE DoctorName__r.Name IN :regDocNameList];
	Map<id, String> docNameMap = new Map<Id, String>();
	for(Registration__c reg : docNameList)
		docNameMap.put(reg.Id, reg.DoctorName__r.Name);
	
	for(Registration__c reg : trigger.new)
    {
        system.debug(reg.name);
        if(reg.OutPatient__c==true)
        {
             OutPatient__c op = new OutPatient__c();
            op.Name=reg.Name__c;
            op.RegNumber__c=reg.Name;
            op.DoctorName__c = docNameMap.get(reg.Id);
            lop.add(op);
        }
        insert lop;
    }
}

Thanks 
Prosenjit
DmonikaDmonika
@ Prosenjit thank you for ur response and supoorting me.

User-added image

this is the output..:-(

Even i too got the same output before.

Thanks 
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Monica ,
Is the code running or you are geting exception ? If codein is running check your Registartion record and its acosiated Docro Record that there is any empty field or not.
Thanks
Prosenjit
DmonikaDmonika
On obseveration, I got one thing that is reg.DoctorName__c is retrieving 18digits ID.So while querying with the value will it retrieve our required field values.
Prosenjit Sarkar 7Prosenjit Sarkar 7
But monica I havn't use  DoctorName__c. I use DoctorName__r.Name instead.