You need to sign in to do that
Don't have an account?
VancouverDev
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:
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!
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!
Or
All Answers
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.
Addressable_Spend_Multiplier__c field ???
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.
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;
}
}
Employee_Lower_Bound__c
and
SIC__c
Employee_Upper_Bound__c
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"
Or
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.