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
CarlBCarlB 

Duplicate record created, but trigger only called once

Hello.

I have a very odd situation. 

I have some code that makes a web service call and then inserts a record based on the result of this call.  The insert causes an after-insert trigger to run that updates a running-total amount field in a related record (it isn't a master-detail relationship, so can't use a rollup field).

This code has been installed as a managed package on various customer's orgs and works ok.

However, on one customer's org something strange happened.  The user invoked the functionality, but two identical records were created (down to having the same created timestamp).  But, the trigger appears to have only run once, because the total field in the related record only reflects a single update.

This leaves me with two possibilities:

1) Everything worked as expected, but the trigger ran twice in parallel, meaning that one update overwrote the other update
2) Salesforce has a bug that causes duplicate records to be created without any other effect.

Any thoughts?

Regards,

Carl

 
JayantJayant
Can you paste the trigger code/pseudo code here ?
surasura
make sure trigger defined object does not have workflows defined becuase workflows make your triggers execute twice 
refer https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm
 
CarlBCarlB
Hello.

The trigger is:
 
trigger AfterInsert on A__c (after insert) {
  
 	Set<Id> ids = new Set<Id>();
	for (A__c a : Trigger.new) {
		if (a.Link__c!=null) {
			ids.add(a.Link__c);
		}
	}

	Map<Id, B__c> bs = new Map<Id, B__c>([ Select id, total__c, number__c From B__c Where id in :ids]);
	
	for (A__c a : Trigger.new) {
		B__c b = bs.get(a.Link__c);
		b.Number__c++;
		b.Total__c = b.Total__c + a.Amount__c;
	}

	update bs.values();

}

The VF controller extension page action is:
 
PageReference ref = new PageReference(...);

...

HttpRequest req = new HttpRequest();
req.setEndpoint(ref.getURL());
req.setMethod('GET');

HttpResponse res = new Http().send(req);

...

A__c a = new A__c();
a.Link__c = ...;
a.Amount__c = ...;
				
insert a;
			
return new PageReference(...);

So, the user clicked a button that ran the above code, but two A__c records were created (identical creation timestamps).  The trigger appears to have only processed only one of the A__c records because the B__c record linked to the records ended up with a Number__c of 1 and a Total__c of Amount__c (rather than Amount__c * 2)

There are no workflows defined for either A__c or B__c.

Regards,

Carl


 
CarlBCarlB
Hello

Something that occured to me is - does Salesforce prevent double form submission on its standard buttons?  That is to say, if I click a button on a standard Salesforce page, does Salesforce do anything to prevent me very quickly clicking the same button?

Similarly, if two records are inserted at the same time, is it possible for the same trigger to be running twice at the same time?  If so, in the above example, should I be doing an "FOR UPDATE" select on object B__c?

Regards,

Carl