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
VancouverDevVancouverDev 

Getting "Unexpected Token" in Apex class post-declaration

Hi everyone,

Hoping someone can have a look and help me identify my problem. I've been working on the code in a fragmented, few-minutes-at-a-time way and I'm kind of stuck.

Here's the class:
global class AddressableSpend  {

public static Decimal CalculateAddressableSpend(Integer employees, Integer accountSIC, Decimal revenue){
	//Create a variable to store the results of our query, Query the Addressable Spend metadata table to get the appropriate records.
	Decimal multiplierResults = new Decimal
	multiplierResults = [SELECT Addressable_Spend_Multiplier__c FROM MultiplierMetadata__mdt  WHERE (employees>=Employee_Lower_Bound__c && employees <= Employee_Upper_Bound__c && accountSIC == SIC__c) LIMIT 1]
	//Use the Addressable Spend Multiplier from the retrieved record to multiply the revenue
	Decimal finalSpendAmount = multiplierResults * revenue;
	return finalSpendAmount;
	}
}

I'm trying to call this class from a trigger, which will pass in the three variables employees, accountSIC, and revenue. The class is supposed to return the multiplied revenue value (here labeled as finalSpendAmount) to the trigger to update the Account.

I'm getting four errors on the above code. Two are "expecting ; but was )" and "expecting ; but was ]" so I'm putting them aside for the moment. The ones I'm more concerned about are "Unexpected token 'multiplierResults'" and "Unexpected token 'Employee_Lower_Bound__c'" I've declared the multiplierResults variable, and I've encapsulated the query in [], so I'm not sure why those two are throwing errors.

If anyone can point me to the probably-quite-simple fix I'd appreciate it. Thanks for the help!
Best Answer chosen by VancouverDev
Raj VakatiRaj Vakati
Try this code
 
global class AddressableSpend  {
    
    public static Decimal CalculateAddressableSpend(Integer employees, String accountSIC, Decimal revenue){
        Decimal multiplierResults = [SELECT Addressable_Spend_Multiplier__c FROM MultiplierMetadata__mdt 
                                     where SIC__c=:accountSIC AND Employee_Lower_Bound__c>=:employees AND  Employee_Upper_Bound__c<=:employees LIMIT 1].Addressable_Spend_Multiplier__c;
   
        Decimal finalSpendAmount = multiplierResults * revenue;
        return finalSpendAmount;
    }
}



Or 

 
global class AddressableSpend  {
    
    public static Decimal CalculateAddressableSpend(Integer employees, Integer accountSIC, Decimal revenue){
		
		String val = String.valueOf(accountSIC);
        Decimal multiplierResults = [SELECT Addressable_Spend_Multiplier__c FROM MultiplierMetadata__mdt 
                                     where SIC__c=:val AND Employee_Lower_Bound__c>=:employees AND  Employee_Upper_Bound__c<=:employees LIMIT 1].Addressable_Spend_Multiplier__c;
   
        Decimal finalSpendAmount = multiplierResults * revenue;
        return finalSpendAmount;
    }
}

 

All Answers

Raj VakatiRaj Vakati
Try this
 
global class AddressableSpend  {

public static Decimal CalculateAddressableSpend(Integer employees, Integer accountSIC, Decimal revenue){
	//Create a variable to store the results of our query, Query the Addressable Spend metadata table to get the appropriate records.
	
Decimal multiplierResults = [SELECT Addressable_Spend_Multiplier__c FROM MultiplierMetadata__mdt  WHERE (Employee_Lower_Bound__c>=:employees &&  Employee_Upper_Bound__c<=:employees && SIC__c=:accountSIC) LIMIT 1].Addressable_Spend_Multiplier__c;
	//Use the Addressable Spend Multiplier from the retrieved record to multiply the revenue
	Decimal finalSpendAmount = multiplierResults * revenue;
	return finalSpendAmount;
	}
}

 
VancouverDevVancouverDev
Unfortunately that fixes the unexpected token issues but introduces the following errors:
Unexpected token ':'  
Unexpected token ':'
Expecting ';' but was: ')'
Expecting ';' but was: ']'
Expression cannot be assigned
Variable does not exist: Addressable_Spend_Multiplier__c

Does simply changing it so it initializes the variable in the same line it runs the query fix the unexpected token error? I had it like that before but it seemed to continue throwing that error.

I'm also a little puzzled by the Addressable_Spend_Multiplier__c not existing, as that field does exist on the metadata object I'm querying.
Raj VakatiRaj Vakati
Can you give me the API names of the 

Addressable_Spend_Multiplier__c  field ???


 
VancouverDevVancouverDev
Sorry, I'm confused. That IS the API name. It was the name assigned to it by the system when I created the field "Addressable Spend Multiplier" on the metadata object.
Raj VakatiRaj Vakati
Try this pls
 
global class AddressableSpend  {
    
    public static Decimal CalculateAddressableSpend(Integer employees, Integer accountSIC, Decimal revenue){
        Decimal multiplierResults = [SELECT Addressable_Spend_Multiplier__c FROM MultiplierMetadata__mdt 
                                     where SIC__c=:accountSIC AND Employee_Lower_Bound__c>=:employees AND  Employee_Upper_Bound__c<=:employees LIMIT 1].Addressable_Spend_Multiplier__c;
   
        Decimal finalSpendAmount = multiplierResults * revenue;
        return finalSpendAmount;
    }
}

 
VancouverDevVancouverDev
@Raj Vakati

Almost there! Just throws a single error now "Invalid bind expression type of Integer for column of type String" which is showing up on line 4

Now I'm puzzled by that because the Addressable_Spend_Multiplier__c field is a Number field, so shouldn't attempting to store it as a Decimal properly be translating arbitrary precision number to arbitrary precision number? Or does the SOQL query automatically return a number value as a string instead of a number? Most of my SOQL work has been directly with Data Loader instead of through Apex, maybe there's an extra piece missing I'm not seeing.
SarvaniSarvani
Try this,

global class AddressableSpend  {

public static Decimal CalculateAddressableSpend(Integer employees, Integer accountSIC, Decimal revenue){
    
    MultiplierMetadata__mdt  multiplierResults = [SELECT Addressable_Spend_Multiplier__c,
                                                                Employee_Lower_Bound__c,
                                                                Employee_Upper_Bound__c,
                                                                SIC__c
                                                               FROM MultiplierMetadata__mdt  
                                                                WHERE (
(Employee_Lower_Bound__c>=:employees) AND (Employee_Upper_Bound__c<=:employees ) AND ( SIC__c=:accountSIC)
) LIMIT 1];
    
    
    //Use the Addressable Spend Multiplier from the retrieved record to multiply the revenue
    Decimal finalSpendAmount = (multiplierResults.Addressable_Spend_Multiplier__c) * revenue;
    return finalSpendAmount;
    }
}
Raj VakatiRaj Vakati
What is the data type of the 

Employee_Lower_Bound__c

and 

SIC__c

Employee_Upper_Bound__c
VancouverDevVancouverDev
Employee fields are both Number fields
SIC__c is a text field, and the Sic field on the Account object is also a text field
I attempted converting both in the query to Strings but it then complains "Method does not exist or incorrect signature: void String(Integer) from the type AddressableSpend"
Raj VakatiRaj Vakati
Try this code
 
global class AddressableSpend  {
    
    public static Decimal CalculateAddressableSpend(Integer employees, String accountSIC, Decimal revenue){
        Decimal multiplierResults = [SELECT Addressable_Spend_Multiplier__c FROM MultiplierMetadata__mdt 
                                     where SIC__c=:accountSIC AND Employee_Lower_Bound__c>=:employees AND  Employee_Upper_Bound__c<=:employees LIMIT 1].Addressable_Spend_Multiplier__c;
   
        Decimal finalSpendAmount = multiplierResults * revenue;
        return finalSpendAmount;
    }
}



Or 

 
global class AddressableSpend  {
    
    public static Decimal CalculateAddressableSpend(Integer employees, Integer accountSIC, Decimal revenue){
		
		String val = String.valueOf(accountSIC);
        Decimal multiplierResults = [SELECT Addressable_Spend_Multiplier__c FROM MultiplierMetadata__mdt 
                                     where SIC__c=:val AND Employee_Lower_Bound__c>=:employees AND  Employee_Upper_Bound__c<=:employees LIMIT 1].Addressable_Spend_Multiplier__c;
   
        Decimal finalSpendAmount = multiplierResults * revenue;
        return finalSpendAmount;
    }
}

 
This was selected as the best answer
VancouverDevVancouverDev
Hi Raj,

That was the last step needed! The second code option saved with no errors.

Thanks for all your help! I've marked your code as a Best Answer so anyone looking in future can find it.