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
Dman100Dman100 

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.
Dman100Dman100

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.

sfdcfoxsfdcfox

You'd have to use a for loop:

Code:
sobject[] array1 = new account[0];
id[] array2 = new id[0];
array1.add(new account(name='test'));
for(sobject so:array1)
{ array2.add(so.id);
}

Hope this helps.

Dman100Dman100

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);

}

}

I got the following error:

Save error: Illegal assignment from void to Id
canonwcanonw
Of course you should get this error.  The id field is not set at all.
What are you trying to accomplish exactly?
Dman100Dman100

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?

sfdcfoxsfdcfox

If you want the IDs of all the tasks, do this:

Code:
public static void Test(Task[] tasks) {
  Map<Id,Task> TaskMap = new Map<Id,Task>(tasks);
  Id[] TaskIds = TaskMap.KeySet();
}

If you want other values (like the what IDs), do this:

Code:
public static void Test(Task[] tasks) {
  Set<Id> TaskWhatIds = new Set<Id>();
  for(Task t:tasks)
  {   if(t.whatid<>null)
      {   TaskWhatIds.add(t.whatid);
      }
  }
}

 

Edit: Fixed getKeySet to KeySet.

Message Edited by sfdcfox on 08-07-2008 11:48 AM

Message Edited by sfdcfox on 08-07-2008 12:22 PM
Dman100Dman100

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
sfdcfoxsfdcfox
Sets and Maps can only work on the ID field (it's a convenient shortcut); you'll need to instead use a for loop to add the ids to a set/list/array/etc.
Dman100Dman100

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

When I tried to edit a 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: data changed by trigger for field Opportunity/Account ID: id value of incorrect type: 00T4000000e7zRrEAI
 
Do you see the error in my code?  I tried to comment each line of code with what I am trying to execute.