You need to sign in to do that
Don't have an account?
Rachel Linder 8
Writing My First Trigger and Getting an Error
I am writing my first trigger. It involves two objects:
A name is chosen from a picklist field on the opportunity. Upon save the trigger should go look at the Holding Object object and find that name and pull the email address from that Persons record on the custom object.
Here is screenshot of what I have so far including the error.
The actual code is below (in case the above is hard to read):
trigger Opportunity_Adelphic_AE_Email_Trigger on Opportunity (after update, before insert, before update, before delete) {
Opportunity [] opp = trigger.new;
//Fill in the Adelphic AE email fields based on the person selected from the drop down list.
for(integer i=0;i <opp.size();i++){
Set<string> Person = new Set<string>();
Person.add(trigger.new[i]. Adelphic_AE__c);
if(Person.Size() > 0){
map<string, string> DM = new map<string, string>();
Holding_Object__c [] DMM = [select Persons_Name__c, Email_Address__c from Holding_Object __c where Persons_Name__c in: Person ];
for(Holding_Object__c lzip: DMM){
DM.put(lzip.Persons_Name__c, lzip.Email_Address__c);
}
for(Holding_Object__c h: DMM){
trigger.new[i]. Adelphic_AE_Email__c = DM.get(trigger.new[i]. Adelphic_AE__c);
}
}
The error I am receiving is: "Compile Error: unexpected syntax: 'mismatched input '<EOF>' expecting RCURLY' at line 20 column 1.
Any feedback and guidance would be appreciated.
- Opportunity (Standard Object)
- Holding Object (Custom Object)
A name is chosen from a picklist field on the opportunity. Upon save the trigger should go look at the Holding Object object and find that name and pull the email address from that Persons record on the custom object.
Here is screenshot of what I have so far including the error.
The actual code is below (in case the above is hard to read):
trigger Opportunity_Adelphic_AE_Email_Trigger on Opportunity (after update, before insert, before update, before delete) {
Opportunity [] opp = trigger.new;
//Fill in the Adelphic AE email fields based on the person selected from the drop down list.
for(integer i=0;i <opp.size();i++){
Set<string> Person = new Set<string>();
Person.add(trigger.new[i]. Adelphic_AE__c);
if(Person.Size() > 0){
map<string, string> DM = new map<string, string>();
Holding_Object__c [] DMM = [select Persons_Name__c, Email_Address__c from Holding_Object __c where Persons_Name__c in: Person ];
for(Holding_Object__c lzip: DMM){
DM.put(lzip.Persons_Name__c, lzip.Email_Address__c);
}
for(Holding_Object__c h: DMM){
trigger.new[i]. Adelphic_AE_Email__c = DM.get(trigger.new[i]. Adelphic_AE__c);
}
}
The error I am receiving is: "Compile Error: unexpected syntax: 'mismatched input '<EOF>' expecting RCURLY' at line 20 column 1.
Any feedback and guidance would be appreciated.
Thanks & Regards
David Hales(KSPL1005)
All Answers
You need to add two more curly braces at the end of class. When you will add { curly braces at line no. 20 and save this trigger you will get compile time error saying that expecting RCURLY' at line 21 column 1. so have to add one more curly braces.
So final code will look like below.
trigger Opportunity_Adelphic_AE_Email_Trigger on Opportunity (after update, before insert, before update, before delete) {
Opportunity [] opp = trigger.new;
//Fill in the Adelphic AE email fields based on the person selected from the drop down list.
for(integer i=0;i <opp.size();i++){
Set<string> Person = new Set<string>();
Person.add(trigger.new[i]. Adelphic_AE__c);
if(Person.Size() > 0){
map<string, string> DM = new map<string, string>();
Holding_Object__c [] DMM = [select Persons_Name__c, Email_Address__c from Holding_Object __c where Persons_Name__c in: Person ];
for(Holding_Object__c lzip: DMM){
DM.put(lzip.Persons_Name__c, lzip.Email_Address__c);
}
for(Holding_Object__c h: DMM){
trigger.new[i]. Adelphic_AE_Email__c = DM.get(trigger.new[i]. Adelphic_AE__c);
}
}
}
}
Note** First just save the trigger after adding two curly braces and let me know if any further concern.
Please mark as Best answer if it solved your problem
Regards,
Virender Singh
Please try this one.
Thanks & Regards
David Hales(KSPL1005)
Please select as best answer as it will give boost to my work and I will try to help with more energy to all whoever needs it :)
Regards,
Virender Singh
I tried your logic above and it gives me a compile error on save:
Compile Error: unexpected syntax: 'extraneous input "Adelphic_AE__c' ex[ecting RPAREN' at line 25 column 34
I get the following error when saving your code above:
Compile Error: unexpected syntax: 'extraneous inut 'Adelphic_AE__C' expecting RPAREN' at line 25 column 34
Remove space from line number 25.
opp.Adelphic_AE_Email__c = DM.get(opp.Adelphic_AE__c);
Try this.
But now i get the error: Compile Error: Variable does not exist: Person at line 6 column 11
Set<string> Person = new Set<string>(); was missed. Please use this and try. Try this code because it's a standard code as i can see in your code you are using for loop it is not good in trigger.
Thanks & Regards
David Hales(KSPL1005)
Please select Best answer, You have got your answer.
Thanks & Regards
David Hales(KSPL1005)
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Opportunity_Adelphic_AE_Email_Trigger_Ver2 caused an unexpected exception, contact your administrator: Opportunity_Adelphic_AE_Email_Trigger_Ver2: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Opportunity_Adelphic_AE_Email_Trigger_Ver2: line 7, column 1
trigger Opportunity_Adelphic_AE_Email_Trigger on Opportunity (before insert, before update) {
for(Opportunity opp : trigger.new)
{
if(opp.Adelphic_AE__c!=null OR opp.Adelphic_AE__c != '')
Person.add(opp.Adelphic_AE__c);
}
map<string, string> DM = new map<string, string>();
Set<String> Person = new Set<String>();
List<Holding_Object__c> HoldingLst = new List<Holding_Object__c>();
if(Person!=null && Person.size()>0)
{
HoldingLst=[select Persons_Name__c, Email_Address__c from Holding_Object__c where Persons_Name__c in: Person];
}
if(HoldingLst!=null && HoldingLst.size()>0)
{
for(Holding_Object__c hd : HoldingLst)
{
DM.put(hd.Persons_Name__c, hd.Email_Address__c);
}
}
for(opportunity opp : trigger.new)
{
if(DM.Containskey(opp.Adelphic_AE__c) && DM.get(opp.Adelphic_AE__c)!=null)
opp.Adelphic_AE_Email__c = DM.get(opp.Adelphic_AE__c);
}
}
Thanks & Regards
David Hales(KSPL1005)
Compile Error: unexpected syntax: 'mismatched input 'OR' expecting RPAREN' at line 5 column 39
Try this.
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger Opportunity_Adelphic_AE_Email_Trigger_Ver2 caused an unexpected exception, contact your administrator: Opportunity_Adelphic_AE_Email_Trigger_Ver2: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.Opportunity_Adelphic_AE_Email_Trigger_Ver2: line 6, column 1
Thanks & Regards
David Hales(KSPL1005)
trigger Opportunity_Adelphic_AE_Email_Trigger_Ver2 on Opportunity (before insert, before update) {
for(Opportunity opp : trigger.new)
{
if(opp.Adelphic_AE__c!=null)
Person.add(opp.Adelphic_AE__c);
}
map<string, string> DM = new map<string, string>();
Set<String> Person = new Set<String>();
List<Holding_Object__c> HoldingLst = new List<Holding_Object__c>();
if(Person!=null && Person.size()>0)
{
HoldingLst=[select Persons_Name__c, Email_Address__c from Holding_Object__c where Persons_Name__c in: Person];
}
if(HoldingLst!=null && HoldingLst.size()>0)
{
for(Holding_Object__c hd : HoldingLst)
{
DM.put(hd.Persons_Name__c, hd.Email_Address__c);
}
}
for(opportunity opp : trigger.new)
{
if(DM.Containskey(opp.Adelphic_AE__c) && DM.get(opp.Adelphic_AE__c)!=null)
opp.Adelphic_AE_Email__c = DM.get(opp.Adelphic_AE__c);
}
}
Thanks & Regards
David Hales(KSPL1005)
If code is working, Hit on best answer :)
Thanks & Regards
David Hales(KSPL1005)
Thanks & Regards
David Hales(KSPL1005)