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
ADNADN 

Trigger

I create a trigger  which prevent user form save account if it associated with a recordtype account:

Is the following trigger correct? Do I need to put the query outside the account for loop.

 

Trigger UmbrellaAccountTrigger on Account (before insert,before update) {
private Account[] Acc =Trigger.new;
integer SOQL;
List <contact> Con= New List <Contact>();
For(Account a:Acc){
a.Updated__c=[select count() from contact where accountId=:a.ID];
If((a.Updated__c!= 0)&&(a.RecordTypeC__c==1 )){

a.addError('You can not save Umbrella Record associated with a contact');
}
}
}

 

Note: a.Updated__c is a custom field on account and it display the number of relcated contact



Best Answer chosen by Admin (Salesforce Developers) 
SteveDevSteveDev

Yes, you should always put queries and DML statements outside of loops, that way you won't breach the governor limits no matter how many records are updated/inserted.

 

For this trigger, I would go for something like this:

 

Trigger UmbrellaAccountTrigger on Account (before insert,before update) {

Set<Id> accountIds = new Set<String>();
for (Account a : trigger.new) {
     accountIds.add(a.Id);
}

for (Account a : [Select Id, Updated__c (Select Id from Contact) where Id in :accountIds) {
     Contact[] cons = a.Contacts;
     if (cons.size()!=0 && a.RecordTypeC__c==1) {
         a.addError('You can not save Umbrella Record associated with a contact');
     } else {
         a.Updated__c = cons.size();
     }
}


}

 

All Answers

SteveDevSteveDev

Yes, you should always put queries and DML statements outside of loops, that way you won't breach the governor limits no matter how many records are updated/inserted.

 

For this trigger, I would go for something like this:

 

Trigger UmbrellaAccountTrigger on Account (before insert,before update) {

Set<Id> accountIds = new Set<String>();
for (Account a : trigger.new) {
     accountIds.add(a.Id);
}

for (Account a : [Select Id, Updated__c (Select Id from Contact) where Id in :accountIds) {
     Contact[] cons = a.Contacts;
     if (cons.size()!=0 && a.RecordTypeC__c==1) {
         a.addError('You can not save Umbrella Record associated with a contact');
     } else {
         a.Updated__c = cons.size();
     }
}


}

 

This was selected as the best answer
ADNADN

Thanks, that's help.

But i still get an exception error because of workflow of (RecordTypeC__c ) custom field on Account

 

on My account organization I have 3 Record type: Umbrella/Office/NoCompany

if want write a statement: what is the data Type of record type of account ? Recordtype ?

instead of : if (cons.Size()!=o && a.ReportTypeC==1) ;

I Want write  the following statment: if(cons.Size() !=0  &&  a.ReportType='Umbrella') ? still i get apex compiler error because of data type.

 

Many thanks for your help