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
Matthew HoldgateMatthew Holdgate 

Populate lookup with trigger

Hi everyone ive asked this question alot and am yet to get a solution. I need to populate a lookup field with a record that matches some criteria. Attached is my code and entity model etc
trigger setTermsConditions on DOUM_DOU__c (before insert, before update) {

Map<Id,Id> cases = new Map<Id,Id>(); 
 
    List<DOUM_Terms_Conditions__c> listGL = new List<DOUM_Terms_Conditions__c>(); 

try{     

listGL = [Select DOUM_TermsConditions.Name, DOUM_Terms_Conds__c from DOUM_DOU__c where Id In: trigger.newMap.keySet() AND DOUM_Terms_Conditions__c.DOUM_TCs_RD__c = NULL]; 

}catch(exception e){ 

 system.debug(e.getmessage()); 

} 

if(listGL !=null && ! listGL.isempty()){  

    for(DOUM_DOU__c objGL: listGL) 
 
    { 

        cases.put(objGL.id,objGL.DOUM_TermsConditions__r.Name); 
        
    } 

} 

    for (DOUM_DOU__c a : Trigger.new) 

    { 
    
        if(a.DOUM_Terms_Conds__c != nul && cases.containskey(a.DOUM_Terms_Conds__c)) 

        { 
            a.DOUM_Terms_Conds__c = cases.get(a.DOUM_Terms_Conds__c); 

        } 
    } 

} 


}
My code doesnt save because i get this error
 
Error: Compile Error: Didn't understand relationship 'DOUM_TermsConditions' 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. at line 9 column 10


User-added image
Akhil ReddyAkhil Reddy
Please change 
DOUM_Terms_Conditions__c

to 
DOUM_Terms_Conditions__r

in Line # 9
SalesforceSCSalesforceSC
In line 9, try Select DOUM_TermsConditions__r.Name
instead of
Select DOUM_TermsConditions.Name
Matthew HoldgateMatthew Holdgate
I now get this error
Error: Compile Error: Didn't understand relationship 'DOUM_TermsConditions__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. at line 9 column 10

 
rajat Maheshwari 6rajat Maheshwari 6

Hi,

Biggest mistake, i am seing here is  : - 

You have initialize the list  : - 

List<DOUM_Terms_Conditions__c> listGL = new List<DOUM_Terms_Conditions__c>();

but in query, you are using : - 

listGL = [Select DOUM_TermsConditions.Name, DOUM_Terms_Conds__c from DOUM_DOU__c where Id In: trigger.newMap.keySet() AND DOUM_Terms_Conditions__c.DOUM_TCs_RD__c = NULL];


Firstly, please replace DOUM_DOU__c by DOUM_Terms_Conditions__c and use all the fields of DOUM_Terms_Conditions__c   in soql query.

 

Thanks
Rajat Maheshwari

 

 

EldonEldon
Hi Mathew go to that object edit page and go into your look up field DOUM_Terms_Conditions__c and check for the child relationship name and replace that name in the line 9 with DOUM_Terms_Conditions__c.
Matthew HoldgateMatthew Holdgate
After changing a few things when promted by the system after those changes I got the trigger to save. However it doesnt work. When a new record is created the terms and conditions lookup field doesn't populate. 

My code is now 
trigger setTermsConditions on DOUM_DOU__c (before insert, before update) {

Map<Id,Id> cases = new Map<Id,Id>(); 
 
    List<DOUM_Terms_Conditions__c> listGL = new List<DOUM_Terms_Conditions__c>(); 

try{     

listGL = [Select Name from DOUM_Terms_Conditions__c where Id In: trigger.newMap.keySet() AND DOUM_Terms_Conditions__c.DOUM_TCs_RD__c = NULL]; 

}catch(exception e){ 

 system.debug(e.getmessage()); 

} 

if(listGL !=null && ! listGL.isempty()){  

    for(DOUM_Terms_conditions__c objGL: listGL) 
 
    { 

        cases.put(objGL.id,objGL.Name); 
        
    } 

} 

    for (DOUM_DOU__c a : Trigger.new) 

    { 
    
        if(a.DOUM_Terms_Conds__c != null && cases.containskey(a.DOUM_Terms_Conds__c)) 

        { 
            a.DOUM_Terms_Conds__c = cases.get(a.DOUM_Terms_Conds__c); 

        } 
    } 

}