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
Alex Valavanis 13Alex Valavanis 13 

apex class assistance

Could you please tell me where the error is here? Is it because there is a blank space somewhere?

The error i am receiving is "Error: Compile Error: Invalid constructor name: MyPageExtension at line 1 column 93"
 
public with sharing class OwnerChangeExtension{public List<Contact>contacts{get;set;}public MyPageExtension
(ApexPages.StandardSetController controller){controller.addFields(new List<String>{'Name','FirstName',
'AccountId', 'Account.Name', 'OwnerId','Account.Owner.Name'});
       contacts=(List<Contact>)controller.getSelected();
    }
    public PageReference save() {
        try{
            Map<Id, Account> accountById= new Map<Id, Account>();
            for(Contact con : contacts){
                if(con.AccountId!=null){
                    if(!accountById.containsKey(con.AccountId)){
                        Account acc = new Account(Id=con.AccountId,
                                              OwnerId=con.Account.OwnerId,
                                              Name=con.Account.Name);
                        accountById.put(con.AccountId, acc);
                    }
                }
            }
            update contacts;
            update accountById.values();
            return new ApexPages.Action('{!List}').invoke();
        }catch(Exception e){
            ApexPages.addMessage(newApexPages.Message(ApexPages.Severity.Error, 'Error occured while updating below records.'+ e.getMessage()));
         return null;
        }
    }

    public PageReference cancel() {
        return new ApexPages.Action('{!List}').invoke();
    }
}

 
Abdul KhatriAbdul Khatri
The Constructor name must be same to the class name. Here is the issue you have. I have fixed it and you can use the below code

User-added image
 
public with sharing class OwnerChangeExtension{

    public List<Contact>contacts{get;set;}
    
    public OwnerChangeExtension(ApexPages.StandardSetController controller){
    
        controller.addFields(new List<String>{'Name','FirstName','AccountId', 'Account.Name', 'OwnerId','Account.Owner.Name'});
        contacts=(List<Contact>)controller.getSelected();
    }
    
    public PageReference save() {
        try{
            Map<Id, Account> accountById= new Map<Id, Account>();
            for(Contact con : contacts)
            {
                if(con.AccountId!=null)
                {
                    if(!accountById.containsKey(con.AccountId))
                    {
                        Account acc = new Account(Id=con.AccountId,
                                              OwnerId=con.Account.OwnerId,
                                              Name=con.Account.Name);
                        accountById.put(con.AccountId, acc);
                    }
                }
            }
            update contacts;
            update accountById.values();
            return new ApexPages.Action('{!List}').invoke();
        }catch(Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Error occured while updating below records.'+ e.getMessage()));
         return null;
        }
    }

    public PageReference cancel() {
        return new ApexPages.Action('{!List}').invoke();
    }
}

 
Abdul KhatriAbdul Khatri
Hi Alex,

Was my solution helped?