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
Raj88Raj88 

Need help with DML requires SObject or SObject list type

I am getting this error "DML requires SObject or SObject list type: Set<Status__c>  " when i tried to save the class. This is my code.

Apex Class:
        List<Status__c> OpprtyToUpsert = new List<Status__c>(); 
        Set<Status__c> OpprtyToset = new Set<Status__c>(); 
        Status__c acc = new Status__c();
        for (OppWrapper avar : OppWrappersList )
        {
            if (avar.UpdatePrev == True)
                {
                   if (avar.acc.id != null)               
                       {                
                           avar.acc.Satisfied_By_Previous_Deal__c = true;
                           OpprtyToUpsert.add(avar.acc); 
                           OpprtyToset.addAll(OpprtyToUpsert); 
                       }
                }
             else if (avar.UpdateSta == True)
                {
                    if (avar.acc.id != null)               
                        {
                            avar.acc.Current_Status__c = selectestatus;
                            OpprtyToUpsert.add(avar.acc);
                            OpprtyToset.addAll(OpprtyToUpsert); 
                        }
                }
        }
    upsert OpprtyToset;

I am getting duplicate id error when i tried to update the records so i converted List to Set and tried to update. But i cant save the class it is showing the above error. Can anyone help me? Thanks in advance.
Best Answer chosen by Raj88
Amit Chaudhary 8Amit Chaudhary 8
Please try below code with set to check deplicate record in List. I hope this will help u.
 
List<Status__c> OpprtyToUpsert = new List<Status__c>(); 
		Set<String> setId = new Set<String>();
		
        Status__c acc = new Status__c();
        for (OppWrapper avar : OppWrappersList )
        {
            if (avar.UpdatePrev == True)
                {
                   if (avar.acc.id != null)               
                       {                
						    if(setId.contains(avar.acc.id) == false)
						    {
							   avar.acc.Satisfied_By_Previous_Deal__c = true;
							   OpprtyToUpsert.add(avar.acc); 
							   setId.add(avar.acc.id)
							}   
                       }
                }
             else if (avar.UpdateSta == True)
                {
                    if (avar.acc.id != null)               
                    {
						if(setId.contains(avar.acc.id) == false)
						{
							avar.acc.Current_Status__c = selectestatus;
							OpprtyToUpsert.add(avar.acc);
							setId.add(avar.acc.id)
						}	
                    }
                }
        }
    upsert OpprtyToUpsert;

Please let us know if this will help you.

Thanks
Amit Chaudhary

All Answers

Agustina GarciaAgustina Garcia
Your are getting the duplicate error when try to insert the List because you are adding all the time the same Status__c variabe. You are creating it outside the loop and modify it once and again inside the loop and add it  You would only need to create the new object inside the loop
 
List<Status__c> OpprtyToUpsert = new List<Status__c>(); 
Status__c acc;
for (OppWrapper avar : OppWrappersList )
{
    acc = new Status__c();
    //your code
}
upsert OpprtyToUpsert;

Hope this help
Raj88Raj88
Hi Augustina and Amit,
I have tried both your codings but still i am getting 'Duplicate id in List' error.

Thanks
Vivekh
Amit Chaudhary 8Amit Chaudhary 8
Please try below code with set to check deplicate record in List. I hope this will help u.
 
List<Status__c> OpprtyToUpsert = new List<Status__c>(); 
		Set<String> setId = new Set<String>();
		
        Status__c acc = new Status__c();
        for (OppWrapper avar : OppWrappersList )
        {
            if (avar.UpdatePrev == True)
                {
                   if (avar.acc.id != null)               
                       {                
						    if(setId.contains(avar.acc.id) == false)
						    {
							   avar.acc.Satisfied_By_Previous_Deal__c = true;
							   OpprtyToUpsert.add(avar.acc); 
							   setId.add(avar.acc.id)
							}   
                       }
                }
             else if (avar.UpdateSta == True)
                {
                    if (avar.acc.id != null)               
                    {
						if(setId.contains(avar.acc.id) == false)
						{
							avar.acc.Current_Status__c = selectestatus;
							OpprtyToUpsert.add(avar.acc);
							setId.add(avar.acc.id)
						}	
                    }
                }
        }
    upsert OpprtyToUpsert;

Please let us know if this will help you.

Thanks
Amit Chaudhary
This was selected as the best answer