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 

How do I fix this error? Apex trigger SalesforceTrigger Error caused an unexpected exception. First error: cannot_insert_update_activate_entitiy, taskstoopps: exectionbeforeupdae casused by> System.NullPointerException .

One of our sales folks user who tried to create a task on a contact level got this message : Apex trigger SalesforceTrigger Error caused an unexpected exception. First error: cannot_insert_update_activate_entitiy, taskstoopps: exectionbeforeupdae casused by> System.NullPointerException . 

How do I fix this, I'm new to Salesforce. Any feedback would help. 
Raj VakatiRaj Vakati
Can you share code along with error screenshot to fix 
Andrey VyshinskiyAndrey Vyshinskiy
User-added image


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);
                }
            }
        }
        
     
}
Raj VakatiRaj Vakati
the error is coming from SalesforceIsTerriable trigger 
Andrey VyshinskiyAndrey Vyshinskiy
I see, thanks for the feedback. Do I need to edit the trigger, I'm guessing?