You need to sign in to do that
Don't have an account?
James 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.
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!
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!
Then process the records and then update the database with NotUpdatedContact
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,