You need to sign in to do that
Don't have an account?
Bob
Trigger to add before insert before update getting an error
I have a trigger that I am trying to add a before update to a before insert trigger but i keep getting the following error.
What can I change on this code to make it allow insert and updates? I need it to set to 'true' for any new Quotes and 'false' for any Quotes that get updated?
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger MarkPrimaryQuote caused an unexpected exception, contact your administrator: MarkPrimaryQuote: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0Q0n0000000DRrLCAW; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0Q0n0000000DRrL) is currently in trigger MarkPrimaryQuote, therefore it cannot recursively update itself: []: Trigger.MarkPrimaryQuote: line 19, column 1
What can I change on this code to make it allow insert and updates? I need it to set to 'true' for any new Quotes and 'false' for any Quotes that get updated?
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger MarkPrimaryQuote caused an unexpected exception, contact your administrator: MarkPrimaryQuote: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0Q0n0000000DRrLCAW; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 0Q0n0000000DRrL) is currently in trigger MarkPrimaryQuote, therefore it cannot recursively update itself: []: Trigger.MarkPrimaryQuote: line 19, column 1
trigger MarkPrimaryQuote on Quote (before insert, before update) { List<DateTime> oracleQuoteList = new List<DateTime>(); for(Quote qRec : Trigger.new) { if(qRec == null ) qRec.Primary_Quote__c = true; oracleQuoteList.add(qRec.Created_Date__c ); } List<Quote> quoteListToUpdate = new List<Quote>(); for(Quote qRec : [SELECT id,Primary_Quote__c,Created_Date__c from Quote WHERE Created_Date__c= TODAY AND HOUR_IN_DAY(LastModifiedDate) > 1]) { qRec.Primary_Quote__c =false; quoteListToUpdate.add(qRec); } if(quoteListToUpdate != null && quoteListToUpdate .size() > 0) { update quoteListToUpdate; } }
You are getting this error because you are updating the same record in your before trigger and due to this the trigger is called again and again.
As per your requirement i have updated the trigger code as below :
Please use and modify the above trigger accorddingly and let me know if you need more help or information on this.
Thanks,
Abhishek Bansal
When you use before insert or before update events, you no need to explicitly update list.
Thanks,
Dhanya
Stage__c , Oracle_Quote__c,Pricebook__c,Name, Net_Revenue__c,Amount__c,Order_Type__c. Any help would be greatly appreciated.
I have updated the trigger code, Please find below the updated code : I have added the updation check for some fields and you can add for more fields as per your requirement.
Please let me know if yopu need more help on this.
Thanks,
Abhishek Bansal
If this answer helps you out, please mark it as Best Answer.
The below section will check the recently inserted records :
Please let me know if you need more information on this.
Thanks,
Abhishek Bansal
for(Quote qRec : [SELECT id,Primary_Quote__c,Created_Date__c from Quote WHERE Created_Date__c= TODAY AND HOUR_IN_DAY(Created_Date__c) > 1]) {
qRec.Primary_Quote__c =false;
}
If all that you need to do is to mark the field Primary_Quote__c = true for each newly inserted record.
And mark it false for each recently updated record. Then the following trigger would serve the purpose for you.
Remember: Trigger.new is a bulkified List. meaning that it contains all recently inserted and updated records from multiple instances.
You dont need to perform SOQL query to fetch recently updated Quotes. You already have it in this list.
Plus, You don't need to use Update/ Insert command on Quote records.
The purpose of the trigger is to perform some operations either before or after DML operation. After the logic ends, all the records are Inserted/Updated/Deleted by the system.
Updatation/Deletion/Insertion is already in progress or completed when a trigger is fired.
Mark this as Best Answer, if it solves your problem.
Does this make sense?
Yes your requirement makes sense and based on this i have creted a trigger code. Please find the triggercode below : Please take care of the API name of fields and any other syntax error as i have not tested this code.
Please let me know If you face any issue.
Thanks,
Abhishek Bansal
Initial term of field expression must be a concrete SObject: Boolean at line 32 column 90
Thanks for your help
Please find the updated code below :
Thanks,
Abhishek Bansal