You need to sign in to do that
Don't have an account?

DML statment cannot operate on trigger.new or trigger.old
I'm trying to write a simple trigger that renames a record. I keep getting the error "CallLineItemRename: execution of BeforeInsert
caused by: System.SObjectException: DML statment cannot operate on
trigger.new or trigger.old: Trigger.CallLineItemRename: line 18, column
9" . What am I missing here?
trigger CallLineItemRename on Call_Line_Item__c (before insert, before update) { // this triger renames the call line item id to be // the specified inventory item name or the product name List<Call_Line_Item__c> callItems = new List<Call_Line_Item__c>(); callItems = Trigger.new; for (Call_Line_Item__c callItem : callItems) { //check if inventory or product was specified if (callItem.Inventory_Item__c <> NULL) { callItem.Name = callItem.Inventory_Item__r.Name; callItem.Name = 'inv item name'; } else if (callItem.Products__c <> NULL) { callItem.Name = callItem.Products__r.Name; } } update callItems; }
All Answers
What about: callItem.Inventory_Item__c != null
Not sure if that evalutes any different...but worth a shot.
I'm getting the same error from an after insert/after update trigger. The goal is to update the triggering object so that the trigger can ignore that object on subsequent updates.
Sample code (edited for brevity):
trigger NotifyTPAOfBug on Case (after insert, after update) { for(Case c : Trigger.new) { if ( c.Status == 'Closed' && c.BugNoteSent__c == FALSE ) { Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); Messaging.sendEmail(new Messaging.Email[] { email }); c.BugNoteSent__c = TRUE; update c; } } }
Sir,
When remove the update statement ,it works but it creates one new record ,previous record is not updated,actually there is no updation,it is inserted as a new record,how can i update the field of existing record at the time of insert?
please help me................
Thanking You....
Worked for me! Thank you!
Hi,
Its bit confusing : Sometimes it works without update/insert state,ments and sometime it doesnt(before triggers)...ANy explanation please..
Example 1 do not work without insert statement
trigger updatevendor on Opportunity (before insert,before update)
{
List<Vendor__c> vends= new List<Vendor__c>();
Map<ID,Opportunity> campIdOppMap = new Map<ID,Opportunity> ();
//lets itereate thru the opportunity in the trigger.new i.e. the opportunities being inserted
for (Opportunity t : trigger.new){
if (t.CampaignId!= null & t.LeadSource=='Web' )
system.debug('The selected opportunity for the update is '+t);
//this map will hold the campaignID of the campaigns associated with the opportunities(being inserted) and the opportunity record
campIdOppMap.put(t.CampaignId,t);
}
//The custom object also holds the campaignId to which it is related in campaign__c
//So lets SELECT the records from custom object vendor__c which is associated to the campaignIds collected in map
//i.e. campaign__c = campIdOppMap.keyset() and then iterate thru them
for (Vendor__c cv : [Select Campaign__c,Campaign__r.id,updatevendor__c From Vendor__c where Campaign__c IN :campIdOppMap.keyset()]){
//as it is a before trigger we do not need to write the update/insert statement
system.debug(cv.Campaign__c +'The values in campaign__c and cv.campaign__r.Id '+ cv.campaign__r.Id );
cv.updatevendor__c= campIdOppMap.get(cv.Campaign__c).Amount;
system.debug('The amount field has this value in the opportunity'+cv.updatevendor__c);
vends.add(cv);
system.debug('Here is the list to be inserted'+vends);
}
upsert vends;
}
Example 2 : works without the insert/update statement
trigger ChangeNegativeQuantity on OpportunityLineItem (before insert) {
List<OpportunityLineItem> products = Trigger.new;
for(OpportunityLineItem p : products){
Opportunity opty = [SELECT RecordtypeId,Type FROM Opportunity WHERE id = :p.opportunityId];
system.debug(opty);
if (opty.RecordTypeId == '01290000000ggQ2AAI' ) {
system.debug(opty.RecordTypeId);
p.Quantity *= -1.0;
system.debug(p.Quantity);
}
}