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 

'TasksToOpps' caused an unexpected exception

Hi guys,
I'm getting this message and our sales team cant log in the calls/tasks they make in SFDC. 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

The code for the 'TaskToOpps" is below, I not sure how to fix this.


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);
                }
            }
        }
        
     
}

 
Best Answer chosen by Andrey Vyshinskiy
Maharajan CMaharajan C
Hi Andrey,

In the production we can't edit the Apex Clases,Trigger directly.

So first we have to edit the trigger in Respective sandbox there we have to update the code.

Then using the change set or Eclipse from sandbox we have to update the code.

Change Set Reference Links:

https://help.salesforce.com/articleView?id=000171224&type=1
http://www.salesforcetutorial.com/deployments-using-change-sets/
http://salesforcesolution.blogspot.in/2011/10/how-to-deploy-any-trigger-from-sandbox.html
http://theblogreaders.com/salesforce-deployment-from-sandbox-to-production-using-change-sets/

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj

All Answers

Raj VakatiRaj Vakati
Can you share the trigger "SalesforceIsTerrible" code also ? 
 
Andrey VyshinskiyAndrey Vyshinskiy
Yes, here you go:

User-added image
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;
}
Maharajan CMaharajan C
HI Andrey,

Check the List then perform the update operation if its null then you got this error:

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);
    if(l_Tasks.size() > 0)
{
     update l_Tasks;
}

}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj


 
Andrey VyshinskiyAndrey Vyshinskiy
Hi Raj, thanks for the updated code, how do I perform the update operation in Salesforce to update this trigger, I don't seem to have an edit button that lets me update the code? Please let me know. Thanks, Andrey
Maharajan CMaharajan C
Hi Andrey,

In the production we can't edit the Apex Clases,Trigger directly.

So first we have to edit the trigger in Respective sandbox there we have to update the code.

Then using the change set or Eclipse from sandbox we have to update the code.

Change Set Reference Links:

https://help.salesforce.com/articleView?id=000171224&type=1
http://www.salesforcetutorial.com/deployments-using-change-sets/
http://salesforcesolution.blogspot.in/2011/10/how-to-deploy-any-trigger-from-sandbox.html
http://theblogreaders.com/salesforce-deployment-from-sandbox-to-production-using-change-sets/

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj
This was selected as the best answer
Andrey VyshinskiyAndrey Vyshinskiy
Thank you I will follow the links you provided to update the apex trigger.