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
NikiG22NikiG22 

Task Trigger

Hello - I have a trigger that inserts a task in my opportunity when the opp is cloosed won. Now i need to eclude users with a role = inside sales. i have it in there but it is not working?

 

Also - i need help bringing my code up to 100%

 

any help would be awesome. Thank you

 

public class TasksForOpportunitiesClosedToContracts 
{
    public static void createTasksForOpportunityClosedToContract(Opportunity[] opportunities)
    {
        RecordType RecType = new RecordType();  
        RecType = [SELECT ID FROM RecordType WHERE Name = 'SAM Task'];
        

        for (Integer i = 0; i < opportunities.size(); i++) 
        {
            IF (opportunities[i].StageName == 'Closed Won' &&
                opportunities[i].LeadSource != 'Purchased from Site' &&
                opportunities[i].Message_Sent__c == false &&
                opportunities[i].Owner.UserRole.Name !='Inside Sales Representative'&&
                opportunities[i].Count_of_Pending_RS__c==0) 
            {
                Account acct = new Account();
                Task newContractTask = new Task();                
                
                acct = [SELECT client_id__c FROM Account WHERE ID = :opportunities[i].AccountId];
                                
                newContractTask.client_id__c = acct.client_id__c;
                newContractTask.OwnerID = opportunities[i].Account_Manager__c;
                newContractTask.Subject = 'New Closed Won Opportunity';
                newContractTask.Description = opportunities[i].Description;
                newContractTask.WhatID = opportunities[i].ID;
                newContractTask.Status = 'Not Started';
                newContractTask.RecordTypeID = RecType.ID;
                newContractTask.Priority = 'High';
                newContractTask.Type = 'New Closed Won Opportunity';
                newContractTask.ActivityDate = system.today();
                newContractTask.reminderdatetime = system.now();
                newContractTask.isreminderset = false;
                newContractTask.Short_Description__c = 'Create Contract/ Activate Job';
    
                insert newContractTask;

                
            } 
            
        } 
      } 
    
    static testMethod void test_trTasksForOpportunitiesToContracts()
    {
        Opportunity candidateOpportunity = [Select opp.StageName, opp.LeadSource From Opportunity opp 
                                            where opp.StageName != 'Closed Won' 
                                            and opp.StageName != 'Closed Lost' 
                                            and opp.Billing_is_different_than_Account__c = false 
                                            LIMIT 1];
        candidateOpportunity.StageName = 'Closed Won';
        candidateOpportunity.Billing_Email__c = 'test@healthecareers.com';
        candidateOpportunity.Billing_Contact__c = 'Test Contact' ;
        candidateOpportunity.Invoice_Delivery_Method__c = 'Email';
        candidateOpportunity.Payment_Method__c = 'Invoice';
        candidateOpportunity.Billing_Contact__c = 'Test Contact' ;
        candidateOpportunity.Type = 'Renewal';
        candidateOpportunity.New_Dollar_Amount__c =0;
        candidateOpportunity.Win_Loss_Notes__c = 'These are test notes.';
        update candidateOpportunity;
        
        Task newContractTask = [select Type from Task where WhatId = :candidateOpportunity.Id and Subject = 'New Closed Won Opportunity' LIMIT 1];
        System.assertEquals('New Closed Won Opportunity', newContractTask.Type);
        
    }
    
    
}

 

SimonJaiSimonJai

That's not a trigger because you haven't defined when it should "trigger".

 

trigger TasksForOpportunitiesClosedToContracts on Task (after insert) {

     // Code goes here

}

 

NikiG22NikiG22

Thats my fault it is a Class and the insert of the task works just fine. I just need to exclude the insert if an opportuntiy owner has a sepecific role.

SimonJaiSimonJai

I think you should add a debug message inside your if statement, turn on the debugging feature and check to see what is wrong.

 

System.debug('*****Opportunity User Role: ' + opportunities[i].Owner.UserRole.Name);

Alternatively that's what apex testing is for, to ensure your functions work as they should. So create dummy opportunities inside your test method, call the createTasksForOpportunityClosedToContract() function and ensure the values are correct.