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
Priti jaiswal 8Priti jaiswal 8 

Get attachment parent ownerid via soql

Hello All,

I am trying to get the attachment parent owner id via SOQL, but it did not return any data.
Currently i have written individual query for each object to get the ownerId.
The problem here is, if we are having more than 10 parent objects then we have to write individual SOQL for each object, which looks quote ugly.
Any suggestion or solution is much appreciated.

Thanks in advance!!
Arshadulla.ShariffArshadulla.Shariff

Hello Priti,

Try with the following code it may help you.
The only limitation is parent objects should not be greater than 100.

 

/*Anonymous block code*/
Map<String,String> keys = new Map<String,String>();
Map<String,Schema.SobjectType> describe = Schema.getGlobalDescribe();
for(String s:describe.keyset())
keys.put(describe.get(s).getDescribe().getKeyPrefix(),s);
Map<String,set<Id>> parentIds_sObjectMap= new Map<String,set<Id>>();
for(Attachment att:[select Name,parentid From Attachment]){
    set<id> parentIds = new set<Id>();
    String parentId_Prefix =String.valueof(att.parentid).substring(0,3);
    if(keys.get(parentId_Prefix)!= null ){
        if(parentIds_sObjectMap.get(keys.get(parentId_Prefix)) != null)
        parentIds = parentIds_sObjectMap.get(keys.get(parentId_Prefix));
        parentIds.add(att.parentid);
        parentIds_sObjectMap.put(keys.get(parentId_Prefix),parentIds);
    }
}
Map<Id,Id> parentId_To_OwnerId = new Map<Id,Id> ();
if(parentIds_sObjectMap != null && parentIds_sObjectMap.keyset().size()<100){
    for(String sobjectName :parentIds_sObjectMap.keySet()){
        SET<ID> keys_Id = parentIds_sObjectMap.get(sobjectName);
    String Query = 'Select id,OwnerId FROM '+sobjectName+'  Where Id in :keys_Id ';
        for(sObject sObj :Database.Query(Query))
            parentId_To_OwnerId.put((ID)sObj.get('Id'),(Id)sObj.get('OwnerId'));
        
    }​
/*The following map will give you parentId map with ownerId*/
    System.debug('DEBUG OUPTUT --> parentId_To_OwnerId  '+parentId_To_OwnerId);
}
 


Any bugs we can overcome, need to spend time on the above code.

I hope this helps you if its please mark it solved.
Thanks
Arshad

 

Priti jaiswal 8Priti jaiswal 8
Hi Arshad,

Thanks for the response!!

The issue that i can see here is Database.Query(Query) is inside for loop. I our code we are stuck at this point only. 

for(String sobjectName :parentIds_sObjectMap.keySet())
{    
    SET<ID> keys_Id = parentIds_sObjectMap.get(sobjectName);
    String Query = 'Select id,OwnerId FROM '+sobjectName+'  Where Id in :keys_Id ';  
     for(sObject sObj :Database.Query(Query))  
           parentId_To_OwnerId.put((ID)sObj.get('Id'),(Id)sObj.get('OwnerId'));   
}​

Regards,
Priti
Arshadulla.ShariffArshadulla.Shariff
Hi,
I do understand that I've broken the best practice principle, but to overcome code breaking.
i have used entry condition probaly you can modify as per your requirement.
 if(parentIds_sObjectMap != null && parentIds_sObjectMap.keyset().size()<100){

  }
Since Soql call inside for loop will throw Exception on 101 soql call.
Need to dedicate more time for resolving the issue.

Thanks 
Arshad
Arshadulla.ShariffArshadulla.Shariff
Hello Priti,
Did you figure out the issue, let us know the solution.
So that it can help the community.

Thanks.