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
Ajay0105Ajay0105 

Need Help ..

Hi All,

 

My Requirement is 

 

 Account Is Record Type="Master Account Layout' and parent account is Null then HQ field set to True

 

I have Created a trigger for achieving above reqt

 

1
2
3
4
5
6
7
8
9
10
11

trigger accountTypeChange on Account (before Insert) {

for(Account acc :Trigger.new){
  If(acc.Record_Type__c == 'Master Account Layout' &&  (acc.Parent == null || acc.Parent.Record_Type__c == 'Virtual Acount Partner'))
{
   // acc.HQ__c =true;
}  
 insert acc; 
  
}
}

 

But as I want to insert the record on Account Object I am getting the error msg 

 

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

 

Plz Help me in rectifying the Issue..

 

 

Best Answer chosen by Admin (Salesforce Developers) 
PremanathPremanath

Hi you can try like this ,it may helpful to you

Don't write Dml operation in for loop, it will Exception when cross governor limitations.

 

trigger accountTypeChange on Account (before Insert) {
List<Account> varacc=new List<Account>();
for(Account acc :Trigger.new){
  If(acc.Record_Type__c == 'Master Account Layout' &&  (acc.Parent == null || acc.Parent.Record_Type__c == 'Virtual Acount Partner'))
   {
       acc.HQ__c =true;

       varacc.add(acc);
    } 
  
}

update varacc;
}

 

 

 

If it is helpful plz make it as solution for others it may benfit

Prem

All Answers

PremanathPremanath

Hi you can try like this ,it may helpful to you

Don't write Dml operation in for loop, it will Exception when cross governor limitations.

 

trigger accountTypeChange on Account (before Insert) {
List<Account> varacc=new List<Account>();
for(Account acc :Trigger.new){
  If(acc.Record_Type__c == 'Master Account Layout' &&  (acc.Parent == null || acc.Parent.Record_Type__c == 'Virtual Acount Partner'))
   {
       acc.HQ__c =true;

       varacc.add(acc);
    } 
  
}

update varacc;
}

 

 

 

If it is helpful plz make it as solution for others it may benfit

Prem

This was selected as the best answer
SFFSFF

I think this still has some issues - try this:

 

trigger accountTypeChange on Account (before insert)
{
  for(Account acc : trigger.new)
  {
    if (acc.Record_Type__c == 'Master Account Layout' && 
        (acc.Parent == null || 
         acc.Parent.Record_Type__c == 'Virtual Acount Partner'))
    {
       acc.HQ__c = true;
    } 
  }
}

You never need to - or want to - perform DML on the record you are processing in a before trigger.

 

Hope this helps,

Ajay0105Ajay0105

Hi John,

Good Morning,

 

Thanks for you reply..

 

The problem is Resolved

 

Actually The basic thing that I forget and as you say 

 

"You never need to - or want to - perform DML on the record you are processing in a before triggers you say "