You need to sign in to do that
Don't have an account?
mriz
SOQL: how to have 1-1 mapping of objects sharing 1-M relationship
I have two custom objects Project__c & task__c (custom task, so that same task can be allocated to multiple people). one project can have multiple tasks, but task is uniquely associated with project.
I can't use lookup field in task for Project due to my design constraint. I am rather having a field project_id__c holding ID of project in task.
Apex Code:
one quickFix is to query project object inside for loop, but that is extrememly poor approach in SFDC.
Whats solution for this?
I can't use lookup field in task for Project due to my design constraint. I am rather having a field project_id__c holding ID of project in task.
Apex Code:
public class TestClass1{ public List<Id> tasks{get;set;} public Map<Id,String> taskMap{get;set;} // to hold task id and project name public TestClass1(){ try{ tasks = [SELECT id,name,project_id__c FROM Task__c]; projectNames(); }catch(Exception e){ System.debug(e); } } public void projectNames(){ List<Id> projectIds = new List<Id>; for(Task__c t:tasks) projectIds.add(t.project_id__c); System.debug('Size of tasks: '+tasks.size()); List<Project__c> projects = [SELECT name FROM Project__c WHERE id IN:projectIds] System.debug('Size of projects: '+projects.size()); /* this section fails when, say 3 out of 5 tasks are part of one project for(Integer i=0;i<tasks.size();i++) taskMap.put(t[i].id,projects[i].name); */ } }I am facing problem in handling bulkified query.
one quickFix is to query project object inside for loop, but that is extrememly poor approach in SFDC.
Whats solution for this?
I have updated your code to fulfill your requirement.
Please find the updated code below :
I have added the comments in code where i have performed updates.
Please use the above code and let me know if you have any issue with this.
Thanks,
Abhishek Bansal
All Answers
I have updated your code to fulfill your requirement.
Please find the updated code below :
I have added the comments in code where i have performed updates.
Please use the above code and let me know if you have any issue with this.
Thanks,
Abhishek Bansal
In addition to Abhishek's code, I modified few things.
Please check the below code:
Here I followed the best practices, null checks and few naming conventions.
Please let me know if this helps you.
Regards,
Mahesh