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
HilineHiline 

Easy Trigger to update Child record from a User field

Hello,

I'm trying to create trigger on the Opportunity object to update the child Opportunity Product records with the Opportunity Owner's department number, which is one of the fields in the User object.  I'm getting the following compile error:

 

Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<OpportunityLineItem> at line 24 column 14

 

Here's the code I have.

trigger OpportunityUpdateDept on Opportunity (after update) {
    
    for( Opportunity parent: Trigger.new)
    {
    
        List<OpportunityLineItem> children = [ SELECT Department_Number__c from 
                                               OpportunityLineItem where Opportunity.id = :parent.Id];

        list <User> currentOwner = [Select User.Department_Number__c
                                            from User
                                            where User.id = :parent.ownerid];                                                       
        
        List<OpportunityLineItem> childrenToUpdate = new List<OpportunityLineItem>();
        
        for(OpportunityLineItem thischild: children )
        {
           if( thischild.Department_Number__c !=  currentOwner[0].Department_Number__c)
           {
               thischild.Department_Number__c =  currentOwner[0].Department_Number__c;
               childrenToUpdate.add(thischild);
           }
        }
        
        if( !childrenToUpdate.isEmpty)
        {
           update childrenToUpdate;
        }
    
    }                       
}

 

I'm not exactly sure what is causing the error.  If I remove the if statement that checks if childrentToUpdate.isEmpty, then it works.  But I want to be able to skip the update if its not necessary.  Any assistance with this issue is very much appreciated.

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
HariDineshHariDinesh

Hi,

 

Here you declared “childrenToUpdate” as list.

check where list contain dat or not by using sutable list methods.

 

So replace your code with below code to resolve this error

if( !childrenToUpdate.isEmpty())

OR

 

IF(childrenToUpdate.size() >0)

 OR

 

IF(childrenToUpdate != null && childrenToUpdate !=’’)

 

It will solve your problem and will skip the update if its not necessary.

 

Mark it as Resolved if it helps you or resolves your problem.

All Answers

Dev@Force.ax647Dev@Force.ax647

just put () after isEmpty because isEmpty is a function not property.

 

        if( !childrenToUpdate.isEmpty())
        {
           update childrenToUpdate;
        }
HariDineshHariDinesh

Hi,

 

Here you declared “childrenToUpdate” as list.

check where list contain dat or not by using sutable list methods.

 

So replace your code with below code to resolve this error

if( !childrenToUpdate.isEmpty())

OR

 

IF(childrenToUpdate.size() >0)

 OR

 

IF(childrenToUpdate != null && childrenToUpdate !=’’)

 

It will solve your problem and will skip the update if its not necessary.

 

Mark it as Resolved if it helps you or resolves your problem.

This was selected as the best answer
HilineHiline

Thank you to both Dev@Force and HariDinesh.  Adding the brackets to isEmpty() did the trick.