You need to sign in to do that
Don't have an account?
nikita dhamal
trigger on order object when checkbox on account is checked
I want to show error and order will not be allowed to be created if there is a hard block on the account object is checked.
no error but hte trigger is not working.
my trigger is:
trigger ValidateOrder on Order (before insert) {
for(Order occ : Trigger.new){
if(occ.id!=null)
{
occ = [Select Accountid from Order where id =: occ.id];
system.debug('***********************'+occ);
Account acc = [Select id,Hard_Block__c from Account where id=:occ.Accountid];
system.debug('***********************'+acc);
if(acc.Hard_Block__c == True)
occ.addError(' This Order Cannot be created' );
}
}
}
no error but hte trigger is not working.
my trigger is:
trigger ValidateOrder on Order (before insert) {
for(Order occ : Trigger.new){
if(occ.id!=null)
{
occ = [Select Accountid from Order where id =: occ.id];
system.debug('***********************'+occ);
Account acc = [Select id,Hard_Block__c from Account where id=:occ.Accountid];
system.debug('***********************'+acc);
if(acc.Hard_Block__c == True)
occ.addError(' This Order Cannot be created' );
}
}
}
Please try below code :
All Answers
Please try below code :
It's not a best practice to write SOQL in for loop as this will hit the governer limit of 100 SOQLs in one transaction if more than 100 records will be imported in Order object and will lead to failures. Please find the below mentioned code:
First: You are using Before Insert and checking for Id of Order, as the Id of order will not be available because it has not been created yet. Hence your code will never get executed.
Second: What is the need of "occ = [Select Accountid from Order where id =: occ.id];" as you have the date already in occ.
Third: Never write SOQL in for loop. Try Sets for fetching the Account Ids in your Loop and the query on the set to get the Account Detail.
Hope this helps.