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 freshersfdc fresher 

DML statements Query

Hi all,
in which triggers, we cant use DML Statments.could anyone please explain me


 
NagendraNagendra (Salesforce Developers) 
Hi,

May I suggest you please check with below links from the community forums with a similar discussion which might help. Please let us know if this helps.

Thanks,
Nagendra
GhanshyamChoudhariGhanshyamChoudhari

1. in BEFORE TRIGGER  you need not to use DML operations.
because it will trigger before the data save and automatically DML operation happens next so you need not specify explicitly.

2. in AFTER TRIGGER you need to specify DML operation because it will trigger after data save and you need to apply your business logic and save the data again so you need DML operation here.
sfdc freshersfdc fresher
Hi Ghanshyam,

I am getting below Error. When I have wrriten below code
trigger AccountAddressTrigger on Account (before insert,before update) {
    
    for(Account a:Trigger.new)
    {
        if(Trigger.isInsert)
        {
            if(a.Match_Billing_Address__c==True)
            {
             a.ShippingPostalCode=a.BillingPostalCode;   
            }
       update a; 
        }
        else
        {
            a.ShippingPostalCode=a.BillingPostalCode;
            insert a;
        }
    }
}

/////// iF I remove any of the above statements Update a or Insert a...its working fine ..No errors am getting, but when I have both statements  am getting below Error. Could you please explain me why it so?

Challenge Not yet complete... here's what's wrong: 
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountAddressTrigger: execution of BeforeInsert caused by: System.SObjectException: DML statement cannot operate on trigger.new or trigger.old Trigger.AccountAddressTrigger: line 11, column 1: []
GhanshyamChoudhariGhanshyamChoudhari
you are using before trigger. in this case, you need not to mention insert/update DML operation because this trigger will fire before the data save and once it will fire then next operation will perform by salesforce which is data save(DML operation)