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
Shanth Shenoy 3Shanth Shenoy 3 

Apex Interview Question

Hi Friends from the Salesforce Forum,
I was asked this interview question (I am new to Salesforce hence I fubbed the Interview :-( , heres the question, can I request for help so I can be better prepared next time over. Thank you in advance.

-- Findout Issues in this code, Bad practices in this Method
*********************************************************************************
Private Map<ID,String> updateAccounts(List<Account>(accList){
Map<Id, Account> updatedAccs = new Map<Id,Account>(accList);
Set<String> validAccTypes = new Set<String>{'Prospect', 'Customer'};
try{ 
Map<Id, String> errorMap = new Map<Id, String> ();
 If(accList == null || accList.size() ==0)
Return errorMap;
For(Account acc:accList){
if(acc.DunsNumber.Lenght >9){
errorMap.put(acc.Id, 'Duns Number more than 9 Characters';
updatedAccs.remove(acc.Id);
}
} else If(acc.AccountNumber.Lenght >40){
   errorMap.put(acc.Id, 'Account has more than 40 Characters');
   updatedAccs.remove(acc.Id);
} else If(validAccTypes.contains(acc.Type)){
  errorMap.put(acc.Id, 'Account Type is not Valid');
  updatedAccs.remove(acc.Id);
}
Update updatedAccs;
} catch(Exception ex)
  System.debug(ex.getMessage()0;
}
Return errorMap;

 
Best Answer chosen by Shanth Shenoy 3
Bryan Leaman 6Bryan Leaman 6
Did you type this in or copy/paste? There are mis-matched brackets and parens to begin with. The first "else" appears to be after the if and for loop are both closed. Typo on "Lenght" instead of "Length".

Bad practice:
- No comments (imo)
- Not checking for null before accessing field lengths -- could result in attempt to dereference a null object error.
- multiple return points (imo)
- return after function body is closed (mis-matched brackets)
- tests for multiple errors but because of if/else structure, only the first error is returned
- catching any exception and simply ignoring the error (log to debugger only) 

There may be more. :)