-1

I have the following method that runs on beforeUpdate and beforeInsert Triggers on Event:

private void populateMeetingStatus()
{
    System.debug('populateMeetingStatus');
    for (Event event_i : (List <Event>) Trigger.new)
    {
        if(event_i.Event_Details__C != null)
        {
            if(event_i.Meeting_Status__c == 'Completed')
            {
                //"Event Locked" - The field should be True if Meeting status is Completed
                System.debug('event_i.Event_Details__c:' + event_i.Event_Details__c);
                System.debug('event_i.Event_Details__r.Event_Locked__c:' + event_i.Event_Details__r.Event_Locked__c);

                event_i.Event_Details__r.Event_Locked__c = true;

            }
            else
            {
                //"Event Locked" - The field should be false
                event_i.Event_Details__r.Event_Locked__c = false;
            }
        }
    }

it fails on the line

event_i.Event_Details__r.Event_Locked__c = true;

with the error

System.NullPointerException: Attempt to de-reference a null object

i printed the value of this field:

10:22:49:172 USER_DEBUG [1512]|DEBUG|event_i.Event_Details__c:a1G9E000000InGTUA0
10:22:49:172 USER_DEBUG [1513]|DEBUG|event_i.Event_Details__r.Event_Locked__c:false

this fiels exists, and still i receive this error. why does this occur? how can i solve it?

5
  • You need to add a null to lookup field before accessing lookup’s field.
    – Raul
    Dec 31 '17 at 9:51
  • @RahulSharma What do you mean add a null?
    – Dana Griff
    Dec 31 '17 at 10:00
  • Before accessing the reference field with ‘__r’ do a nill check on ‘__c’ field like ‘event_i.Event_Details__c != null’
    – Raul
    Dec 31 '17 at 10:03
  • updated the code, still occurs
    – Dana Griff
    Dec 31 '17 at 10:08
  • 2
    I've added a handful of duplicate questions that should provide the information you're looking for. Relationships are not available automatically, but instead you have to query for them. You also need an explicit DML update in order to save those changes. There's plenty of other questions on this site as well with similar answers, so please take some time to do a search or two for the information you're looking for.
    – sfdcfox
    Dec 31 '17 at 11:43
2

I think you have to use "after Insert" and "after Update" trigger events. Why because in below line you are referring Parent Object field

     event_i.Event_Details__r.Event_Locked__c = true;

Use system.debug to find the value of that statement

 system.debug('event locked >>>>'+ event_i.Event_Details__r.Event_Locked__c);

and Also refer below links:

https://help.salesforce.com/articleView?id=000063739&type=1.

and also make method "Public" once for Testing Purpose(Itsnt important but just try it)

Something liKE THIS:

 trigger EventdetailsUpdate on yourobjectApi(after insert,after Update){
 if((trigger.isInsert||trigger.isUpdate)&&trigger.isAfter)){
   EventClass.eventDetails(trigger.new);
   }




    public class EventClass{
     public static  void eventDetails(List<yourobjectApi> eventList)
  {
     set<id>acid=new set<id>();
List<Event_Details__C> eventList=new List<Event_Details__C>();
for (Event event_i:eventList))
{
    if(event_i.Event_Details__C != null  )
    {
        if( event_i.Meeting_Status__c == 'Completed')
        {
              acid.add(event_i.Id);
       }    
 }}
              List<Event_Details__C> eventList=[select id,Event_Details__C from Event_Details__C where event_i.Event_Details__C in:acid];
       for(Event_Details__C ev:eventList){ 
        ev.Event_Details__r.Event_Locked__c = true;
         eventList.add(ev);}
            if(!eventList.isempty()){update eventList;}}
                 }
2
  • The code sample is inaccurate, because Meeting_Status__c is part of event, but i took your idea and modified it. thanks a lot!
    – Dana Griff
    Dec 31 '17 at 12:53
  • Okay No problem..I dont know your object structure ..its brief Idea I had given to you.Thanks Dec 31 '17 at 14:16

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