You need to sign in to do that
Don't have an account?

Need help with getting a query out of a Loop
I have this trigger that i created but i am unable to figure out how to get the query out of the for loop, my trigger will run but i know it will hit some governer limits.
trigger AcceptJobAndCreateEmployee on Candidate_Application__c (after update) {
//This will be the list to add the new candidate
List<Account> accounts = new List<Account>();
//Query for the Account Record Types
List<RecordType> rtypes = [Select name, id FROM RecordType WHERE sObjectType = 'Account' AND IsPersonType = True];
Map<String,String> accountRecordTypes = new Map<String,String>();
for(RecordType rt: rtypes){
accountRecordTypes.put(rt.Name,rt.id);
}
for(Candidate_Application__c thisApplication: Trigger.new)
{
Candidate_Application__c oldApp = Trigger.oldMap.get(thisApplication.id);
Boolean AppFalse = oldapp.Job_Accepted__c;
Boolean AppTrue = thisApplication.Job_Accepted__c;
List <Candidate__c> newCandidates = [Select id, name FROM Candidate__C WHERE id =: thisApplication.Candidate__c];
if(!AppFalse && AppTrue)
{
if(newCandidates.size() > 0){
Account converted = new Account();
converted.RecordTypeid = accountRecordTypes.get('Person Account');
converted.LastName = newCandidates.get(0).Name;
converted.Phone = newCandidates.get(0).Phone__c;
accounts.add(converted);
}
}
}
insert accounts;
}
trigger AcceptJobAndCreateEmployee on Candidate_Application__c (after update) {
//This will be the list to add the new candidate
List<Account> accounts = new List<Account>();
//Query for the Account Record Types
List<RecordType> rtypes = [Select name, id FROM RecordType WHERE sObjectType = 'Account' AND IsPersonType = True];
Map<String,String> accountRecordTypes = new Map<String,String>();
for(RecordType rt: rtypes){
accountRecordTypes.put(rt.Name,rt.id);
}
for(Candidate_Application__c thisApplication: Trigger.new)
{
Candidate_Application__c oldApp = Trigger.oldMap.get(thisApplication.id);
Boolean AppFalse = oldapp.Job_Accepted__c;
Boolean AppTrue = thisApplication.Job_Accepted__c;
List <Candidate__c> newCandidates = [Select id, name FROM Candidate__C WHERE id =: thisApplication.Candidate__c];
if(!AppFalse && AppTrue)
{
if(newCandidates.size() > 0){
Account converted = new Account();
converted.RecordTypeid = accountRecordTypes.get('Person Account');
converted.LastName = newCandidates.get(0).Name;
converted.Phone = newCandidates.get(0).Phone__c;
accounts.add(converted);
}
}
}
insert accounts;
}
I believe to move the query out of your loop, you will need to build a set of Candidate Ids as you loop through Application objects. Only add entries to this set if your condition if (!AppFalse && AppTrue) is true. So your new code would look something like this. Only thing you may need to also add is a check to ensure that thisApplication.Candidate__c is non-null. Is that a condition you need to worry about? Hope this helps. Please let us know!
All Answers
Instead of using direct query in trigger use List,Map,and Set funtion.
Please refer below code as example,It will give you more idea,how to write code?
https://developer.salesforce.com/forums/#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=ALLQUESTIONS&id=906F0000000B0VAIA0
Regards,
Anil Savaliya
I believe to move the query out of your loop, you will need to build a set of Candidate Ids as you loop through Application objects. Only add entries to this set if your condition if (!AppFalse && AppTrue) is true. So your new code would look something like this. Only thing you may need to also add is a check to ensure that thisApplication.Candidate__c is non-null. Is that a condition you need to worry about? Hope this helps. Please let us know!