Skip to content

Instantly share code, notes, and snippets.

@swapnilshrikhande
Created July 17, 2017 07:40
Show Gist options
  • Save swapnilshrikhande/8f47a0e94178c09e41c251fb16507af4 to your computer and use it in GitHub Desktop.
Save swapnilshrikhande/8f47a0e94178c09e41c251fb16507af4 to your computer and use it in GitHub Desktop.
Clean method to remove nulls from sobject list
Account firstAccount = new Account(Name='Test 5');
Account secondAccount = new Account(Name='Test 6');
Account thirdAccount = new Account(Name='Test 7');
List<Account> accountList = new List<Account>{null, firstAccount, null, secondAccount, null, null , thirdAccount, null };
List<Sobject> clean(List<SObject> inputSobjectList){
Integer listSize = inputSobjectList.size();
for(Integer index=0;index < listSize; ++index){
if( inputSobjectList[index] == null ){
inputSobjectList.remove(index);
--listSize;
--index;
}
}
return inputSobjectList;
}
insert clean(accountList);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment