I am iterating a list of task and converting its fields in string, all fields are converted into string except activitydate. It is showing strange behaviour, out of 10 tasks only 3 tasks are coming in table because there is some issue with activitydate.

for(task tsk : tasksToUpdate)
    string subject = '<a href=\''+System.URL.getSalesforceBaseURL().toExternalForm()+'/'+tsk.id+'\'  target=\'_blank\' title=\''+tsk.subject+'\'>' + tsk.subject + '</a>';
    string relatedTo = taskEventDetails.get(tsk.id);
    string checkVal='';
    checkVal = '<img src=\'/img/checkbox_checked.gif\' alt=\'Checked\' width=\'21\' height=\'16\' class=\'checkImg\' title=\'Checked\'>';
    string ownerName = tsk.owner.Name;
    string tempActDate = string.valueOf(tsk.ActivityDate.format());
    system.debug('tempActDate==='+tempActDate );
    string actDate = (tempActDate!=null && tempActDate != '')?tempActDate:'';
    String tempLstModified = string.valueOf(tsk.LastModifiedDate.format());
    string lstModified = tempLstModified;
    stringArrAW.add('["'+subject+'","'+relatedTo+'","'+checkVal+'","'+actDate+'","'+ownerName +'","'+lstModified +'"]');

I'd expect this code to crash, because ActivityDate can be null, which means that tsk.ActivityDate.format() should throw a NullPointerException. I can demonstrate this through the following code:

Task t = [select ActivityDate from Task where ActivityDate = null limit 1];

You do end up eventually checking for a null value, but it's already too late; the code would have thrown its exception before then. Instead, you should do something like this:

String tempActDate = tsk.ActivityDate==null?:'':tsk.ActivityDate.format();
  • you know what, you have just saved my life thanks man..
    – Anzar
    Nov 15 '16 at 18:36
  • 1
    @Anzar If this answer has solved your question you can use the check mark next to it to mark it as such. That will help others who are looking at it to locate the accepted solution. Nov 15 '16 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.