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
Arav SundarArav Sundar 

unexpected token error

HI All ,

learning SFDC coding , need a help please  .. in the Apex guide i am trying to execute the below code in the anonymus block in the developer console when i click on execute i am getting an error unexpected token at line 2 .. But if i save the same code directly in the apex class no error its getting saved , can anyone tell me how is it working .. And i am getting an error in the system.debug line where i am using as red cant i put the debug statement there pls confirm

public class MyClass {
class RGB {
Integer red;
system.debug (this is red : + red);
Integer green;
Integer blue;
//Static and Instance Methods, Variables, and Initialization Code
//Classes, Objects, and Interfaces
RGB(Integer red, Integer green, Integer blue)
system.debug(this is RGB : + RGB);
{ this.red = red;
this.green = green;
this.blue = blue; }
}
static Map<String, RGB> colorMap = new Map<String, RGB>();
system.debug(this is the static colormap : + colorMap);
static {
colorMap.put('red', new RGB(255, 0, 0));
colorMap.put('cyan', new RGB(0, 255, 255));
colorMap.put('magenta', new RGB(255, 0, 255)); }
}
Best Answer chosen by Arav Sundar
Amit Chaudhary 8Amit Chaudhary 8
Hi,

Just update you class same like below class.
public class MyClass 
{
    class RGB 
    {
        Integer red;
        Integer green;
        Integer blue;
        RGB(Integer red, Integer green, Integer blue)
        { 
            this.red = red;
            this.green = green;
            this.blue = blue; 
            system.debug('this is red :' + red);
        }
    }
    
static Map<String, RGB> colorMap = new Map<String, RGB>();
static 
{
    colorMap.put('red', new RGB(255, 0, 0));
    colorMap.put('cyan', new RGB(0, 255, 255));
    colorMap.put('magenta', new RGB(255, 0, 255)); 
}

  public MyClass()
  {
        system.debug( 'this is the static colormap : ' + colorMap);
  }

}

Please follow below step :-

Step 1) Click on Your Name then click on Developer console.
Step 2) In developer console click on Debug and open Anonymous window like below or click

User-added image

Step 3) Enter below code in Anonymous window like below
MyClass obj = new MyClass();

User-added image
Step 4) Click on Open log check box and click on execute like above screen shot.
Step 5) Then search your debug like below "this is the static colormap"

User-added image


I tested above code in my developer org which is working fine. Please let us know if this will help you

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com

All Answers

Mahesh DMahesh D
Hi Arav,

Please check the below modified code:
 
public class MyClass {
    class RGB {
        Integer red;
        //system.debug('this is red :' + red);
        Integer green;
        Integer blue;
        //Static and Instance Methods, Variables, and Initialization Code
        //Classes, Objects, and Interfaces
        RGB(Integer red, Integer green, Integer blue)
        //system.debug(this is RGB : + RGB);
        { this.red = red;
        this.green = green;
        this.blue = blue; }
    }
    static Map<String, RGB> colorMap = new Map<String, RGB>();
    //system.debug('this is the static colormap : '+ colorMap);
    static {
        colorMap.put('red', new RGB(255, 0, 0));
        colorMap.put('cyan', new RGB(0, 255, 255));
        colorMap.put('magenta', new RGB(255, 0, 255)); 
    }
}

The above class is saving it properly. But I would recommend to follow below links to learn about Apex Programming.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_intro_what_is_apex.htm

https://developer.salesforce.com/page/Apex

https://developer.salesforce.com/page/An_Introduction_to_Apex

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_intro_learning_apex.htm

https://resources.docs.salesforce.com/sfdc/pdf/salesforce_apex_language_reference.pdf


Please do let me know if it helps you.

Regards,
Mahesh
Amit Chaudhary 8Amit Chaudhary 8
I found below issue in your code
1) Debug system outside the constructor and method
2) No ' ' in Debug Statement

Please update  your class like below
public class MyClass 
{
    class RGB 
    {
        Integer red;
        Integer green;
        Integer blue;
        RGB(Integer red, Integer green, Integer blue)
        { 
            this.red = red;
            this.green = green;
            this.blue = blue; 
            system.debug('this is red :' + red);
        }
    }
    
static Map<String, RGB> colorMap = new Map<String, RGB>();
static 
{
    colorMap.put('red', new RGB(255, 0, 0));
    colorMap.put('cyan', new RGB(0, 255, 255));
    colorMap.put('magenta', new RGB(255, 0, 255)); 
}

  public MyClass()
  {
        system.debug( 'this is the static colormap : ' + colorMap);
  }

}

To excute above class try below code on developer console
MyClass obj = new MyClass();
Output will :-
36.0 APEX_CODE,DEBUG
Execute Anonymous: MyClass obj = new MyClass();
23:41:20.32 (32783658)|EXECUTION_STARTED
23:41:20.32 (32868597)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
23:41:20.32 (35921488)|USER_DEBUG|[13]|DEBUG|this is red :255
23:41:20.32 (36008255)|USER_DEBUG|[13]|DEBUG|this is red :0
23:41:20.32 (36071495)|USER_DEBUG|[13]|DEBUG|this is red :255
23:41:20.32 (36308886)|USER_DEBUG|[27]|DEBUG|this is the static colormap : {cyan=RGB:[blue=255, green=255, red=0], magenta=RGB:[blue=255, green=0, red=255], red=RGB:[blue=0, green=0, red=255]}
23:41:20.32 (36378018)|CODE_UNIT_FINISHED|execute_anonymous_apex
23:41:20.32 (38032682)|EXECUTION_FINISHED

Let us know if this will help you


 
Arav SundarArav Sundar

@ Mahesh - its saving proprely in the Apex class but if you execute the same  in the developer console you will get an error

@ Amit - R u telling to execute this in the Developer console if yes this is not happening and if you saving this in the Apex class how are you executing this code can you pls tell me  that

 

Mahesh DMahesh D
Hi Arav,

First save this class in Salesforce UI:
 
public class MyClass {
    class RGB {
        Integer red;
        Integer green;
        Integer blue;
        //Static and Instance Methods, Variables, and Initialization Code
        //Classes, Objects, and Interfaces
        RGB(Integer red, Integer green, Integer blue) { 
			this.red = red;
			this.green = green;
			this.blue = blue; 
		}
    }
	
    static Map<String, RGB> colorMap = new Map<String, RGB>();
    static {
        colorMap.put('red', new RGB(255, 0, 0));
        colorMap.put('cyan', new RGB(0, 255, 255));
        colorMap.put('magenta', new RGB(255, 0, 255)); 
    }
    public MyClass() {
        System.debug('===colorMap:'+colorMap);
    }
}

Go to Developer Console and create an object for the parent class:

MyClass mc = new MyClass();

This way it will work properly. Also don't forget to enable the debug so that it will be easy for you to see the output.

Regards,
Mahesh
Amit Chaudhary 8Amit Chaudhary 8
Hi,

Just update you class same like below class.
public class MyClass 
{
    class RGB 
    {
        Integer red;
        Integer green;
        Integer blue;
        RGB(Integer red, Integer green, Integer blue)
        { 
            this.red = red;
            this.green = green;
            this.blue = blue; 
            system.debug('this is red :' + red);
        }
    }
    
static Map<String, RGB> colorMap = new Map<String, RGB>();
static 
{
    colorMap.put('red', new RGB(255, 0, 0));
    colorMap.put('cyan', new RGB(0, 255, 255));
    colorMap.put('magenta', new RGB(255, 0, 255)); 
}

  public MyClass()
  {
        system.debug( 'this is the static colormap : ' + colorMap);
  }

}

Please follow below step :-

Step 1) Click on Your Name then click on Developer console.
Step 2) In developer console click on Debug and open Anonymous window like below or click

User-added image

Step 3) Enter below code in Anonymous window like below
MyClass obj = new MyClass();

User-added image
Step 4) Click on Open log check box and click on execute like above screen shot.
Step 5) Then search your debug like below "this is the static colormap"

User-added image


I tested above code in my developer org which is working fine. Please let us know if this will help you

Thanks
Amit Chaudhary
amit.salesforce21@gmail.com
This was selected as the best answer