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 

Trigger to populate the lookup field on child object

Hi

I have a field where the lookup filter always delivers one record. Is it possible to build a trigger that populates this in the back end and prevents the user having to have to click on this. If so how would it look?

My Parent object is called "terms and conditions" and has fields for ID, Content, Retirement date and Active date. the filter on the lookup at the minute it shows the record that has no retirement date and the active date is after the current date.

The child object has a lookup field on that calles DOUM_TCs. 

Any help as I have never heard of apex or triggers before starting this development. Cheers
NagendraNagendra (Salesforce Developers) 
Hi Matthew,

Please check with below thread from forums community with similar kind issue and suggested workarounds. Please mark this as solved if it's resolved.

Regards,
Nagendra.
 
Matthew HoldgateMatthew Holdgate
Hi Nagendra,

I have this so far but get an error: "Invalid foreign key relationship: DOUM_Terms_Conditions__c.DOUM_Terms_Conditions__cobjGL at line 8 column 19"

trigger SetTermsConditions on DOUM_Version__c (before insert, before update) {

Map<Id,Id> cases = new Map<Id,Id>();
    List<DOUM_Version__c> listGL = new List<DOUM_Version__c>();
    listGL = [Select DOUM_Terms_Conditions__r.Name, DOUM_Terms_Conditions__c from DOUM_Version__c where Id In: trigger.newMap.keySet() AND DOUM_Terms_Conditions__c <> null];
    for(DOUM_Terms_Conditions__c objGL: listGL)
    {
        cases.put(objGL.DOUM_Terms_Conditions__cobjGL.DOUM_Terms_Conditions__r.Name);
    }
    for (DOUM_Version__c a : Trigger.new)
    {
        if(a.DOUM_Terms_Conditions__c != null)
        {
            a.DOUM_Terms_Conditions__c = cases.get(a.DOUM_Terms_Conditions__c);
        }
    }
}


Any ideas?
sagar Vadariya 5sagar Vadariya 5
Replace " cases.put(objGL.DOUM_Terms_Conditions__cobjGL.DOUM_Terms_Conditions__r.Name);  "
With
" cases.put(objGL.id,objGL.DOUM_Terms_Conditions__r.Name); "

objGL.id will give you the id of DOUM_Terms_Conditions__c record. 
I can help you more if you can tell me about what values you weant to see in cases map.  cases<?,?>
Matthew HoldgateMatthew Holdgate
So its posting in a lookup box so wont the ID suffice?
Matthew HoldgateMatthew Holdgate
Apex trigger SetTermsConditions caused an unexpected exception, contact your administrator: SetTermsConditions: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.SetTermsConditions: line 5, column 1


I know get the above error