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
James Byron 26James Byron 26 

Using APEX and SOQL to query multiple objects

I'm new to APEX/SOQL.  I am building a apex class that contains a method which, when called from a trigger or elsewhere, will update a field in each contact that is passed in a list by argument.  The filed will need to look at numerous objects, some of which have a 1:1 relationship between contacts (like one active application per contact), while others will have a 1:many relationship (like many test scores or educational histories per aplication).  The application object has an api name EnrollmentrxRx__Enrollment_Opportunity__c as you see in the code below.
 
public class CalculateFinAidIndex {
    public static void updateIndex(Contact[] contacts) {
        for (Contact con : contacts) {
            String activeApp = con.Active_Application_SF_ID__c;
            EnrollmentrxRx__Enrollment_Opportunity__c app = [SELECT Name FROM EnrollmentrxRx__Enrollment_Opportunity__c AS a WHERE a.Name = activeApp LIMIT 1];
        }
    }
}

In the Dev Console, I get a compile error "expecting a colon, found 'activeApp'".  Can you offer some quidance for reating this query to select the active Application by referecing it from the field by that name on the Contact object?

Also, my class will need to run to update every Contact that's already been created.  I only need to update Contact records that have not previously been updated.  I'm looking at using a trigger's "after update" and "after insert" to cover new and updated contacts.  Is there a better way to running this update on all contacts to check if they haven't been processed yet?  I guess that would be like a bulk update that calls this function...thatnks for your help!
ProlayProlay
//Please decleare the EnrollmentrxRx__Enrollment_Opportunity__c as List

List<EnrollmentrxRx__Enrollment_Opportunity__c> app = [SELECT Name FROM EnrollmentrxRx__Enrollment_Opportunity__c AS a WHERE a.Name = activeApp LIMIT 1];

ProlayProlay
To find the Contacts which are not updated, first create a Formual Field Contact_Not_Updated__c with the formula as "AND( CreatedDate <> LastModifiedDate )". Then create a trigger for "before update" on Contact object using the following SOQL query
List<Contact> NotUpdatedContact = [SELECT Name FROM Contact WHERE Contact_Not_Updated__C = TRUE AND ID in Trigger.newMap.keySet()];

Then process the records and then update the database with ​NotUpdatedContact
Carlos Campillo GallegoCarlos Campillo Gallego
Hi James,
The problem of your query is that you are not using ' ' after the WHERE statement. Try with 'activeApp' . Another thing that you should have in mind is to avoid having queries inside loops (you have this query inside your for loop). The reason for this is that you will probably reach SOQL limit very easily depending on the rest of you org code. The way to avoid this is called bulkification (probably you have already heard of it before :) )  Take a look to this link if you want to know some of the best common practices (maybe you can even add one of yours!)
http://salesforce.stackexchange.com/questions/47469/general-trigger-bulkification-best-practices

Regards,
James Byron 26James Byron 26
For anybody who reads this thread in the future, I thought I'd followup with the following reference to another thread that is relevant and useful: https://developer.salesforce.com/forums/?id=906F00000008x3NIAQ