You need to sign in to do that
Don't have an account?
Matthew 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
to
in Line # 9
instead of
Select DOUM_TermsConditions.Name
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
My code is now