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
Edward EastEdward East 

Issue deploying a task trigger into production

Hi all, 

I'm trying to create a trigger that will automatically change an opportunity stage once a certain task is completeted. The code is here:

trigger changeOpportunityStage on Task (before insert, after update) {

    Set<Id> oppIds = new Set<Id>();  
    for(Task t:trigger.new){
        String wId = t.WhatId;
              if(wId!=null && wId.startsWith('006') && t.Status == 'Completed' && t.Subject.Contains('Account - Initial Report')){
                oppIds.add(t.WhatId);
  }
    }//for 
   List<Opportunity> opps = [select Id, StageName from opportunity where id in: oppIds AND ( RecordType.name = 'Influencer' or RecordType.name = 'Creative')];    
        for(Opportunity opp : opps){
          if(opp.StageName == 'Closed Won')
          opp.StageName = 'Report Sent';
}
    try{
        update opps;
    }catch(DMLException e){
        system.debug('Opportunities were not all properly updated.  Error: '+e);
    }   
}//trigger

It works perfectly in sandbox and I've been trying to deploy it to production. However, my apex text class has been flagging up this error and I don't know how to resolve this issue:

testChangeOpportunityStage.testChangeOpportunityStatusTrigger(), Details: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Name ID: id value of incorrect type: 00624000008UJnkAAG: [WhoId] Class.testChangeOpportunityStage.testChangeOpportunityStatusTrigger: line 14, column 1

Here is my test class:

@isTest
private class testChangeOpportunityStage {
    static testMethod void testChangeOpportunityStatusTrigger(){
      String desiredNewOpportunityStage='Report Sent';
      
      Account a = new Account(Name ='testing acc');
insert a;
        
        Opportunity opp= new Opportunity (Name='triggertest', Account = a, AccountId=a.Id, CloseDate = System.today(), StageName='Closed Won');
        insert opp;
        Task t=new Task (whoId=opp.Id, subject='Account - Initial Report', Status='Completed');

        Test.StartTest();
            insert t;
        Test.StopTest();
        
        Opportunity opp2=[Select Id, StageName FROM Opportunity WHERE Id=:opp.Id];
        system.assertEquals(desiredNewOpportunityStage,opp2.StageName);
    }
}

Thanks a lot!

 
Best Answer chosen by Edward East
srlawr uksrlawr uk
You need to change this line
 
Task t=new Task (whoId=opp.Id, subject='Account - Initial Report', Status='Completed');

to be this
 
Task t=new Task (whatId=opp.Id, subject='Account - Initial Report', Status='Completed');

whoId and whatId are two fields on "Task" - and can only be completed exclusivly, "whoId" is for contacts and "whatId" is for other objects. You got this right in your code, but wrong in your test!

Have a quick look over the docs here for the two fields "whatId" and "whoId"

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_task.htm

All Answers

srlawr uksrlawr uk
You need to change this line
 
Task t=new Task (whoId=opp.Id, subject='Account - Initial Report', Status='Completed');

to be this
 
Task t=new Task (whatId=opp.Id, subject='Account - Initial Report', Status='Completed');

whoId and whatId are two fields on "Task" - and can only be completed exclusivly, "whoId" is for contacts and "whatId" is for other objects. You got this right in your code, but wrong in your test!

Have a quick look over the docs here for the two fields "whatId" and "whoId"

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_task.htm
This was selected as the best answer
Edward EastEdward East
Thanks so much!