0

I can't figure out this error. It only happens on the 3 triggers I have created on Opportunities that send emails. It always points to the "Messaging.sendEmail(emails);" line. These triggers worked before, and then I installed the NPSP 3.0 and this error started appearing. NPSP says it has nothing to do with them, which is fine, I just know that's the only thing we changed. We need to keep NPSP 3.0 because we were having different errors with 2.0.

Here is the full error

Upsert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ClientCare_TeamTrainingFundingReceived: execution of AfterInsert

caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Task: bad field names on insert/update call: ActivityOriginType: [ActivityOriginType]

Trigger.ClientCare_TeamTrainingFundingReceived: line 148, column 1: []

One of the triggers

trigger ClientCare_TeamTrainingFundingReceived on Opportunity (after insert, after update) {

List<ID> RequireContactId = new  List<ID>() ;  
List<ID> RequireCampaignId = new  List<ID>() ;

for(Opportunity  Opp : trigger.new)
{
    if(Opp.Contact_ID__c != null && Opp.CampaignId != NULL){ 
        RequireContactId.add(Opp.Contact_ID__c);
        RequireCampaignId.add(Opp.Campaign_ID__c);
    }   
}

Map<ID,Contact> ContactData = new Map<Id,Contact>([Select Owner.Email,OwnerId,Email, CC_Emails_To__c, Dog_Type__c, Almost_Funded_Task__c, All_Funds_Received_Email__c, Last_Date_Contribution_Received__c, Percentage_Received__c, Source_Campaign__c, Remaining_Balance__c, Total_Funds_Received__c, RecordTypeId, Program__c, Stage__c, Status__c, Name, ID from Contact where id in : RequireContactId limit 1000 ]);    
Map<ID,Contact> ContactForUpdate = new Map<Id,Contact>();

Map<ID,Campaign> CampaignData = new Map<Id,Campaign>([Select AmountWonOpportunities, Name, ID from Campaign where id in : RequireCampaignId limit 1000 ]);    
Map<ID,Campaign> CampaignForUpdate = new Map<Id,Campaign>();

List<Task> newTasks = new List<Task>();
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();

for(Opportunity  theOpp : trigger.new)
{
    if(theOpp.Contact_ID__c != null && ContactData.containskey(theOpp.Contact_ID__c)&& theOpp.Campaign_ID__c != null && CampaignData.containskey(theOpp.Campaign_ID__c))
    {  
       Contact theContact =  ContactData.get(theOpp.Contact_ID__c) ;
       Campaign theCampaign =  CampaignData.get(theOpp.Campaign_ID__c) ;  
       if (theCampaign.Name.contains('Team Training_') && (theContact.Source_Campaign__c == NULL || theContact.Source_Campaign__c == theOpp.Campaign_ID__c) && theContact.Stage__c == 'Candidate' && theContact.Status__c == 'Active' && theContact.RecordTypeId == '012d0000000t7JQ')
       {
           theContact.Source_Campaign__c = theCampaign.Id;

           if (theContact.Percentage_Received__c >= 80 && theContact.Percentage_Received__c < 100 && theContact.All_Funds_Received_Email__c == NULL && theContact.Almost_Funded_Task__c == FALSE)
           {      
               theContact.Almost_Funded_Task__c = TRUE;           
                Task fundingTask = new Task();
                fundingTask.ActivityDate = date.today();
                fundingTask.WhoId = theContact.id;
                fundingTask.OwnerId = '005d0000001knHT';
                fundingTask.Priority = 'Normal';
                fundingTask.Status = 'Not Started';
                fundingTask.Subject = 'Please Read: ' + theContact.Percentage_Received__c + '% Funding Received for ' + theContact.Name;
                fundingTask.Description = theContact.Percentage_Received__c + '% of funding has been received for ' + theContact.Name + '. '
                + 'They will be ready for Team Training soon. You will be notified once 100% of funds are received.\n'
                + 'Program: ' + theContact.Program__c + '\n'
                + 'Dog Type: ' + theContact.Dog_Type__c;           
                newTasks.add(fundingTask);

                Task fundingTask2 = new Task();
                fundingTask2.ActivityDate = date.today();
                fundingTask2.WhoId = theContact.id;
                fundingTask2.OwnerId =theContact.OwnerId;
                fundingTask2.Priority = 'Normal';
                fundingTask2.Status = 'Not Started';
                fundingTask2.Subject = 'Please Read: ' + theContact.Percentage_Received__c + '% Funding Received for ' + theContact.Name;
                fundingTask2.Description = theContact.Percentage_Received__c + '% of funding has been received for ' + theContact.Name + '.'
                + ' They will be ready for Team Training soon. You will be notified once 100% of funds are received.\n'
                + 'Program: ' + theContact.Program__c + '\n'
                + 'Dog Type: ' + theContact.Dog_Type__c;                      
                newTasks.add(fundingTask2);
           }else if (theContact.Percentage_Received__c >= 100 && theContact.All_Funds_Received_Email__c == NULL)
           {
                theContact.All_Funds_Received_Email__c=date.today();

                if(theContact.Email != NULL){
                    User uEmail = [select Email from User where Id = :theContact.OwnerID limit 1]; 
                    OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = :uEmail.Email limit 1];
                    Messaging.SingleEmailMessage sendEmail = new Messaging.SingleEmailMessage();
                    //OpenDoors & CanineMagic - All Funds Received Acknowledgement                                   
                    sendEmail.setTemplateID('00Xd0000000k3LW');                    
                    sendEmail.setTargetObjectId(theOpp.Contact_ID__c);
                    if ( owea.size() > 0 ) {
                        sendEmail.setOrgWideEmailAddressId(owea.get(0).Id);
                    }else{
                        sendEmail.setOrgWideEmailAddressId('0D2d0000000TOS2');                  
                    }
                    if(theContact.CC_Emails_To__c != NULL){
                        String[] CCEmails = theContact.CC_Emails_To__c.split(';');
                        sendEmail.setCCAddresses(CCEmails);
                    }
                    emails.add(sendEmail);
                } 


                Task fundingTask = new Task();
                fundingTask.ActivityDate = date.today();
                fundingTask.WhoId = theContact.id;
                fundingTask.OwnerId = '005d0000001knHT';
                fundingTask.Priority = 'High';
                fundingTask.Status = 'Not Started';
                fundingTask.Subject = 'All Funding Received for ' + theContact.Name;
                fundingTask.Description = 'All funding has been received for ' + theContact.Name + '.'
                + ' They are ready to be scheduled for Team Training. Please fill in the Team Training Estimates or Team Training Start when you have a date for them.\n'
                + 'Program: ' + theContact.Program__c + '\n'
                + 'Dog Type: ' + theContact.Dog_Type__c;                     
                newTasks.add(fundingTask);

                Task fundingTask2 = new Task();
                fundingTask2.ActivityDate = date.today();
                fundingTask2.WhoId = theContact.id;
                fundingTask2.OwnerId =theContact.OwnerId;
                fundingTask2.Priority = 'High';
                fundingTask2.Status = 'Not Started';
                fundingTask2.Subject = 'All Funding Received for ' + theContact.Name;
                fundingTask2.Description = 'All funding has been received for ' + theContact.Name + '.'
                + ' They are ready to be scheduled for Team Training. Please fill in the Team Training Estimates or Team Training Start when you have a date for them.\n'
                + 'Program: ' + theContact.Program__c + '\n'
                + 'Dog Type: ' + theContact.Dog_Type__c;                      
                newTasks.add(fundingTask2);
           }
           ContactForUpdate.put(theContact.id,theContact);
       }       
    }
}  


   if( ContactForUpdate != null && ContactForUpdate.size() > 0)
   {
       update ContactForUpdate.values();

   }


   if( newTasks!= null && newTasks.size() > 0)
   {
       insert newTasks ;
   }

   if (emails != NULL && emails.size() > 0)
   {
       Messaging.sendEmail(emails);
   }    

}

Edit: If I take out the insert newTasks(); line, the code works. Still can't figure out what the problem is though as I have this same task code on other objects without any problem.

2

It's a Known Issue titled:

Task insertion from EmailTaskCreator can result in INVALID_FIELD_FOR_INSERT_UPDATE, Task: bad field names on insert/update call: ActivityOriginType

Summary
The Messaging.sendEmail() apex method call in an apex trigger could cause an API Exception, INVALID_FIELD_FOR_INSERT_UPDATE, Task: bad field names on insert/update call: ActivityOriginType: [ActivityOriginType], at a Task insertion.

Repro
This issue is not reproducible on demand

Workaround
None. Investigation is still in progress.

  • Hey Leon, link-only answers are frowned upon in this forum. I have updated your answer to show you an example of what you can do instead. In the future, please try to include some content from your linked resource and some explanation if needed. – Adrian Larson Feb 25 '16 at 20:21
0

Do you have any process builder actions that are on the task object? That was causing mine to fail - even though the inserted task did not meet the criteria for action...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.