Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void createTimeSheet() {
- errorMsg = '';
- TimeSheet__c newTimeSheet = new TimeSheet__c(
- Date__c = date.valueOf(newTimeSheetDate),
- Status__c = 'New',
- Employee__c = [SELECT Id FROM Employee__c WHERE Name=:EmployeeName].Id
- );
- try {
- insert newTimeSheet;
- //catching the DML exceptions first
- } catch(DmlException e) {
- errorMsg = e.getDmlMessage(0);
- ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,errorMsg));
- //catching all other exceptions
- } catch(Exception e) {
- errorMsg = e.getMessage();
- ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,errorMsg));
- }
- }
- public static testMethod void createTimeSheetNullPointerFail_test() {
- init();
- cont.EmployeeName = testEmployeeName;
- cont.tslist = testTsList;
- test.startTest();
- cont.createTimeSheet();
- test.stopTest();
- System.assert(cont.errorMsg.contains('System.NullPointerException: Attempt to de-reference a null object'), cont.errorMsg);
- }
- try {
- Date.valueOf((String) null);
- } catch (Exception e) {
- System.debug('>>> ' + e.getMessage());
- }
- public void createTimeSheet() {
- errorMsg = '';
- List<Employee__c> foundEmployees = [SELECT Id FROM Employee__c WHERE Name=:EmployeeName];
- if (foundEmployees.size() > 0) {
- TimeSheet__c newTimeSheet = new TimeSheet__c(
- Date__c = date.valueOf(newTimeSheetDate),
- Status__c = 'New',
- Employee__c = foundEmployees[0].Id
- );
- }
- else {
- // handle scenario here where the employee wasn't found, show error on page possibly
- // also consider adding else if scenario if more than 1 employee is found that matches the name
- }
- // TODO evaluate if try/catches are still needed
- try {
- insert newTimeSheet;
- //catching the DML exceptions first
- } catch(DmlException e) {
- errorMsg = e.getDmlMessage(0);
- ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,errorMsg));
- //catching all other exceptions
- } catch(Exception e) {
- errorMsg = e.getMessage();
- ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,errorMsg));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement