function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andrey VyshinskiyAndrey Vyshinskiy 

Apex trigger SalesforceIsTerrible caused an unexpected exception

I'm getting this message and our sales team cant log in the calls they make. Does anybody know how to fix this? 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger SalesforceIsTerrible caused an unexpected exception, contact your administrator: SalesforceIsTerrible: execution of BeforeInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 00Tf100002NostKEAR; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, TasksToOpps: execution of BeforeUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.TasksToOpps: line 40, column 1: []: Trigger.SalesforceIsTerrible: line 27, column 1
David @ ConfigeroDavid @ Configero
You have a variable (for code you haven't provided) that has a value of null and you are trying to reference a value on this value.  You have a stack trace in your error message, so check the line and see why the variable is set to null.  You either need to fix the problem of why it is null, or check to ensure it is not null before evaluating a property/field on the object.

Please mark this answer as correct if it helps you.
Andrey VyshinskiyAndrey Vyshinskiy
I checked and have not found the issue, I have the apex trigger code below, can you let me know if you see the issue in the code? I'm new to SFDC and have not worked with apex trigger before.

SalesforceIsTerrible code
trigger SalesforceIsTerrible on Task (before insert) {
      System.debug('hello');
      list<Task> l_Tasks = new list<Task>();
      set<ID> current_Tasks = new set<ID>();
      
      for(Task t:Trigger.new) {
          current_Tasks.add(t.id);
          System.debug(t.id);
      }
      
      
      date target_date = System.today();
      System.debug(target_date);
      
      l_Tasks = [SELECT ID
                FROM Task
                Where 
                createddate >=:Date.Today().addDays(-1)
                And WhatId = null
                AND Id NOT IN :current_Tasks
                And (Type = 'Email' Or Type ='ClearSlide Email Pitch')
                And (OwnerId != '005j000000CFBjr' AND OwnerId != '005j000000BlMOj')
                AND WhoID != null
                ];
     System.debug(l_Tasks);
     System.debug(current_Tasks);
     update l_Tasks;
}
David @ ConfigeroDavid @ Configero
Please post the code for the trigger named 'TasksToOpps'
Andrey VyshinskiyAndrey Vyshinskiy
Here you go: 

Apex Trigger TasksToOpps

trigger TasksToOpps on Task (before insert, before update) {
            
        /***************
        * Variables
        ***************/
        list<Task> l_Tasks = new list<Task>(); // Tasks we'll be updating
        set<ID> s_ContactIDs = new set<ID>(); // Set of Contact IDs
        set<ID> s_AccountIDs = new set<ID>(); // Set of Account IDs
        set<ID> s_CCAccountIDs = new set<ID>(); // Set of Account IDs of Current Clients
        
        /***************
        * Get all the nerds we reach out to 
        ***************/
        for(Task t:Trigger.new) {
             
            // Add Task to working list and collect the Contact ID
            if (t.WhatId == null &&(t.Type == 'Email' | t.Type =='ClearSlide Email Pitch') && t.WhoId != null) {
                // only for Contacts
                if (String.valueOf(t.WhoId).startsWith('003')){
                    l_Tasks.add(t);
                    s_ContactIDs.add(t.WhoId);
                    System.debug('WhoId:' + t.WhoId);
                }
            }
        }
        System.debug(s_ContactIDs);
        
        //Where do you work
        map<ID, Account> map_cID_to_aID = new map<ID, Account>();
            for (Contact c:
                [SELECT ID, Account.id, Account.name, Account.Account_Status__c
                FROM Contact
                Where Id in :s_ContactIDs
                ]){
                map_cID_to_aID.put(c.ID, c.Account);
                }
        System.debug(map_cID_to_aID);
        
        for (ID c:s_ContactIDs) {
            s_AccountIDs.add(map_cID_to_aID.get(c).Id);
        }
        System.debug(s_AccountIDs);
        
        for (ID cc:s_ContactIDs) {
            if (map_cID_to_aID.get(cc).Account_Status__c == 'Current Client'){
                s_CCAccountIDs.add(map_cID_to_aID.get(cc).Id);
            }
        }
        System.debug(s_CCAccountIDs);
        
        // Maps Account ID to an Opportunity ID
        map<ID, ID> map_aID_to_oID = new map<ID, ID>();
            // Query for the Contact's Open Opportunities. Sort by CloseDate DESC so the Task gets assigned to the earliest Opportunity as it loops
            for (Opportunity o:[select Id, AccountId
                               from Opportunity 
                               where AccountId in :s_AccountIDs 
                               AND Opportunity.IsClosed = false
                               order by Opportunity.CloseDate DESC
                               ]) {
                map_aID_to_oID.put(o.AccountId, o.Id);
            }
        System.debug(map_aID_to_oID);
        
        for (Task t:l_Tasks) {
            if (map_aID_to_oID.get(map_cID_to_aID.get(t.WhoId).id) == null && s_CCAccountIDs.contains(map_cID_to_aID.get(t.WhoId).id)== False){
                System.debug(t);
                System.debug(t.OwnerId);
                System.debug(t.Account);
                System.debug(s_CCAccountIDs.contains(map_cID_to_aID.get(t.WhoId).id));
                //need to put in Jess and My IDs once I am done testing. Confirmed to work though.
                if (t.OwnerId != '005j000000CFBjr' && t.OwnerId != '005j000000BlMOj'){
                    Opportunity opp = new Opportunity();
                    opp.AccountId = map_cID_to_aID.get(t.WhoId).Id;
                    opp.name = map_cID_to_aID.get(t.WhoId).name;
                    opp.StageName = '0 - Cold';
                    opp.OwnerId = t.OwnerId;
                    opp.CloseDate = system.today().addMonths(3);
                    System.debug(opp);
                    insert opp;
                    map_aID_to_oID.put(opp.AccountID, opp.Id);
                   }
            
            }
        }
        
        for (Task t:l_Tasks) {
            if (map_cID_to_aID.get(t.WhoId) != null) {
                System.debug('first map check');
                if (map_aID_to_oID.get(map_cID_to_aID.get(t.WhoId).Id) != null){
                    System.debug('Second map check');
                    System.debug(map_aID_to_oID.get(map_cID_to_aID.get(t.WhoId).Id));
                    t.WhatId = map_aID_to_oID.get(map_cID_to_aID.get(t.WhoId).Id);
                }
            }
        }
        
     
}