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
SFDC Coder 8SFDC Coder 8 

Bug in order trigger while updating

Hi All,
I have written a trigger on Order.
I am giving errors in two scenarios. I have one custom field Blocked__c in account. If I have blocked__c = Y, I dont want to create order.
This condition is not working properly.
This is my code. Please tell me where is bug
trigger orderTrigger on Order (before insert) {

List<Order> ordLs = [Select Id, F1__c,Status,AccountId,Account.Blocked__c from Order where createdDate=today ];  

for (Order ordNew : Trigger.new)
  {      
    for(Order oa: ordLs)
        {         
               if(oa.AccountId == ordNew.AccountId && oa.F1__c == ordNew.F1__c && oa.status == 'confirmed')         
                  {              
                    ordNew.AccountId.addError('Some messge');        
                  } 
               if(oa.Account.Blocked__c=='Y') {
                   ordNew.AccountId.addError('Blocked'); 
               } 
         }
        
  }

}

Please help me.
Line Number 13 is not working properly. Please suggest me changes.
Thanks in Advance
sandeep reddy 37sandeep reddy 37
i think both if conditions are working so there is no else  i think you got two error messages or write in else block other wise write this condition on first if control statement check it
 
SFDC Coder 8SFDC Coder 8
Hi Piyush,
When I have Blocked__c != Y in Account, eventhough order is not creating
SFDC Coder 8SFDC Coder 8
No Piyush,
Even when I am creating new order, First it should check 'Blocked__c', then order should get create.
This is not working all the time
sfdcMonkey.comsfdcMonkey.com
hi
use below code
trigger orderTrigger on Order (before insert) {

List<Order> ordLs = [Select Id, F1__c,Status,AccountId,Account.Blocked__c from Order where createdDate=today ];  

for (Order ordNew : Trigger.new){      
    for(Order oa: ordLs)
        {         
                 
               if(oa.AccountId == ordNew.AccountId && oa.F1__c == ordNew.F1__c && oa.status == 'Draft'){              
                    ordNew.AccountId.addError('Some messge');        
                  }
                   
                 if(ordNew.Account.Blocked__c == 'Y') {   // here is ordNew.Account.Blocked__c
                   ordNew.AccountId.addError('Blocked');
               }
              
         }
        
  }

}
let me inform if it helps you
thanks

 
SFDC Coder 8SFDC Coder 8

Hi Piyush,
Its not working

If Blocked == 'Y' also order is creating
 

anuragsinghnsanuragsinghns

SFDC Coder 8
try ordNew.account.addError('Error Message'); //not accountID

Or throw an error on the record itself
try ordNew.addError('Error Message');

Please mark as best answer if this is the solution

SFDC Coder 8SFDC Coder 8

Hi Anurags

Its not working. I dont know what is error

even if Blocked == 'Y' also order is creating