• sfkevin
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

Hello,

I am new to dev work and learning Apex code.  I wrote a trigger that prevents us from needing to create several workflow field updates.  It evaluates a few variables on a record and returns the proper Account Type.  In the IDE the code passed the syntax check but when I went to try it out on an Account Record in the sandbox my trigger threw an exception as follows:

 

Error: Invalid Data.
Review all error messages below to correct your data. Apex trigger AccountTypeUpdate caused an unexpected exception, contact your administrator: AccountTypeUpdate: execution of AfterUpdate caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.AccountTypeUpdate: line 16, column 1

 

What can I do to fix this error?  Here is the trigger code:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

trigger AccountTypeUpdate on Account (after update) {
    List<Account> acctTypeUpdate = new List<Account>();
        for(Account a : Trigger.new){
            //Series of conditional statements that check for the Site ID Status and returns the proper value for the Type field
            if(a.SiteIDStatus__c =='Live' & a.ParentId == null){
            a.Type = 'Client - Parent';
                if(a.SiteIDStatus__c =='Live' & a.ParentId != null){
                a.Type = 'Client - LOB';
                    if(a.SiteIDStatus__c !='Expired'){
                    a.Type = 'Client - Cancelled';
                    } else {
                        a.Type = 'Prospect';
                    }
                }   
            }
        update a;
        }
}

Hello,

I am new to dev work and learning Apex code.  I wrote a trigger that prevents us from needing to create several workflow field updates.  It evaluates a few variables on a record and returns the proper Account Type.  In the IDE the code passed the syntax check but when I went to try it out on an Account Record in the sandbox my trigger threw an exception as follows:

 

Error: Invalid Data.
Review all error messages below to correct your data. Apex trigger AccountTypeUpdate caused an unexpected exception, contact your administrator: AccountTypeUpdate: execution of AfterUpdate caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.AccountTypeUpdate: line 16, column 1

 

What can I do to fix this error?  Here is the trigger code:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

trigger AccountTypeUpdate on Account (after update) {
    List<Account> acctTypeUpdate = new List<Account>();
        for(Account a : Trigger.new){
            //Series of conditional statements that check for the Site ID Status and returns the proper value for the Type field
            if(a.SiteIDStatus__c =='Live' & a.ParentId == null){
            a.Type = 'Client - Parent';
                if(a.SiteIDStatus__c =='Live' & a.ParentId != null){
                a.Type = 'Client - LOB';
                    if(a.SiteIDStatus__c !='Expired'){
                    a.Type = 'Client - Cancelled';
                    } else {
                        a.Type = 'Prospect';
                    }
                }   
            }
        update a;
        }
}