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
cbrocbro 

Trigger Compile Error "left angle bracket" (too many SOQL Queries fix, as well)

Can anyone help me to fix this trigger?  

 

What am I missing here?

 

 

Error: Compile Error: expecting a left angle bracket, found '>' at line 3 column 32

 

trigger ContractLine_ContractHeaderLookupUpdate on Contract_Line__c (before insert, before update){

Set <Id> headerIds = new Id<Set>():
List <Contract_Line__c> contractLines = new List <Contract_Line__c>();
List <Contract_Line__c> updateContractUpdate = new List <Contract_Line__c>();

//go through trigger items and add related headers to a set
for(Contract_Line__c c: Trigger.new){
headerIds.add(c.Contract_Header_LOOKUP__c)
}

//go through headers and get account Ids
    List <Contract_Header__c> headers = [Select Id, Account__c FROM Contract_Header__c WHERE Id IN: headerIds];

    //match header accounts to cl accounts
List <Contract_Line__c> contractLines = [Select Id, Contract_Header_LOOKUP__c
     FROM Contract_Line__c
     WHERE Id IN: Trigger.newMap.keySet()
     AND Account__c IN: contractHeaderAccts]);

//copy the record id to contract header lookup
if(contractLines.size()>0 && headers.size()>0){     
for(Contract_Line__c cl: contractLines){
for(Contract_Header__c ch: headers){
cl.Contract_Header_LOOKUP__c = ch.Id;
updateContractUpdate.add(cl);
}
}
}

    if(updateContractUpdate.size()>0){
     update updateContractUpdate;
    }
 }

 

 

This was my original code, but I was getting too many SOQL queries (not enough filters) when uploading data, so I changed it (with lots of help) to the above:

 

trigger ContractLine_ContractHeaderLookupUpdate on Contract_Line__c (before insert, before update)
{

    for (Contract_Line__c cl: Trigger.new)
    {
        
        if(cl.Contract_Header_LOOKUP__c == Null)
        {
            cl.Contract_Header_LOOKUP__c = [SELECT id FROM Contract_Header__c WHERE Name =: cl.Contract_Header__c].id ;
        }
        
    }
    
}

 

Best Answer chosen by Admin (Salesforce Developers) 
David Lee(China Developer)David Lee(China Developer)

Please notice the second line of your code:-)

Set <Id> headerIds = new Id<Set>():
It should be
Set <Id> headerIds = new Id<Set>();

All Answers

David Lee(China Developer)David Lee(China Developer)

Please notice the second line of your code:-)

Set <Id> headerIds = new Id<Set>():
It should be
Set <Id> headerIds = new Id<Set>();
This was selected as the best answer
ForcepowerForcepower
Try this:
Set <Id> headerIds = new Set <Id>():

Best,
Ram
cbrocbro

After a few more tweaks, here is the final code.  I had to change it to after insert and after update, as I was receiving errors in trying to refer to a null object when trying to run this before insert.

 

I needed to add filters to stop the SOQL queries from going over the limit.

 

Instead of pulling back ALL Contract Header records, I only pulled back records that matched the Account ID for the Contract Lines.  

 

In our instance, Contract_Header__c.Account__c will always match Contract_Line__c.Account__c, so that is how I fixed the issue of too many SOQL Queries.

 

trigger ContractLine_ContractHeaderLookupUpdate on Contract_Line__c (after insert, after update){

Set <Id> headerIds = new Set <Id>();
List <Contract_Line__c> contractLines = new List <Contract_Line__c>();
List <Contract_Line__c> updateContractUpdate = new List <Contract_Line__c>();

//go through trigger items and add related headers to a set
    for(Contract_Line__c c: Trigger.new)
    {
        headerIds.add(c.Contract_Header_LOOKUP__c);
    }

//go through headers and get account Ids
List <Contract_Header__c> headers = [Select Id, Account__c FROM Contract_Header__c WHERE Id IN: headerIds];

//match contract header accounts to contract line accounts
List <Contract_Line__c> contractLinesToo = [Select Id, Contract_Header_LOOKUP__c
     FROM Contract_Line__c
     WHERE Id IN: Trigger.newMap.keySet()
     AND Account__c IN: headerIds];

//copy the record id to contract header lookup
    if(contractLines.size()>0 && headers.size()>0)
    {     
        for(Contract_Line__c cl: contractLinesToo){
        for(Contract_Header__c ch: headers){
        cl.Contract_Header_LOOKUP__c = ch.Id;
        updateContractUpdate.add(cl);
            }
        }
    }

    if(updateContractUpdate.size()>0){
     update updateContractUpdate;
    }
 }