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
developer_forcedeveloper_force 

Field expression not allowed for generic SObject

Hi,

 

Need help with the error below

 

Here is the trigger handler class where iam implementing the before insert trigger

 

public void OnBeforeInsert(Sales_Target__c[] newRecords){


newRecords = trigger.new;
for(Sales_Target__c newSalesTarget : newRecords)
{
if ((newSalesTarget.Officer_Code__c != null) &&
(newSalesTarget.Officer_Code__c= System.Trigger.oldMap.get(newSalesTarget.Id).Officer_Code__c))

}

 

and the error i got is "Field expression not allowed for generic SObject '' at the line highlighted in red. Not sure what that mean.

 

Thanks

Andy BoettcherAndy Boettcher

Your code appears to be sound - I know that the one thing that always bites me when dealing with INSERT triggers and Ids - that when you specify "before insert" - your records don't have an Id yet.  Try flipping to "after insert" and see if that resolves the issue?

 

-Andy

developer_forcedeveloper_force

Yes. I changed the code instead like this 

 

public void OnBeforeInsert(Sales_Target__c[] newRecords){

  
   newRecords = trigger.new;
      for(Sales_Target__c newSalesTarget : newRecords)
   {
   
Sales_Target__c[] salesTarget = [Select Id, Officer_Code__c , Target_Period_End__c , Target_Period_Start__c from Sales_Target__c 
where Officer_Code__c =:newSalesTarget.Officer_Code__c];

 

if(salesTarget.size()>0){

//some error message

}

}

 

Pls let me know if that looks good!!!!

 

Thanks

Prad47Prad47

I was facing same issue while comparing Trigger.newMap.get(Id).Status__C with objCandidate.Status__C 

But I tried in following way and it works 

 

 sObject candidate = Trigger.newMap.get(candidate.Id);

 candidate .get('Ass__Status__c') ==objCandidate.Status__c 

 

 

Prad47Prad47

@Andy 

We can get the  id in case of "before insert " with Trigger.new 

In Trigger.new and Trigger.old id's are same but records detail are different.