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
Amit Karlekar 3Amit Karlekar 3 

How do I solve this error - "Variable does not exist: Contact"

public class createNewAccount {
    
    public Contact createAcc(string conName){
        
        account acc = new account();
        acc.Name = conName;
        insert acc;
        system.debug(acc);
        
        contact con = new contact contact();
        con.LastName = conName + 'Contact';
        con.AccountId = acc.Id
        insert con;
        system.debug(con);
        
        return Contact;
    }

}

this is the program in which I couldn't find the error. Can somebody please help me?
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Can you try the below code.
 
public class createNewAccount {
public Contact createAcc(string conName){
        
        account acc = new account();
        acc.Name = conName;
        insert acc;
        system.debug(acc);
        
        contact con = new contact();
        con.LastName = conName + 'Contact';
        con.AccountId = acc.Id
        insert con;
        system.debug(con);
        
        return con;
    }

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
Shams TabrezShams Tabrez
Hi Amit,

You have to remove the 'contact' in the contact object creation line.
You have missed adding the semicolon after acc.Id
You have to replace the 'Contact' with 'con' in the return statement.

Use the below code:
public class createNewAccount {
    
    public Contact createAcc(string conName){
        
        account acc = new account();
        acc.Name = conName;
        insert acc;
        system.debug(acc);
        
        contact con = new contact();
        con.AccountId = acc.Id;
        con.LastName = conName + 'Contact';
        insert con;
        system.debug(con);
        
        return con;
        
    }
    
}

If this solution is helpful, Please mark it as the best answer.

Thanks & Regards,
Shams Tabrez