0

My Code is:

public with sharing class AccountTriggerHandler {
    public static List<Account> CreateAccounts(){
        return [SELECT Id, Name FROM Account WHERE ShippingState == BillingState];
    }
}

why there is an error

"unexpected token BillingState"?

The requirement is:

Create an Apex class named AccountTriggerHandler that contains a public static method called CreateAccounts to accept the List of Account objects For each Account record, before saving, ensure that the ShippingState field has the same value as the BillingState field

1

You need to ensure that the ShippingState field has the same value as the BillingState field before records are saved. You need to invoke it in before insert context and assign BillingState to ShippingState

Trigger:

trigger AccountTrigger on Account (before insert) {

    if(Trigger.isBefore && Trigger.isInsert)
        AccountTriggerHandler.CreateAccounts(Trigger.new);

}

AccountTriggerHandler:

public class AccountTriggerHandler {

    public static void CreateAccounts(List<Account> accounts) {
        for(Account acc : accounts) {
            acc.ShippingState = acc.BillingState;
        }
    }

}
2
0

Hi I think you are looking to query account where ShippingState and BillingState are equals. In this scenario, you can create a formula field, the return type for formula field will be boolean/Checkbox. Formula should be ShippingState = BillingState.

This formula field will return you either true or false.

You can use this formula field in your query

public with sharing class AccountTriggerHandler {
    public static List<Account> CreateAccounts(){
        return [SELECT Id, Name FROM Account WHERE CustomforMulaField__c = true];
    }
}

Hope it answer your question

2
  • Thank you for your answer, but can you tell me how to create this formula field 'CustomforMulaField__c' where where ShippingState and BillingState are equals?
    – Ryan Ren
    Sep 20 '19 at 14:48
  • N.B. query will not use an index and if set of Accounts in database is large time out; however, use of formula field for this use case works well for most orgs
    – cropredy
    Sep 20 '19 at 21:00
0

As far as this requirement is concerned, you are going a little bit off direction, the requirement says to accept a List of Accounts and not return List of Accounts and ensure that the ShippingState field has the same value as the BillingState field. So you will have to create a method

  • Which accepts list of Account records.
  • Loop through the records and assign ShippingState equal to BillingState

And regarding the things you are trying, unfortunately you cannot do field to field comparison in salesforce i.e. you cannot compare values of BillingState and ShippingState. For such kind of scenarios, you can refer salesforce help documentation i.e. Field to field comparison in an SOQL WHERE clause wherein you will have to create a boolean formula field (where formula will be ShippingState=BillingState) and query for those records where this boolean field is true.

And if you want to compare values without using a new formula field, you can do that in Apex which you cannot do in SOQL

for(Account acc: lstAccount){
    if(acc.ShippingState==acc.BillingState){
        //do your stuff here
    }
}

And just a note about the the bind variable, you should use : in the SOQL query when using bind variables. You can check this Using Apex Variables in SOQL and SOSL Queries for further help on this topic. Also, you should be using = instead of == in the SOQL query. Example of using variables in Apex

string sBillingState='SE';
[SELECT Id, Name FROM Account WHERE ShippingState =:sBillingState];
7
  • na112.salesforce.com/_ui/common/apex/debug/ApexCSIPage Thank you! But the error still exist: unexpected token 'BillingState'
    – Ryan Ren
    Sep 20 '19 at 10:50
  • -1 vote for this query will not work. You cannot compare two fields in where clause. Sep 20 '19 at 10:54
  • Well, I assumed BillingState as a variable, my initial answer was based on that assumption. Sep 20 '19 at 10:58
  • Hey, According to your assumption, I assum BillingState as a variable, so I revised my code as:--------public with sharing class AccountTriggerHandler { public static List<Account> CreateAccounts(String BillingState){ List<Account> lstAccount = new List<Account>(); String BillingState; for(Account acc: lstAccount){ if(acc.ShippingState==acc.BillingState){return lstAccount; } } }} still wrong. Can you check what is it?
    – Ryan Ren
    Sep 20 '19 at 12:35
  • If you assumed BillingState as variable, then your query should be [SELECT Id, Name FROM Account WHERE ShippingState =:BillingState]; Sep 20 '19 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.