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
Nandhini S 3Nandhini S 3 

DML Exception

Hi Team,

I'm checking if my list is empty before the update. My list returns null but still the If condition is resulting true and DML statement is throwing error.
If(myList != null && !myList.isempty())
{
update myList;
}

What am i doing wrong here
Nandhini S 3Nandhini S 3
myList.size() returns 1 but there is nothing in the list.
ShirishaShirisha (Salesforce Developers) 
Hi Nandhini,

Greetings!

The list should be initialized to use isEmpty() otherwise you can use myList.size()>0 to check,if the list is empty or not.

Reference:https://salesforce.stackexchange.com/questions/77173/list-isempty-vs-list-size-0-vs-list-null/77200

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri

 
Nandhini S 3Nandhini S 3
Hi Shirisha,
My code goes something like this

List<Project__c> myList = new List<Project__c>();
for(project__c proj: newList)
{
project__c thisProject = new project();
if(some condition)
{
some field assignments
}
myList.add(thisProject);
}
if(myList.size()>0)
{
update myList;
}

My if condition is not satisfied and there is nothing in the myList but when i check the size it shows 1.