2

I am getting the below error while saving . Illegal assignment from SObject to Activity_Tracker__c at line 4 column 13.

Code .

public class ActivityTriggerHandler {
   public static void doBeforeUpdate(Map < Id, Activity_Tracker__c > newMap) {
      for (Activity_Tracker__c at: newMap.values()) {
         Activity_Tracker__c atprior = Trigger.oldMap.get(at.id);
         at.Start_Date_Prior_Value__c = at.Start_Date__c;
         at.End_Date_Prior_Value__c = at.End_Date__c;
         at.Status_Prior_value__c = at.Status__c;
         at.Updates_prior_value__c = at.Updates__c;
         at.Description_prior_value__c = at.Description__c;
         if (at.Start_Date__c < > atprior.Start_Date__c) {
            at.Start_Date_Prior_Value__c = atprior.Start_Date__c;
         }
         if (at.End_Date__c < > atprior.End_Date__c) {
            at.End_Date_Prior_Value__c = atprior.End_Date__c;
         }
         if (at.Status__c < > atprior.Status__c) {
            at.Status_Prior_value__c = atprior.Status__c;

         }
         if (at.Updates__c < > atprior.Updates__c) {
            at.Updates_prior_value__c = atprior.Updates__c;
         }
         if (at.Description__c < > atprior.Description__c) {
            at.Description_prior_value__c = atprior.Description__c;
         }
         if (at.Revised_End_Date__c < > atprior.Revised_End_Date__c) {
            at.Revised_End_Prior_Date__c = atprior.Revised_End_Date__c;
         }
         // Edit Access to Action Ownere and CoE Lead fields  
         if (at.Action_Owner__c != at.OwnerId && at.Action_Owner__c == UserInfo.getUserId()) {
            if (atprior.Name != at.Name || atprior.RAG_Status__c != at.RAG_Status__c || atprior.Category__c != at.Category__c || atprior.End_Date__c != at.End_Date__c || atprior.RAG_Status__c != at.RAG_Status__c || atprior.Revised_End_Date__c != at.Revised_End_Date__c || atprior.Instance__c != at.Instance__c || atprior.Start_Date__c != at.Start_Date__c || atprior.Start_Date__c != at.Start_Date__c) {
               at.addError(System.Label.Activity_Update_Desc);
            }
         }
         if (UserInfo.getName() < > 'TestUser1' && UserInfo.getName() < > 'TestUser2 ' && at.OwnerId != UserInfo.getUserId()) {
            system.debug('@@@@@' + UserInfo.getName());
            if (atprior.Start_Date__c != at.Start_Date__c || atprior.End_Date__c != at.End_Date__c) {
               at.addError(System.Label.Activity_Start_End_Date);
            }
         }

      }
   }
}

Regards

  • Also post trigger code snippet including trigger declaration and call to triggerhandler method. – Arpit_S Apr 7 '15 at 6:33
8

When using the trigger static variables inside helper classes there is no sObject context so the records inside the maps will be generic sObjects rather than concrete (as you would have inside the trigger itself).

You will need to cast your generic sObject record to a concrete one, so in your case the code should look like this:

Activity_Tracker__c atprior = (Activity_Tracker__c)Trigger.oldMap.get(at.id);

| improve this answer | |
0

Given that this code is called in a separate method and class, you are not passing in Trigger.oldMap(). I think this will resolve your issues.

You should really try to abstract away the concept of a trigger inside the trigger handler - if you do, you'll be able to use this class outside of the context of a trigger.

This is generally more applicable when passing in <List> objects. Sometimes I run a preprocessing loop inside the trigger itself to extract the items that need to be modified and pass THAT list to the handler.

Anyway, as @Bachovsky says, Trigger.oldMap is a static class, so yes, a simple cast will fix the problem - I just don't think that this is the best way to handle this, as it is highly coupled.

| improve this answer | |
  • Trigger is a static variable and it's accessible in this context. – Boris Bachovski Apr 7 '15 at 7:39
  • 1
    Fair enough. It'll still fix the issue though :) – Caspar Harmer Apr 7 '15 at 8:08

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.