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
swain 10swain 10 

Set to store string of Ids from List<sobject> array

List<sObject> allResult= OV_Searchcc360.isDuplicate(objectType, fields, sources, null);

So allResult is a List of array having multiple records of one object , each record having several fields. I need to use  id from this collection in a Set. so i written this below code to iterate.
        Set<String> resultIds = new Set<String>();
        for (String sourceId : allResult.keySet())  
          {
          resultIds.add(sourceId .DSE__DS_Account__c);  
           system.debug('-------resultIds-----'+resultIds); 
          } 

I need help getting error
Best Answer chosen by swain 10
Narender Singh(Nads)Narender Singh(Nads)
Hi,
You will have to first cast the sobject to your object.
You code should look like this:
Set<String> resultIds = new Set<String>();
        for (Sobject obj: allResult)  
          {
              YOUR_OBJECT o=(YOUR_OBJECT)obj;
              resultIds.add(o.DSE__DS_Account__c);  
              system.debug('-------resultIds-----'+resultIds); 
          }

Please note that this code will still give error because in 'DSE__DS_Account__c' you have double underscore and apex doesn't support that. So you will have to work your way around it.

Let me know if it helps

All Answers

Narender Singh(Nads)Narender Singh(Nads)
Hi,
You will have to first cast the sobject to your object.
You code should look like this:
Set<String> resultIds = new Set<String>();
        for (Sobject obj: allResult)  
          {
              YOUR_OBJECT o=(YOUR_OBJECT)obj;
              resultIds.add(o.DSE__DS_Account__c);  
              system.debug('-------resultIds-----'+resultIds); 
          }

Please note that this code will still give error because in 'DSE__DS_Account__c' you have double underscore and apex doesn't support that. So you will have to work your way around it.

Let me know if it helps
This was selected as the best answer
Maharajan CMaharajan C
Hi Swain,

First there is no method in the List Class as Keyset() it will comes under map class only.

List<sObject> allResult= OV_Searchcc360.isDuplicate(objectType, fields, sources, null);
System.debug('@@@ allresult ' + allresult.size());                  ///     Check the debug did you have the values for Allresult List from above methosd call then only you will have the values in resultIds.
If(allResult.size() > 0)
{
        System.debug('@@@ Inside loop');
        Set<String> resultIds = new Set<String>();
        for (String sourceId : allResult)  
          {
          if(sourceId.DSE__DS_Account__c != null )
               {
                      resultIds.add(sourceId.DSE__DS_Account__c);  
                      system.debug('-------resultIds-----'+resultIds); 
               }
          } 
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!

Thanks,
Raj