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
Mohd NabeelMohd Nabeel 

Line: 1, Column: 24 Unexpected token '('.

HI, 
I am trying to call my method in the anonymous window but it is showing me (Line: 1, Column: 24 Unexpected token '('.) this error. The class is static so it is not necessary to make an object or instance of that class..
AccountContact.onBefore(Account Name='asdf');
This is my code i am inserting an account with the account name
Best Answer chosen by Mohd Nabeel
Soyab HussainSoyab Hussain
Hi Mohd Nabeel,

You are calling the method with incorrect parameter.
Use this code this will help you to call the method from anonymous window.
List<Account> accList = new List<Account>();
accList.add(new Account(Name = 'Mohd Nabeel'));
insert accList;
AccountContact.onBefore(accList);

If you found it useful please appreciate my efforts and mark it as the best answer

Regards,
Soyab

All Answers

Soyab HussainSoyab Hussain
Hi Mohd Nabeel,

Can you please put your static class code, so I can help you.

Regards,
Soyab
Mohd NabeelMohd Nabeel
public class AccountContact{
    public static void onBefore(List<Account> acctList){
        List<Contact> conList = new List<Contact>();
        for(Account acc: acctList){
            Contact con = new Contact(AccountId = acc.ID);
            List<String> nameStr = acc.Name.split(' ');
            if(nameStr.size()>0)
                con.LastName = nameStr[0];
            if(nameStr.size()>1)
                con.FirstName = nameStr[1];
            con.FirstName = 'Info';
            con.LastName = 'Default';
            con.Email = 'info@websitedomain.tld';
            acc.OnlyDefaultContact__c = TRUE;
            conList.add(con);
        }
        insert conList;
    }
    public static void updateCheckBox(List<Contact> contList){
        Set<id> accountIds = new Set<id>();
        for(Contact con: contList){
            accountIds.add(con.Accountid);
        }
        List<Account> updateAccount = new List<Account>();
        for(AggregateResult ar: [Select count(id), AccountId from Contact where AccountId IN
                                 : accountids group by AccountId having count(id)>1]){
            updateAccount.add(new Account(id = (id)ar.get('Account Id'),OnlyDefaultContact__c
                                = FALSE));
        }
    }
}
Soyab HussainSoyab Hussain
Hi Mohd Nabeel,

You are calling the method with incorrect parameter.
Use this code this will help you to call the method from anonymous window.
List<Account> accList = new List<Account>();
accList.add(new Account(Name = 'Mohd Nabeel'));
insert accList;
AccountContact.onBefore(accList);

If you found it useful please appreciate my efforts and mark it as the best answer

Regards,
Soyab
This was selected as the best answer
Mohd NabeelMohd Nabeel
Thanku Soyab Hussain for responding quickly.. Appreciate your efforts.. But there is this line which i am not getting 
acc.OnlyDefaultContact__c = TRUE;  .. How can we access this fileld.. When i am runnig my code the conatct is created but the checkbox doesnt get checked. But when i am calling like this con.OnlyDefaultContact__c = TRUE;  which is i think is incorrect the it trown an error variable does not exist.. 
Soyab HussainSoyab Hussain
Make sure that this field is actually visible to your user via Field Level Security.
  1. Click the field name in your Setup UI to view the field definition detail.
  2. On this detail view, there will be a Set Field-Level Security button.
  3. Make sure Visible is selected for your Profile.
Mohd NabeelMohd Nabeel
Thanku.