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
Rachel Linder 8Rachel Linder 8 

Writing My First Trigger and Getting an Error

I am writing my first trigger. It involves two objects:
  1. Opportunity (Standard Object)
  2. Holding Object (Custom Object) 
What is supposed to happen is the following:

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.

User-added image

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.
Best Answer chosen by Rachel Linder 8
David HalesDavid Hales
Try this.
trigger Opportunity_Adelphic_AE_Email_Trigger on Opportunity (before insert, before update) {

	map<string, string> DM = new map<string, string>();
    Set<String> Person = new Set<String>();
    For(Opportunity opp : trigger.new)
	{
		if(opp.Adelphic_AE__c!=null)
		Person.add(opp.Adelphic_AE__c);
	}	
	
	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)

All Answers

Virender Singh 29Virender Singh 29
Hi Rachel Linder,

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




 
David HalesDavid Hales
Hi Rachel,

Please try this one.
 
trigger Opportunity_Adelphic_AE_Email_Trigger 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>();
	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))
		opp.Adelphic_AE_Email__c = DM.get(opp. Adelphic_AE__c);
	}

}




Thanks & Regards 
David Hales(KSPL1005)
Rachel Linder 8Rachel Linder 8
@Virender. Thanks. It allowed me to save and I received no errors. 
Virender Singh 29Virender Singh 29
Hi Rachel Linder 8,

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

 
Rachel Linder 8Rachel Linder 8
@David...
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
Rachel Linder 8Rachel Linder 8
@David

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
 
David HalesDavid Hales
Hi Rachel,

Remove space from line number 25.

        opp.Adelphic_AE_Email__c = DM.get(opp.Adelphic_AE__c);

Try this.
trigger Opportunity_Adelphic_AE_Email_Trigger 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>();
	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))
		opp.Adelphic_AE_Email__c = DM.get(opp.Adelphic_AE__c);
	}

}


 
Rachel Linder 8Rachel Linder 8
Thanks everyone. I removed extra spaces - good eye.

But now i get the error: Compile Error: Variable does not exist: Person at line 6 column 11
David HalesDavid Hales
Hi Rachel,

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.
 
trigger Opportunity_Adelphic_AE_Email_Trigger 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))
		opp.Adelphic_AE_Email__c = DM.get(opp.Adelphic_AE__c);
	}

}
Thanks & Regards 
David Hales(KSPL1005)



 
Rachel Linder 8Rachel Linder 8
Thanks David.
David HalesDavid Hales
Hi Rachel,

Please select Best answer, You have got your answer.

Thanks & Regards 
David Hales(KSPL1005)
Rachel Linder 8Rachel Linder 8
I will for sure check the best answer. But when I tested this by creating a new opportunity and choosing a name upon clicking the Save and Add Product button i get the following:

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
David HalesDavid Hales
Try this.

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)
Rachel Linder 8Rachel Linder 8
I now get the following error when trying to save the updates to the trigger 

Compile Error: unexpected syntax:  'mismatched input 'OR' expecting RPAREN' at line 5 column 39
David HalesDavid Hales
May i know what is data type of this field?

Try this.
 
trigger Opportunity_Adelphic_AE_Email_Trigger 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);
	}

}

 
Rachel Linder 8Rachel Linder 8
The data type of the Adalphic AE field is a picklist. The data type for the Adelphic AE email is Email.
Rachel Linder 8Rachel Linder 8
Error now when testing during creating a new oppty.:

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
David HalesDavid Hales
That's fine. Given code should be run. Can you please share your code with me?

Thanks & Regards 
David Hales(KSPL1005)
Rachel Linder 8Rachel Linder 8
David thanks again for your help. Here is my Apex Trigger (if that is what you are referring to:

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);
       }
             

}
David HalesDavid Hales
Try this.
trigger Opportunity_Adelphic_AE_Email_Trigger on Opportunity (before insert, before update) {

	map<string, string> DM = new map<string, string>();
    Set<String> Person = new Set<String>();
    For(Opportunity opp : trigger.new)
	{
		if(opp.Adelphic_AE__c!=null)
		Person.add(opp.Adelphic_AE__c);
	}	
	
	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)
This was selected as the best answer
Rachel Linder 8Rachel Linder 8
You rock...it's working in Sandbox. Tonight I will push to production and give it a try.
David HalesDavid Hales
Welcome, Rachel :) 
David HalesDavid Hales
Hi Rachel,

If code is working, Hit on best answer :)

Thanks & Regards 
David Hales(KSPL1005)
Rachel Linder 8Rachel Linder 8
I checked best answer but when I go back in and look at the case it looks like it isn’t checked. Can you see it checked on your end?
David HalesDavid Hales
Yes, Its checked. Thanks
Thanks & Regards 
David Hales(KSPL1005)