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
Sean BarczynskiSean Barczynski 

Task not getting created, email not sending, other piece of IF statement firing

Good afternoon,

I'm struggling with some code that I wrote not producing the results I'm looking for.  The section I want to focus on is the creation of the Scan Tax Docs task.  When I check the box for Tax Docs Received and hit save, the test email is not being sent (the others in the else statements are sent when applicable) and the task is not being created, but the box for Scan Tax Docs Task Created is being checked (I confirmed that this is the line that is checking this box - I commented it out and it stopped getting checked).

Please help!  Thanks in advance...
 
trigger TaxServiceBeforeUpdate on Tax_Services__c (before update) {


    Set <Id> WhatIdSet = new Set <Id> ();
    
    for(Tax_Services__c t : trigger.new)
    {
    	if(t.Business_Account__c != NULL)
    	{
        	WhatIdSet.add(t.Business_Account__c);
    	}
    }
                                                         
    List<User> userList = [select id, 
    							  Name, 
    							  Email, 
    							  Title, 
    							  CompanyName, 
    							  Phone 
    						 from User];
	
	List<Contact> contactList = [select Id,
    									Name,
    									AccountId,
                                        Email,
                                        FirstName,
                                        ACTDDEV__Nickname__c,
                                        Include_on_Correspondence__c,
                                        Primary__c
                                   from Contact
                                  Where AccountId in :WhatIdSet];
	
	List<Task> serviceTaskList = [select Id,
                                       	 Subject,
                                         Status,
                                         WhatID,
                                         ActivityDate,
                                         Hidden__c
                                    from Task
                                   Where WhatId in :WhatIdSet
                                     and Subject = 'URGENT! Request Tax Docs from Client'];
	
	List<OrgWideEmailAddress> owaList = [select id, 
    											DisplayName, 
    											Address 
    									   from OrgWideEmailAddress];
	
	String taxPlanning = Schema.SObjectType.Tax_Services__c.getRecordTypeInfosByName().get('Tax Planning').getRecordTypeId();
    String taxPrep = Schema.SObjectType.Tax_Services__c.getRecordTypeInfosByName().get('Tax Prep').getRecordTypeId();
    
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    List <Task> TasksToCreate = new List <Task> ();
    List <Task> tasksToUpdate = new List <Task> ();
    
    String greeting;
    String email;
    String newBody2;
    String PrimaryTaxPayer;
	String SecondaryTaxPayer;
	String TaxPayers;
	
	List <String> emailAddresses = new List <String> ();
    
    User sinkrote;
    User sdonatelli;
    User TA;
    OrgWideEmailAddress owa;

    for(Tax_Services__c e : Trigger.new)
    {
    	Tax_Services__c oldservice = Trigger.oldMap.get(e.ID);
    	
    	/***********************************************************************
    	*
    	*	Determining values for numerous pieces of vital information:
    	* 		Tax Advisor
    	*		Tax Admin
    	*		Names of Tax Payers
    	*
    	***********************************************************************/
    	
    	for(Integer u=0;u<=userList.size()-1;u++)
        {
        	for(Integer o=0;o<=owaList.size()-1;o++)
        	{
        		if(userList[u].Id == e.Tax_Advisor__c && userList[u].Email == owaList[o].Address)
        		{
        			owa = owaList[o];
        			TA = userList[u];
        		}
        	}
        	
        	if(userList[u].Email == 'sd****mg.com')
        		sdonatelli = userList[u];
        	
        	if(userList[u].Email == 'si****mg.com')
        		sinkrote = userList[u];
       	}
  	  	
  	  	for(Integer c=0; c<=contactList.size()-1; c++)
        {
        	if(e.Business_Account__c == contactList[c].AccountId && contactList[c].Include_on_Correspondence__c && contactList[c].Email != NULL)
            {
               	emailAddresses.add(contactList[c].Email);
                    
				if(contactList[c].ACTDDEV__Nickname__c != NULL)
					greeting = contactList[c].ACTDDEV__Nickname__c;
				else
					greeting = contactList[c].FirstName;
			}
                
			if(e.Tax_Payer_1__c == contactList[c].Id)
				PrimaryTaxPayer = contactList[c].Name;
			if(e.Tax_Payer_2__c == contactList[c].Id)
				SecondaryTaxPayer = contactList[c].Name;
		}
                               
		if(SecondaryTaxPayer != NULL)
			TaxPayers = PrimaryTaxPayer + ' & ' + SecondaryTaxPayer;
		else
			TaxPayers = PrimaryTaxPayer;
    	
    	/***********************************************************************
    	*
    	*	Tax Prep Workflow
    	*
    	***********************************************************************/
    	
    	if(e.RecordTypeId == taxPrep)
    	{
    		/* Updating Status */
    		
    		if(e.Client_Lost__c)
  	  			e.Status2__c = 'Client Lost';
    		
    		if(e.Tax_Docs_Received__c == TRUE && e.Tax_Docs_Scanned__c == FALSE)
        	{
            	if(e.Tax_Docs_Received__c != oldservice.Tax_Docs_Received__c || e.Tax_Docs_Scanned__c != oldservice.Tax_Docs_Scanned__c)
            	{
    				e.Status2__c = 'Docs Received';
					e.Status_Date__c = system.Today();
					e.Tax_Docs_Received_Date__c = system.Today();
            	}
        	}
        	
        	if(e.Tax_Docs_Received__c == TRUE && e.Tax_Docs_Scanned__c == TRUE)
        	{
            	if(e.Tax_Docs_Received__c != oldservice.Tax_Docs_Received__c && e.Tax_Docs_Scanned__c != oldservice.Tax_Docs_Scanned__c)
            	{
                	e.Status2__c = 'Docs Scanned';
					e.Status_Date__c = system.Today();
					e.Tax_Docs_Scanned_Date__c = system.Today();
            	}
            	
            	if(e.Tax_Docs_Received__c != oldservice.Tax_Docs_Received__c)
            		e.Tax_Docs_Received_Date__c = system.Today();
        	}
        	
        	if(e.X8879_Given_to_Client__c && oldservice.X8879_Given_to_Client__c != e.X8879_Given_to_Client__c)
			{
				e.Status2__c = 'Waiting for 8879';
				e.Status_Date__c = system.Today();
			}
		
			if(e.Return_Filed__c && oldservice.Return_Filed__c != e.Return_Filed__c)
			{
				e.Status2__c = 'Filed';
				e.Status_Date__c = system.Today();
			}
			
			if(e.TARCT_Return_Locked_Completed__c && oldservice.TARCT_Return_Locked_Completed__c != e.TARCT_Return_Locked_Completed__c)
			{
				e.Status2__c = 'Final';
				e.Status_Date__c = system.Today();
				e.Tax_Return_Closed_Date__c = system.Today();
			}
			
			/* Creating Tasks */
			
			if(e.Tax_Docs_Received__c && !e.Tax_Docs_Scanned__c && !e.Scan_Tax_Docs_Task_Created__c)
        	{
            	if(e.Tax_Docs_Received__c != oldservice.Tax_Docs_Received__c || e.Tax_Docs_Scanned__c != oldservice.Tax_Docs_Scanned__c)
            	{
            		Messaging.SingleEmailMessage mail2 = new Messaging.SingleEmailMessage();
                    String[] toAddresses = new String[] {emailAddresses[0]};
                    mail2.setOrgWideEmailAddressId(owa.Id);
                    mail2.setToAddresses(toAddresses);
  					mail2.setSubject('Test 1');
  					String body2 =  'Hi ' + greeting + ',';
  					mail2.setHtmlBody(body2);
  										   
  					mails.add(mail2);
            		
            		Date ScanDocsDueDate = Date.today();
                			
        			if(e.On_Extension__c)
        				ScanDocsDueDate = Date.newInstance(Date.today().year(),4,18);
        			else
        				ScanDocsDueDate = Date.today();
        		
            		TasksToCreate.add(new Task(OwnerID = sinkrote.Id,
                                       Subject = 'Scan Tax Docs',
                                       WhatID = e.Id,
                                       ActivityDate = ScanDocsDueDate,
                                       Status = 'Not Started',
                                       Priority = 'Normal',
                                       Category__c = 'Client/Prospect Work',
                                       Hidden__c = 'Scan Tax Docs'));
                                       
                    e.Scan_Tax_Docs_Task_Created__c = TRUE;
            	}
            	else
            	{
            		Messaging.SingleEmailMessage mail2 = new Messaging.SingleEmailMessage();
                        String[] toAddresses = new String[] {emailAddresses[0]};
                        mail2.setOrgWideEmailAddressId(owa.Id);
                        mail2.setToAddresses(toAddresses);
  						mail2.setSubject('Test 2');
  						String body2 =  'Hi ' + greeting + ',';
  						mail2.setHtmlBody(body2);
  										   
  						mails.add(mail2);
            	}
        	}
        	else
        	{
        		Messaging.SingleEmailMessage mail2 = new Messaging.SingleEmailMessage();
                        String[] toAddresses = new String[] {emailAddresses[0]};
                        mail2.setOrgWideEmailAddressId(owa.Id);
                        mail2.setToAddresses(toAddresses);
  						mail2.setSubject('Test 3');
  						String body2 =  'Hi ' + greeting + ',';
  						mail2.setHtmlBody(body2);
  										   
  						mails.add(mail2);
        	}
        	
        	if(e.Tax_Docs_Received__c && e.Tax_Docs_Scanned__c && !e.Complete_Tax_Return_Task_Created__c && e.Tax_Docs_Scanned__c != oldservice.Tax_Docs_Scanned__c)
        	{
        		Date CompleteReturnDueDate;
	                		
        		if(e.Tax_Prep_Meeting_Date__c != NULL)
        		{
        			if(e.Tax_Prep_Meeting_Date__c.addDays(-7) < system.Today())
    					CompleteReturnDueDate = system.Today();
    				else
    					CompleteReturnDueDate = e.Tax_Prep_Meeting_Date__c.addDays(-7);
        		}
        		else
        		{
        			if(e.On_Extension__c)
        				CompleteReturnDueDate = date.newInstance(date.today().year(),4,18);
        			else
        				CompleteReturnDueDate = system.Today().addDays(10);
        		}
	
        		TasksToCreate.add(new Task(OwnerID = e.Tax_Preparer__c,
                                       Subject = 'Complete Tax Return',
                                       WhatID = e.Id,
                                       WhoId = e.Tax_Payer_1__c,
                                       ActivityDate = CompleteReturnDueDate,
                                       Status = 'Not Started',
                                       Description = 'Please prepare tax return.  Marking this complete will create a task for the Tax Advisor to review the return.',
                                       Priority = 'Normal',
                                       Hidden__c = 'Complete Tax Return'));
                                       
            	e.Complete_Tax_Return_Task_Created__c = TRUE;
            	
        	}
        	
        	if(!e.Review_Tax_Return_Task_Created__c && e.Complete_Tax_Return_Task_Completed__c && e.Complete_Tax_Return_Task_Completed__c != oldservice.Complete_Tax_Return_Task_Completed__c)
        	{
        		Date ReviewReturnDueDate;
	                	
            	if(e.Tax_Prep_Meeting_Date__c == NULL)
        		{
        			ReviewReturnDueDate = system.Today();
        		}
        		else
        		{
        			if(e.Tax_Prep_Meeting_Date__c.addDays(-2) < system.Today())
        			{
        				ReviewReturnDueDate = system.Today();
        			}
        			else
        			{
        				ReviewReturnDueDate = e.Tax_Prep_Meeting_Date__c.addDays(-2);
        			}
        		}
            	
            	TasksToCreate.add(new Task(OwnerID = e.Tax_Advisor__c,
                                	                   Subject = 'Review Tax Return',
                                    	               WhatID = e.Id,
                                           			   WhoId = e.Tax_Payer_1__c,
                                        	           ActivityDate = ReviewReturnDueDate,
                                            	       Description = 'Mark this task complete once it has been reviewed with and approved by the client.',
                                                	   Status = 'Not Started',
                                                       Priority = 'Normal',
                                                       Category__c = 'Client/Prospect Work',
                                                       Hidden__c = 'Review Tax Return'));
                                                       
                e.Review_Tax_Return_Task_Created__c = TRUE;
                e.In_Review__c = True;
                e.Status2__c = 'In Review';
    			e.Status_Date__c = system.Today();
        	}
        	
        	if(!e.Close_Return_Task_Created__c && e.Review_Tax_Return_Task_Completed__c)
        	{
        		TasksToCreate.add(new Task(OwnerID = sinkrote.Id,
                            	           Subject = 'Close Tax Return',
                                	       WhatID = e.Id,
                                       	   WhoId = e.Tax_Payer_1__c,
                                    	   ActivityDate = system.Today(),
                                           Description = 'Perform closing tasks for tax return.  Please ensure the appropriate items are checked on the Tax Service before marking this task complete.',
                                           Status = 'Not Started',
                                           Priority = 'Normal',
                                           Category__c = 'Client/Prospect Work',
                                           Hidden__c = 'Close Tax Return'));
                                                   
                e.Close_Return_Task_Created__c = True;
                e.Completed_Status__c = TRUE;
                e.Tax_Return_Reviewed_Date__c = system.Today();
                e.Status2__c = 'Completed';
				e.Status_Date__c = system.Today();
				e.Tax_Return_Complete_Date__c = system.Today();
        	}
        	
        	if(!e.Invoice_Client_Task_Created__c && e.Review_Tax_Return_Task_Completed__c && e.Client_Needs_to_be_Invoiced__c && !e.Invoice_Given_to_Client__c)
            {
				TasksToCreate.add(new Task(OwnerID = sdonatelli.Id,
        	        	                   Subject = 'Invoice Client',
            	        	               WhatID = e.Id,
                           				   WhoId = e.Tax_Payer_1__c,
                	        	           ActivityDate = system.Today(),
                    	        	       Description = '',
                        	        	   Status = 'Not Started',
                            	           Priority = 'Normal',
                                	       Category__c = 'Client/Prospect Work',
                                    	   Hidden__c = 'Invoice Client'));
                                    	   
                e.Invoice_Client_Task_Created__c = TRUE;
        	}
        	
        	if(e.Tax_Docs_Received__c && serviceTaskList != null && serviceTaskList.size()>0)
        	{
        		for(Integer t=0;t<serviceTaskList.size();t++)
        		{
        			if(serviceTaskList[t].WhatId == e.Business_Account__c)
        			{
	        			serviceTaskList[t].Status = 'Canceled';
	        			tasksToUpdate.add(serviceTaskList[t]);
        			}
        		}
        	}
    	}
    }

	try
    {
    	if(TasksToCreate.size()>0)
        	insert TasksToCreate;
        	
        if(tasksToUpdate.size()>0)
        	update tasksToUpdate;
        	
        if(mails.size()>0)
        	Messaging.sendEmail(mails);
    }
    
    catch (System.DmlException ex)
    {
        System.Debug (ex);
    }
}
Sean BarczynskiSean Barczynski
Correction:  when I check the Tax Docs Received box and save it, the Test 3 email is sent meaning it is not getting inside the IF statement, even though it seemingly should be.
Sean BarczynskiSean Barczynski
Another Correction:  The Test 3 email is being sent, indicating that it is not getting inside the IF statement, however  the Scan Tax Docs Task Created field gets checked, indicating that it is in fact getting inside the IF statement.