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
dcgb2332dcgb2332 

Apex Trigger Help!

I have a code currently in place: 1
2
3
4
5
6
7
Trigger AE_Opps_c ON Opportunity (After Insert, Before Update){

  List<Opportunity> oppList = [SELECT Id, Name FROM Opportunity];
  List<AE_Opps__c> aeList = new List<AE_Opps__c>();
    
    insert aeList;
    }

The purpose is to create a new list by pulling NEW opportunities and placing them in the list. There a two problems:

1) Every time a new opportunity is created, the existing opportunities are duplicated on the list
2) The Opportunity owner name is defaulting to my name and/or the other ADMIN.


Can someone please help me?
Raj VakatiRaj Vakati
Use the below code and Map other fields from Opportunity to AE_Opps__c​
 
Trigger AE_Opps_c ON Opportunity (After Insert, Before Update){

  List<Opportunity> oppList = [SELECT Id, Name FROM Opportunity];
  List<AE_Opps__c> aeList = new List<AE_Opps__c>();
    for(Opportunity opp :oppList){
	AE_Opps__c a = new AE_Opps__c() ;
	a.Name = opp.Name ; 
	// Map other fields from Opportunity to AE_Opps__c
	aeList.add(a);
	}
	if(aeList.size()>0){
    insert aeList;
	}
    }

 
dcgb2332dcgb2332

Hi Raj,

Thank you for the code.

I've input the code but it's still pulling the same opportunities again, creating duplicates, and assigning me as the Opp owner..

Navin Selvaraj23Navin Selvaraj23
Hi Sabrina,

Please try the below code.
 
Trigger AE_Opps_c ON Opportunity (After Insert){
  List<AE_Opps__c> aeList = new List<AE_Opps__c>();
    for(Opportunity opp :Trigger.New){
	AE_Opps__c a = new AE_Opps__c() ;
	a.Name = opp.Name ; 
	aeList.add(a);
	}
	if(!aeList.isEmpty()){
    insert aeList;
	}
    }

Best Regards,
Navin S​​​​​​​