You need to sign in to do that
Don't have an account?
Dman100
get ids from sObject array
Is there a way to obtain the ids from an sObject array?
For example, if I have a method that takes an sObject array as the argument, how can I obtain the ids from the sObject array?
public static void MyMethod(sObject[] so) {
// how do I translate the sObject array into an array of ids from that sObject
}
Thanks in advance.
I've been looking through the APEX reference guide and I cannot find any documentation that shows how to get ids from an sObject array. I might be missing it or not understanding correctly. Any definitive answer on whether this is possible or not, would be appreciated.
Thanks.
You'd have to use a for loop:
Hope this helps.
I tried using the for loop to assign ids from the sObject array:
public static void Test(Task[] tasks) {
id[] arr = new id[0];
for (Task t : tasks)
{
t.WhatId = arr.add(t.id);
}
}
Save error: Illegal assignment from void to Id
What are you trying to accomplish exactly?
get the ids from the Task array that is passed into the method. Loop thru each item in the task array and set the WhatId field equal to the id stored in the id array.
Does that help explain?
If you want the IDs of all the tasks, do this:
If you want other values (like the what IDs), do this:
Message Edited by sfdcfox on 08-07-2008 11:48 AM
Message Edited by sfdcfox on 08-07-2008 12:22 PM
I tried using the Map<Id, Task>
I got this error:
Save error: Method does not exist or incorrect signature: [MAP:Id,SOBJECT:Task].getKeySet()
occurred on this line:
Id[] TaskIds = TaskMap.getKeySet();
I looked in the APEX reference guide and it showed the method as KeySet()
So, I changed the line to:
Id[] TaskIds = TaskMap.KeySet();
Then I recieved this error:
Save error: Illegal assignment from SET:Id to LIST:Id
I then changed to a Set<ID>
So, I have:
Map<Id,Task> TaskMap = new Map<Id,Task>(tasks);
Set<ID> TaskIds = TaskMap.KeySet();
Set<ID> aid = [select whatid from Task where id in :TaskIds];
I get this error:
Illegal assignment from LIST:SOBJECT:Task to SET:Id which occurs on this line:
Set<ID> aid = [select whatid from Task where id in :TaskIds];
Message Edited by Dman100 on 08-07-2008 12:08 PM
Message Edited by Dman100 on 08-07-2008 12:15 PM
thanks sfdcfox, I appreciate the help. If I can ask one more question.
Here is my revised code:
public class TaskRelatedToAccount {
// pass Task array as argument into method
public static void Test(Task[] tasks) {
// create map of IDs from Task array
Map<Id,Task> TaskMap = new Map<Id,Task>(tasks);
// generate set of IDs from Task Map
Set<ID> TaskIds = TaskMap.KeySet();
// create Task array holding result of query containing all WhatId field values
Task[] whatIds = [select whatid from Task where id in :TaskIds];
// iterate thru each item in the whatIds Task array
for (Task to : WhatIds)
{
// create new ID array
Id[] arr = new id[0];
// add whatid values into ID array
arr.add(to.id);
// iterate thru the Task array
for (Task t : tasks)
{
// set the WhatId field equal to the array value in each loop iteration
t.WhatId = arr[0];
}
}
}
}
I was able to successfully save my class and trigger. I tested the trigger by adding a new task and editing a new task.
When I added a new task, I got this error:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger setWhatId caused an unexpected exception, contact your administrator: setWhatId: execution of BeforeInsert caused by: System.ListException: Row with null Id at index: 0: Class.TaskRelatedToAccount.Test: line 5, column 32
Review all error messages below to correct your data.
Apex trigger setWhatId caused an unexpected exception, contact your administrator: setWhatId: data changed by trigger for field Opportunity/Account ID: id value of incorrect type: 00T4000000e7zRrEAI