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
Aryan JhaAryan Jha 

Missing return statement required return type: Account Method does not exist or incorrect signature: void add(Contact) from the type Account

public class AccountContacts {
    public static Account Accountwithnocontacts()
    {
        Account a=new Account();
        a.Name='aryanjha';
        a.Phone='901727392';
        return a;
    }
    public static Account Accountwithcontacts()
    {
        Account acc=[SELECT Id,name FROM Account WHERE Name='Voltas'];
        for(Account a:acc)
        {
            Contact con=new Contact();
            con.firstname='Lucky';
            acc.add(con);
            return acc;
        }
    }

}
PriyaPriya (Salesforce Developers) 
Hi Aryan, 

1. The error "Missing return statement required return type: Account", is occuring as you have written the 'return acc' inside the loop which should have been written after the For loop, before the method get closed. 

2. The error "Method does not exist or incorrect signature: void add(Contact) from the type Account" is occuring because in line no. 15 :- acc.add(con); , you are adding contact data to the account variable and hence the mismatch.

3. Try below code and check if it full fill your requirement :- 
 
public class AccountContacts {
    public static Account Accountwithnocontacts()
    {
        Account a=new Account();
        a.Name='aryanjha';
        a.Phone='901727392';
        return a;
    }
    public static Contact Accountwithcontacts()
    {
        Contact con=new Contact();
        List<Account> acc=[SELECT Id,name FROM Account WHERE Name='Voltas' limit 1];
        for(Account a:acc)
        {
           // Contact con=new Contact();
            con.firstname='Lucky';
            con.LastName='Guru';
            con.AccountId = acc[0].id;
            
            
        }
        return con;
    }

}
 

Please mark as Best Answer so that it can help others in the future.

Regards,

Priya Ranjan