You need to sign in to do that
Don't have an account?
Shanth 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;
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;
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. :)