trying to write a trigger involving two objects, standard Case and custom Milestone__c. When Case status is marked "sold" and case is saved, create a milestone object. My code is partially working but I am afraid I am accidently looping twice, as two Milestone__c records are being created as opposed to the desired one. Below is the code in my triggerhandler for this, any suggestions are appreciated I'm hitting a wall:
public void createMilestone_After(map<Id,Case> oldMap, list<case> newList){
list<Milestone__c> newMilestoneList = new list<Milestone__c>();
SYstem.debug('****'+newList.size());
for(Case currCase : newList){
if(currCase.Status =='Sold' && currCase.Status != (oldMap.get(currCase.Id).Status)){
Milestone__c newMstone = new Milestone__c();
Employee__c em = [SELECT Id, OwnerId FROM Employee__c WHERE SF_User__c =:currCase.Sold_By__c];
newMstone.Employee__c = em.Id;
newMstone.Carrier_Milestone__c = currCase.Policy_Carrier__c;
newMilestoneList.add(newMstone);
System.debug('***'+newMilestoneList);
}
}
if(newMilestoneList.size()>0){
upsert newMilestoneList;
}
}