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 

Need trigger to pull Opp name

Hello,
I'm new to triggers and struggling with this. I need a trigger that pulls the Opportunity name and creates a new record in my custom object, AE Opp. I do not need it to create a lookup or link it to the opp, i just need it to create a whole new record so that I can create a task on it.. Is this feasible?
Best Answer chosen by dcgb2332
GauravGargGauravGarg
Trigger Opportunity_AT On Opportunity (Before Insert, Before Update)
{
	List<AE_Opp__c> aeList = new List<AE_Opp__c>();
	for(Opportunity opp : Trigger.New)
	{
		aeList.add(new AE_Opp__c(Name = opp.Name));
	}

	insert aeList;
}

Try this.

All Answers

Leonardi KohLeonardi Koh
This should not be a problem for trigger to do, but before we can design and build a trigger for it we would need to know at what point should the trigger be fired/started and perform this action.

An example: we could build a trigger that is designed to initiate when you create Opportunity, automatically pull the Opportunity name and create the custom object record. Or we could also build a trigger that does the same thing but initiate when you update Opportunity instead.

Since you need the Opportunity name to create the new record in the custom object, then chances are the trigger will need to be initiated from an action involving Opportunity, but which one that is will depend on your need and will need to be elaborated.
GauravGargGauravGarg
List<Opportunity> oppList = [SELECT Id, Name FROM Opportunity];
List<AE_Opp__c> aeList = new List<AE_Opp__c>();
for(Opportunity opp : oppList)
{
      aeList.add(new AE_Opp__c(Name = opp.Name));
}

insert aeList;
Hi Sabrina,

Please try above code. 

Thanks,
Gaurav
skype: gaurav62990 
 
dcgb2332dcgb2332
Gaurav,

I received an error when I tried your code. (Error: Compile Error: Unexpected token 'List'. at line 1 column 1.. did I missenter something?
dcgb2332dcgb2332
Leonardi,

To answer your question: The trigger would need to pull once the opportunity is created, that way the AE team can look at the AE Opp custom object..

Thank you!
GauravGargGauravGarg
Sabrina,
 
Trigger On Opportunity opp_AT (Before Insert, Before Update){
  List<Opportunity> oppList = [SELECT Id, Name FROM Opportunity];
List<AE_Opp__c> aeList = new List<AE_Opp__c>();
for(Opportunity opp : oppList)
{
      aeList.add(new AE_Opp__c(Name = opp.Name));
}

insert aeList;
}

Try this

Thanks,
Gaurav
Skype: gaurav62990​
dcgb2332dcgb2332
Guarav,

The trigger worked kind of (I had to change some of the verbiage as the API name was different), however, when I create a new Opportunity, it does not create the new record in the AE Opps tab. What could I be doing incorrectly?

Thanks!
GauravGargGauravGarg

Hi Sabrina,

Could you please check if there any required fields on AE_Opp__c. Then that need to be populated too.

Thanks,

Gaurav

dcgb2332dcgb2332
I checked, there are no required fields other than the Opp Name..

Thanks!
GauravGargGauravGarg
Trigger Opportunity_AT On Opportunity (Before Insert, Before Update)
{
	List<AE_Opp__c> aeList = new List<AE_Opp__c>();
	for(Opportunity opp : Trigger.New)
	{
		aeList.add(new AE_Opp__c(Name = opp.Name));
	}

	insert aeList;
}

Try this.
This was selected as the best answer
dcgb2332dcgb2332
Gaurav,

You are the best!!! Thank you so so much!
GauravGargGauravGarg
Your welcome Sabrina.