3

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>';
    system.debug('subject()'+subject);
    string relatedTo = taskEventDetails.get(tsk.id);
    system.debug('relatedTo()'+relatedTo);
    string checkVal='';
    checkVal = '<img src=\'/img/checkbox_checked.gif\' alt=\'Checked\' width=\'21\' height=\'16\' class=\'checkImg\' title=\'Checked\'>';
    string ownerName = tsk.owner.Name;
    system.debug('ownerName()'+ownerName);
    string tempActDate = string.valueOf(tsk.ActivityDate.format());
    system.debug('tempActDate==='+tempActDate );
    string actDate = (tempActDate!=null && tempActDate != '')?tempActDate:'';
    system.debug('actDate()'+actDate);
    String tempLstModified = string.valueOf(tsk.LastModifiedDate.format());
    string lstModified = tempLstModified;
    system.debug('lstModified----'+lstModified);
    stringArrAW.add('["'+subject+'","'+relatedTo+'","'+checkVal+'","'+actDate+'","'+ownerName +'","'+lstModified +'"]');
    system.debug('task----stringArrAW'+stringArrAW);
}
13

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];
system.debug(t.ActivityDate.format());

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();
2
  • 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.