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
deepak balur 19deepak balur 19 

Incompatible element type List for collection of Sobject...

I have a Map
Map<String, List<sObject>> r2I = new Map<String, List<sObject>>();

----code to populate the Map... The sobject belong to different objects like Lead, Account etc so the Map basically is a medley of different objects.

Now want to extract the List for bulk insert so doing the following
list<sObject> sObjInsertLists = new list<sObject>();
for (String objType : r2I.keySet()) {
  sObjInsertLists.add(r2I.get(objType);
}

insert sObjInsertLists;

I keep getting the error "Incompatible element type List for collection of Sobject...". Is there a way to solve this?
Best Answer chosen by deepak balur 19
Abhishek BansalAbhishek Bansal
Hi Deepak,

The value of your map "r2I" is List of Sobjects so when you are using this statement "sObjInsertLists.add(r2I.get(objType)" it will return you a list of Sobject not a single sobject.
You can update your code as follows in order to get rid of this issue :
list<sObject> sObjInsertLists = new list<sObject>();
for (String objType : r2I.keySet()) {
  sObjInsertLists.addAll(r2I.get(objType));
}
// addAll method will accept a list as arguement
If you want to add a single sobject then you can use below code :
list<sObject> sObjInsertLists = new list<sObject>();
for (String objType : r2I.keySet()) {
  sObjInsertLists.addAll(r2I.get(objType)[0]);
}
Please let me know if you need more information on this.

Thanks,
Abhishek Bansal

All Answers

Abhishek BansalAbhishek Bansal
Hi Deepak,

The value of your map "r2I" is List of Sobjects so when you are using this statement "sObjInsertLists.add(r2I.get(objType)" it will return you a list of Sobject not a single sobject.
You can update your code as follows in order to get rid of this issue :
list<sObject> sObjInsertLists = new list<sObject>();
for (String objType : r2I.keySet()) {
  sObjInsertLists.addAll(r2I.get(objType));
}
// addAll method will accept a list as arguement
If you want to add a single sobject then you can use below code :
list<sObject> sObjInsertLists = new list<sObject>();
for (String objType : r2I.keySet()) {
  sObjInsertLists.addAll(r2I.get(objType)[0]);
}
Please let me know if you need more information on this.

Thanks,
Abhishek Bansal
This was selected as the best answer
deepak balur 19deepak balur 19
You are right, I was able to resolve this yesterday but thank you for your response.
Venkateshwar Reddy GiriVenkateshwar Reddy Giri
Hi,
My requirement is to attach the files in notes and attachments of Account object.When the files get attached,the check box field should be checked.
i have tried to achieve my requirement with the trigger below,but I keep getting the error "Incompatible element type Id for collection of Account" Is there any way to solve this?

trigger AttachmentTriggerDemo on Attachment (before insert) {
    List<account>Li = new List<account>();
    Set<account>Ids = new Set<account>();
    for(Attachment att : trigger.New){
         //Check if added attachment is related to Account or not
         if(att.ParentId.getSobjectType() == Account.SobjectType){
              Ids.add(att.ParentId);
         }
    }
    Li = [select id,name,File_Attached__c from Account where id in : Ids];
    if(Li!=null && Li.size()>0){
        for(Account acc : Li){
            acc.File_Attached__c = true;
        }
        update Li;
    }



Thanks in Advanvce
Venkateshwar Reddy GiriVenkateshwar Reddy Giri
Hi,
 
public class AttachmentTriggerHandler 
{
    public void checkAttachments(Attachment[] attlist)
    {
        set<Id> conIds = new set<Id>();
        for(Attachment a : attList){
            if(a.ParentId.getSObjectType() == Contact.SObjectType)
            {
                conIds.add(a.ParentId);
            }
        }
        List<Contact> conList = [SELECT Id,Files_Attached__c FROM Contact WHERE Id IN : conIds];
        AggregateResult[] results= [SELECT ParentId conId,Count(id) tot FROM Attachment WHERE ParentId IN : conIds GROUP BY ParentId];
        Map<Id,Double> ContactIdTotAttach = new Map<Id,Double>();
        for(AggregateResult r : results)
        {
            ContactIdTotAttach.put((Id)r.get('conId'),(Double)r.get('tot'));   
        }
        List<Contact > ContactsToUpdate = new List<Contact >();
        for(Contact c : conList)
        {
            Double totContacts = 0;
            if(ContactIdTotAttach .containsKey(c.Id))
            {
                totContacts = ContactIdTotAttach.get(c.Id);
                if(totContacts==2)
                {
                    c.Files_Attached__c= true;
                }
                else
                {
                    c.Files_Attached__c= false;
                }
                ContactsToUpdate.add(c);
            }
        }
        update ContactsToUpdate ;
    }



how to write a test class for this apex class.can anyone help me out please?