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
Hugo_BHugo_B 

Trigger issue using Set<ID> to pull data.

I have a trigger on a custom object (Time_Card_Entry__c) that looks at a CONTRACT lookup field on the object.  If the Contract lookup is not empty, it populates the set with the contract - then uses "MAP" to pull data about the contract which I use later in the trigger. 

My issue is I want to check to see if the Contract lookup is null, then populate it with a contract record that is based on a SOQL query - but I can't seem to manage to add the SOQL query results to the SET with out getting an error.  Here is what I have:

if(Trigger.isInsert||Trigger.isUpdate) {          
                Set<Id> contractIds = new Set<Id>();
                     // If a Contract exists on the time entry record, popuate the contractID set with the contract  
                     for (Time_Card_Entry__c te : Trigger.new) {
                            if (te.Contract__c != null) {
                                    contractIds.add(te.Contract__c);
                    }else
                         **** HERE IS WHERE I WANT TO ADD TO THE SET A CONTRACT ID BASED UPON A SOQL QUERY E.G BUT THIS IS NOT WORKING - TRIED SEVERAL WAYS - THIS IS ONLY AN EXAMPLE

                         contractids.add(Select ID from CONTRACT where account = "xxxxx" and contract = 'active' Limit 1)

                    }
                }
                

            // Create Map of Contract to hold fields from Contract  
               Map<Id, Contract> contractEntries = new Map<Id, Contract>(
                    [select CMRate__c, ConsultingRate__c, Data_ManagementRate__c, PMRate__c, SupportRate__c, GrpTrainRateFL__c,
                    GrpTrainRateHd__c, GrTrainRateHR__c, OneTrainRate__c, IT_SupportRate__c, Account_Name__c from Contract where id in :contractIds]
                );
    

The above works provided the Contract lookup field is populated, but I can't get the code to look up a contract and populate the SET if there is no contract.

Any help greatly appreciated!

Hugo
MithunPMithunP
Hi Hugo_B,

Add isBefore trigger context and update your code like below.
 
if(Trigger.isBefore){

if(Trigger.isInsert||Trigger.isUpdate) {          
                Set<Id> contractIds = new Set<Id>();
                     // If a Contract exists on the time entry record, popuate the contractID set with the contract  
                     for (Time_Card_Entry__c te : Trigger.new) {
                            if (te.Contract__c != null) {
                                    contractIds.add(te.Contract__c);
                    }else
                       
                         **** HERE IS WHERE I WANT TO ADD TO THE SET A CONTRACT ID BASED UPON A SOQL QUERY E.G BUT THIS IS NOT WORKING - TRIED SEVERAL WAYS - THIS IS ONLY AN EXAMPLE
                         Contract cntrct = [Select ID from CONTRACT where account = "xxxxx" and contract = 'active' Limit 1];
                         contractids.add(cntrct.Id)
                        te.Contract__c = cntrct.Id;
                    }
                }


                

            // Create Map of Contract to hold fields from Contract  
               Map<Id, Contract> contractEntries = new Map<Id, Contract>(
                    [select CMRate__c, ConsultingRate__c, Data_ManagementRate__c, PMRate__c, SupportRate__c, GrpTrainRateFL__c,
                    GrpTrainRateHd__c, GrTrainRateHR__c, OneTrainRate__c, IT_SupportRate__c, Account_Name__c from Contract where id in :contractIds]
                );

}

Best Regards,
Mithun.
BalajiRanganathanBalajiRanganathan
In addtion to Mithun, you have to do the following

1) Execute your SOQL in a query browser to validate it. you will not able say where account= 'xxx'.there is no column account and contract in contract object. you can use where account.name = 'xxx' and status = 'active'

2) Also you should not execute the SOQL inside a for loop. Try to get the set of account Id or account names and execute the SOQL outside for loop