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
orikkerorikker 

Any Idea why Trigger creates duplicate records for some but not all opportunities?

 

trigger farmingTasksGenerator on Opportunity (after update) {
	Task t;
	List <Task> tasksToInsert = new List <Task> ();
	List <Opportunity> oppList = [SELECT Id, Status_Code__c, FA__c, Name, Senior_HMA__c
								 FROM Opportunity WHERE Id IN :Trigger.NewMap.keySet()];
	
	
	for (Opportunity opp: oppList) {
		
		if (Trigger.newMap.get(opp.Id).Status_Code__c != Trigger.oldMap.get(opp.Id).Status_Code__c  ) {
			
			if (opp.Status_Code__c == 'A15' ||
				opp.Status_Code__c == 'C10' ||
				opp.Status_Code__c == 'C20' ||
				opp.Status_Code__c == 'E10'	) {
				
				t = new Task (Subject = 'Farming task - ' + opp.Status_Code__c + 
										' - loan ' + opp.Name, WhoId = opp.FA__c,
								ActivityDate = System.Today()+1, WhatId = opp.Id, Status = 'Not Started', 
								RecordTypeId = '012300000008qRE', Farming_Task__c = true, Type = 'OUTBOUND CALL',
								OwnerId = opp.Senior_HMA__c, isReminderSet = true, ReminderDateTime = System.Now(),
								Farming_Task_Description__c = 'Farming task - ' + opp.Status_Code__c + 
										' - loan ' + opp.Name);
				tasksToInsert.add (t);
			}
		}
	}
	
	insert tasksToInsert;
	tasksToInsert.clear();
	oppList.clear();

}

 We have a trigger that is supposed to create tasks for specific loans that are moved to a specific status. Update is run every morning and updates 3000 records from CSV file and that is when trigger is fired. For some reason, it creates the same task for some but not all records... Any ideas?

 

Thank you. 

 

Best Answer chosen by Admin (Salesforce Developers) 
kritinkritin

Hi orikker,

 

If you have any workflow field updates defined, then it re-execute the trigger,so verify your workflow rule written on Opportunity and first disble these workflow rule and see weather this problem persist or not.

 

Once any update is happening or record and if workrule is there then it re-executed the trigger that's why it is going to recreate the records.

 

Disable you workflow rule field update for Opportunity and run trigger logic again. and hope it will help you.

 

 

All Answers

kritinkritin

Hi orikker,

 

If you have any workflow field updates defined, then it re-execute the trigger,so verify your workflow rule written on Opportunity and first disble these workflow rule and see weather this problem persist or not.

 

Once any update is happening or record and if workrule is there then it re-executed the trigger that's why it is going to recreate the records.

 

Disable you workflow rule field update for Opportunity and run trigger logic again. and hope it will help you.

 

 

This was selected as the best answer
orikkerorikker

That was the problem...

 

I had to create static set to track opps that have already been updated and task created...

 

Thank you.