1

When I used to create the record in Event object it is showing error like:

Error: Invalid Data.
Apex trigger General_Event_Trigger caused an unexpected exception, contact your administrator: General_Event_Trigger: execution of BeforeINsert caused by: System.NullPointerException: Attempt to de-reference a null object: Class.EventClass.updateNewCustomFieldsEvent: line 369, column 1

Trigger code:

trigger General_Event_Trigger on Event (...)
{
    if (trigger.isBefore)
    {
        if (trigger.isInsert)
        {
            EventClass.updateNewCustomFieldsEvent(trigger.new);
        }
        else if (trigger.isUpdate)
        {
            EventClass.updateNewCustomFieldsEvent(trigger.new);
        }
    }
}

Apex class:

public static void updateNewCustomFieldsEvent(List<Event> eventList)
{
    for (Event eve : eventList)
    {
        if (Administrators__c.getInstance().Update_field_Last_Modified_End_User__c)
        {
            String userIdString = eve.LastModifiedById;
            eve.Last_Modified_By_End_User__c = userIdString.subString(0, 15);
            eve.Last_Modified_Date_End_User__c = eve.LastModifiedDate;
        }
    }
}
1
  • In the future, please include all error messages and code as text. It is significantly more accessible for the community than images. – Adrian Larson Feb 11 '17 at 17:38
6

I just checked lastmodified by id is null on a before insert event and this makes sense.

For example (hastily written trigger based approach, not best practice!):

trigger eventTrigger on Event (before insert) {
    system.debug(logginglevel.WARN, '^^^ event last modified: '+trigger.new[0].LastModifiedById);
    system.debug(logginglevel.WARN, '^^^ userinfo:  '+userinfo.getUserId());
}

returns:

11:38:56:001 USER_DEBUG [2]|WARN|^^^ event last modified: null

11:38:56:001 USER_DEBUG [3]|WARN|^^^ userinfo: 00510000001QaKjAAK

While the null check approach is valid you would end up with blank data, alternatively using the userinfo class would give you the current user and therefore the user creating the record.

3
  • line No : 369 as eve.Last_Modified_By_End_User__c = userIdString.subString(0, 15); eve.LastModifiedDate is not created in before insert,then why it is showing error in 369,it should have to show error in 370 only @Girbot – SFDC Feb 13 '17 at 5:51
  • Because you are trying to use substring on null. – Girbot Feb 13 '17 at 6:53
  • what i have to do to rectify the error? substring on Null means, I am not able to understand ,can you please tell for small scenario for my understand – SFDC Feb 13 '17 at 7:54
3

You should check if variable is not null or empty

eve.Last_Modified_By_End_User__c = String.isBlank( userIdString ) ? '' : userIdString.substring(0, 15)

However, like other users reasonably suggest, on before trigger userIdString might be always empty.

1
  • line No : 369 as eve.Last_Modified_By_End_User__c = userIdString.subString(0, 15); eve.LastModifiedDate is not created in before insert,then why it is showing error in 369,it should have to show error in 370 only @Patlaus – SFDC Feb 13 '17 at 5:51

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.