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
Daniel MasonDaniel Mason 

Apex Class unexpected token '{'

HI All, 

I am hoping you can help me. I am very new to VF and Apex and been using different resources on line and i am now stuck and dont know how to proceed. i am getting the following error 'unexpected token: '{' at line 39 column 26' when trying to save the class

Aim : The user should go to the Sales & Marketing Object, click on a related list, this should then open up a VF page. The VF page should show all of the records from the Materials Object. The user should select the records and the Quanity value, upon save this should then map back to  Sales & Marketing Object

I have created the following three objects
 
Materials = 01I20000000rV5S
Materials Junction = 01I20000000rV6B
Sales and Marketing = 01I20000000rV6V

This is the class that i have produced  


public with sharing class test
{
    public List<Materials__c> Materials {get;set;} //global variables
    public List<materialWrapper> materialWrapperList {get;set;} //use this list on vf page and not Materials

    //constructor
    public test()
    {
        Materials = [select ID,name,Product__c, Item__c,Quanity__c, Active__c from Materials__c where Active__c =true limit 10];
        for(Materials__c obj : Materials){
            materialWrapper tempObj= new materialWrapper();
            tempObj.recordId = obj.id;
            tempObj.name = obj.name;
            tempObj.product = obj.Product__c;
            tempObj.item = obj.Item__c;
            tempObj.quantity = obj.Quanity__c;
            tempObj.select = false;
            materialWrapperList.add(tempObj);
        }
    }

    //save method
    public void save()
    {
        list<Materials_Junction__c> recordToInsert = new list<Materials_Junction__c>();
        for(materialWrapper obj : materialWrapperList){
            if(obj.select == true){
                Materials_Junction__c temp = new Materials_Junction__c();
                temp.sales_and_marketing__c = '01I20000000rV6V';
                temp.Materials= obj.recordId;
                temp.quantity = obj.quantity; 
            }
            recordToInsert.add(obj);
        }
        insert recordToInsert;
    }
    
    
    public materialWrapper{
        string recordId {get; set;}
        string name {get; set;}
        string product {get; set;}
        string item {get; set;}
        string quantity {get; set;}
        boolean select {get; set;}
        
        public void materialWrapper(){
            recordId = '';
            name = '';
            product = '';
            item = '';
            quantity = '';
            select = false;
        }
    }
}

Materials Junction Fields
User-added image

Materials Object Fields 
User-added image

Hoping you can help 

Looking forward to your response 

Many thanks 
Pramod GowdaPramod Gowda
Hi,
You missed word "class" on line number 39,
Replace the line 39 with below line and try
"public class materialWrapper" 

I hope it works and you will not get error.

Thanks
Daniel MasonDaniel Mason

HI Parmod, 

I then get the following error " unexpected token: 'select' at line 45 column 16" 

Amit Chaudhary 8Amit Chaudhary 8
Hi Daniel,

Please try to avoid using Standard keyword. I just updated / Fixed your class. Please try below code
public with sharing class testWrapper
{
    public List<Materials__c> Materials {get;set;} 
    public List<materialWrapper> materialWrapperList {get;set;} 
	
    public testWrapper()
    {
        Materials = [select ID,name,Product__c, Item__c,Quanity__c, Active__c from Materials__c where Active__c =true limit 10];
        for(Materials__c obj : Materials)
		{
            materialWrapper tempObj= new materialWrapper();
            tempObj.recordId = obj.id;
            tempObj.name = obj.name;
            tempObj.product = obj.Product__c;
            tempObj.item = obj.Item__c;
            tempObj.quantity = obj.Quanity__c;
            tempObj.selectB = false;
            materialWrapperList.add(tempObj);
        }
    }

    //save method
    public void save()
    {
        list<Materials_Junction__c> recordToInsert = new list<Materials_Junction__c>();
		
        for(materialWrapper obj : materialWrapperList)
		{
            if(obj.selectB == true)
			{
                Materials_Junction__c temp = new Materials_Junction__c();
                temp.sales_and_marketing__c = '01I20000000rV6V';
                temp.Materials= obj.recordId;
                temp.quantity = obj.quantity; 
            }
            recordToInsert.add(obj);
        }
        insert recordToInsert;
    }
    
    
    public class materialWrapper
	{
        public string recordId {get; set;}
        public string name {get; set;}
        public string product {get; set;}
        public string item {get; set;}
        public string quantity {get; set;}
        public boolean selectB {get; set;}
        
        public void materialWrapper()
		{
            recordId = '';
            name = '';
            product = '';
            item = '';
            quantity = '';
            selectB = false;
        }
    }
}

Changes i did
1) Added class keyword in wrapper class
2) Select keyword replace with selectB
3) Updated varaible as public in wrapper class

Let us know if this will help you
Pramod GowdaPramod Gowda
Hi,
"select" is the like keyword so change the name of the variable
eg: isSelect

Thanks
Daniel MasonDaniel Mason

HI Amit and Gowda, 

Looks like the main change is on line 58. I forget that that select is a standard sf function. Great spot :) 

I do get a new error though Error: Illegal assignment from Decimal to String at line 16 column 13

Why is nothing simple hmpp 

Pramod GowdaPramod Gowda
Change Line 48 to  "public Decimal quantity;"
Change Line 57 to "quantity = 0.0;"
Daniel MasonDaniel Mason

HI Pramod,

 

I have made the following changes, however i know get the Invalid field Materials for SObject Materials_Junction__c at line 33 column 17. 

If you have a mo can we screen share and try and TS together ?

public with sharing class testWrapper
{
    public List<Materials__c> Materials {get;set;} 
    public List<materialWrapper> materialWrapperList {get;set;} 
    
    public testWrapper()
    {
        Materials = [select ID,name,Product__c, Item__c,Quanity__c, Active__c from Materials__c where Active__c =true limit 10];
        for(Materials__c obj : Materials)
        {
            materialWrapper tempObj= new materialWrapper();
            tempObj.recordId = obj.id;
            tempObj.name = obj.name;
            tempObj.product = obj.Product__c;
            tempObj.item = obj.Item__c;
            tempObj.quantity = obj.Quanity__c;
            tempObj.selectB = false;
            materialWrapperList.add(tempObj);
        }
    }

    //save method
    public void save()
    {
        list<Materials_Junction__c> recordToInsert = new list<Materials_Junction__c>();
        
        for(materialWrapper obj : materialWrapperList)
        {
            if(obj.selectB == true)
            {
                Materials_Junction__c temp = new Materials_Junction__c();
                temp.sales_and_marketing__c = '01I20000000rV6V';
                temp.Materials= obj.recordIdId;
                temp.quantity = obj.quantity; 
            }
            recordToInsert.add(obj);
        }
        insert recordToInsert;
    }
    
    
    public class materialWrapper
    {
        public string recordId {get; set;}
        public string name {get; set;}
        public string product {get; set;}
        public string item {get; set;}
        public Decimal quantity {get; set;}
        public boolean selectB {get; set;}
        
        public void materialWrapper()
        {
            recordId = '';
            name = '';
            product = '';
            item = '';
            quantity = 0.0;
            selectB = false;
        }
    }
}