You need to sign in to do that
Don't have an account?
Problem with trigger
HI, this is my first trigger. Kindly tell me what mistake am doing?
1. The problem is : Every time this trigger is executed the record gets inserted twice...
How to stop this?
Please find the code below:
trigger SalesOrder on Sales_Order__c (after update){
List <Sales_Order_Status_History__c> sosh = new List<Sales_Order_Status_History__c>();
if(Trigger.isafter){
for(Sales_Order__c so : Trigger.new){
if(so.Sales_Order_Status__c == 'Order Acknowledgement'){
sosh.add(new Sales_Order_Status_History__c(Status__c = so.Sales_Order_Status__c, SO_No__c = so.Name,Record_Created_Date__c=so.createdDate,Last_Modified_Date__c=so.systemModStamp,Record_User__c=userinfo.getUserName()));
insert sosh;
}
else if(so.Sales_Order_Status__c == 'Customer Inputs'){
sosh.add(new Sales_Order_Status_History__c(Status__c = so.Sales_Order_Status__c, SO_No__c = so.Name,Record_Created_Date__c=so.createdDate,Last_Modified_Date__c=so.systemModStamp,Record_User__c=userinfo.getUserName()));
insert sosh;
}
else if(so.Sales_Order_Status__c == 'Scheduling'){
sosh.add(new Sales_Order_Status_History__c(Status__c = so.Sales_Order_Status__c, SO_No__c = so.Name,Record_Created_Date__c=so.createdDate,Last_Modified_Date__c=so.systemModStamp,Record_User__c=userinfo.getUserName()));
insert sosh;
}
}}}
Please help
This should give you what you want. The if statement checks to see if that value has changed. What you did before does not do that. If it is still inserting 2 records at a time... My guess is that there is another trigger somewhere causing this to fire again.
All Answers
Your trigger is executed after your records are inserted, and you're inserting them a second time. Use the DML update statement to update your original records.
Easy. You are inserting your entire list every time. NEVER do an insert inside of a loop.
This is what I have done. It did not work. I tried insert and update DML operation alternatively. Its not working. Can you please tell me what mistake am doing?
trigger SalesOrder on Sales_Order__c (after update){
//delete savepoint
Savepoint sp = Database.setSavepoint();
List <Sales_Order_Status_History__c> sosh = new List<Sales_Order_Status_History__c>();
if(Trigger.isUpdate){
for(Sales_Order__c so : Trigger.new){
if(so.Sales_Order_Status__c == 'Order Acknowledgement' ||
so.Sales_Order_Status__c == 'Customer Inputs' ||
so.Sales_Order_Status__c == 'Scheduling' ||
so.Sales_Order_Status__c == 'Order Acceptance' ||
so.Sales_Order_Status__c == 'Design Queue' ||
so.Sales_Order_Status__c == 'Design' ||
so.Sales_Order_Status__c == 'Punching/Bending Queue' ||
so.Sales_Order_Status__c == 'Punching/Bending' ||
so.Sales_Order_Status__c == 'Fabrication Queue' ||
so.Sales_Order_Status__c == 'Fabrication' ||
so.Sales_Order_Status__c == 'Customer Inspection' ||
so.Sales_Order_Status__c == 'Powder Coating Queue' ||
so.Sales_Order_Status__c == 'Pre-Treatment & Powder Coating' ||
so.Sales_Order_Status__c == 'Assembly Queue' ||
so.Sales_Order_Status__c == 'Assembly' ||
so.Sales_Order_Status__c == 'Finished Goods Queue' ||
so.Sales_Order_Status__c == 'Finished Goods' ||
so.Sales_Order_Status__c == 'Pending Closure' ||
so.Sales_Order_Status__c == 'Closed' ||
so.Sales_Order_Status__c == 'Cancelled' ){
sosh.add(new Sales_Order_Status_History__c(Status__c = so.Sales_Order_Status__c, SO_No__c = so.Name,Record_Created_Date__c=so.createdDate,Last_Modified_Date__c=so.systemModStamp,Record_User__c=userinfo.getUserName()));
}
}
//insert outside for loop. Commented the update DML operation.
insert sosh;
//delete try-catch
try {
// update operation. Commented the insert DML operation.
update sosh;
} catch (DmlException e) {
Database.rollback(sp);
}
}
I'm not sure why you have this. Seems pretty useless to me:
Are you just putting EVERY value the Order status could possibly have? Or are there some missing? If it is every status, then do you even need this if statement? Should you instead just make sure it's not null?
This is what I have done. It did not work. I tried insert and update DML operation alternatively. Its not working. Can you please tell me what mistake am doing?
What do you mean by 'not working'? You need to clarify what that means.
Sorry for the try-catch section. I was just trying it.
Coming to the point, actually the purpose of this trigger is to execute whenever the Sales_Order_Status__c field is updated from its current value to any value.
There is a custom object called sales_order__c with a picklist field Sales_Order_Status__c.
There is another custom object called Sales_Order_Status_History__c which will store the history info about the sales_order__c object.
What the trigger should do?
The trigger should insert a new record into the custom object Sales_Order_Status_History__c every time the custom object sales_order__c's picklist field Sales_Order_Status__c's value is changed(updated).
Problem is: When the value of Sales_Order_Status__c field is updated the records are being inserted into Sales_Order_Status_History__c object for twice.
I was asked in the discussion forum to use DML operation Update and not Insert.
Others told me to use DML operation insert outside the for loop. I did both the things but that did not work and still the records are being inserted twice.
How to solve this problem?
This should give you what you want. The if statement checks to see if that value has changed. What you did before does not do that. If it is still inserting 2 records at a time... My guess is that there is another trigger somewhere causing this to fire again.
Thanks for the reply. It did not work. No there is no trigger. This is my first trigger and also system's first trigger.
I have deactivated all the workflows related to this sales_order object.
There was a look up field between Sales_Order_Status_History__c and Sales_Order__c I deleted that field also.
Even then its not working.
My other requirement is how to find the duration or time taken to change the value of the picklist field Sales_Order_Status__c from its current value to some other value?
Are you using a standard page to modify these? Or a custom page with a controller or extension?
My other requirement is how to find the duration or time taken to change the value of the picklist field Sales_Order_Status__c from its current value to some other value?
You can compare the CreatedDate with the last Status and the new status.
No. Am not using any custom page. Its a simple trigger. Am writing a trigger for the first time.
My other requirement is how to find the duration or time taken to change the value of the picklist field Sales_Order_Status__c from its current value to some other value?
You can compare the CreatedDate with the last Status and the new status.
How? Can you please elaborate this?
I tried something like this: Queried CreatedDate and systemModStamp and used the two values in a formula field as below:
Duration = lastModifiedDate - CreatedDate.
I think it should be something like
Duration = lastModifiedDatePresentStatus - lastModifiedDatePreviousStatus. Here how can I get the lastModifiedDatePreviousStatus? I tried before update trigger with a section of the code in the trigger calculating lastmodifiedDate before update for trigger.old. But the problem is the section of the code related to after update is not getting executed.
What is CreatedDate here? Is it the record CreatedDate? If so then will it not be same even if we update the record?
What is lastModifiedDate?
Code suggested by Damien_ seems perfectly fine.If it is still not working then it means that there is some other wrong things going on.
Can you make sure that there is only one trigger in the system which is same as suggested by Damien_.If it is still an issue then reach me at syedjafferali@live.com so I will try to arrange a meeting and then we can resolve this one.
Are you there? Can I message you now? Its very urgent for me. That is why am buring the mid night oil till now.