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

Problem with Trigger on Data Load - and sometimes other times.

I can't figure out why this trigger does not work sometimes.


It doesn't do anything at all, sometimes.  Other times it adds the wrong Record Id into the Lookup field.  Other times it works just fine.


It seems to freak out during data loads.  It seems to work (most of the time, but not always) during the normal UI/UX.


I'm trying to pull a Record Id from a formula field (Contract_Header__c).  That Record Id should fill in the Contract_Header_LOOKUP__c field.  That's it.  This should happen within the Object (Contract_Line__c) whenever it is created by another object (Asset) which fires a trigger when Asset.Status = 'Active'.


Existing Code:


trigger ContractLine on Contract_Line__c (before insert, before update)  {
    List <Contract_Line__c> clsToUpdate = new List <Contract_Line__c>();
    String headerName, accountName;
    Id chId;

    for(Contract_Line__c c:
        System.debug('++++ looping through Contract_Line__c trigger context ');
        if(c.Contract_Header_LOOKUP__c == null && c.Contract_Header_Name__c != null ){
            System.debug('++++ Contract_Header_LOOKUP__c not null && Contract_Header_Name__c not null  ');
            System.debug('++++ assigning account name and header name ');
            accountName = c.Account__c;
            headerName = c.Contract_Header_Name__c;

    for(Contract_Header__c ch : [SELECT Id FROM Contract_Header__c WHERE Name =: headerName AND Account__c =: accountName ])
        System.debug('++++ query for loop to find Contract_Header__c with matching headername and account ');
        chId = ch.Id;
        System.debug('++++ assigned chId  = ' + chId);

    for(Contract_Line__c cl:
        System.debug('++++ back in trigger context to udpate contract header');
        if(chId != null)
            cl.Contract_Header_LOOKUP__c = chId;
        System.debug('++++ udpated Contract_Header_LOOKUP__c on contractline ');    
        System.debug('++++ cl.Contract_Header_LOOKUP__c = ' + cl.Contract_Header_LOOKUP__c);


Old Code - which worked - but threw too many exceptions (on SOQL queries when doing data loads or lots of changes - b/c there weren't enough filters in place:

trigger ContractLine on Contract_Line__c (after insert, after update){
   List <Contract_Line__c> cList = new List <Contract_Line__c>();
   List <Contract_Header__c> cHeaderList = new List <Contract_Header__c>();
   for(Contract_Line__c c: [Select Contract_Header__c, Contract_Header_LOOKUP__r.Most_Recent_Update__c from Contract_Line__c where Id IN :Trigger.newMap.keySet()])
   c.Contract_Header_LOOKUP__r.Most_Recent_Update__c =;
cHeaderList.add(c.Contract_Header_LOOKUP__r);       }
     update cHeaderList;
    }catch(DMLException e)
     system.debug (e); 


Can anyone help me to make this work?



These variables "String headerName, accountName; Id chId;" are being stomped all over within the loops! You'll need a Map to hold these in, and retrieve and apply them as required within your 2nd and 3rd loop.