0

I want to write a code for Extends keyword in apex using the sobject Contact.I am new to this.Can any one tell me how to run this code in vscode. When I try to run this Code, I got this error.

ERROR: Line: 1, Column: 1

classes are by default virtual ERROR running force:apex:execute: Line: 1, Column: 1

classes are by default virtual 10:44:02.402 sfdx force:apex:execute --apexcodefile c:\Users\Vaasini\Documents\Sample Project.sfdx\tools\tempApex.input ended with exit code 1

My code: **

public virtual  class fetchContact {
    public virtual void getContact(){
        Contact contact1 = [Select Id,Name FROM Contact where (LastName = 'Yona')];
        system.debug(contact1);
    }
}
public with sharing class contactUpdation extends fetchContact{
    public override void getContact(){
        Contact contact1 = [ Select Phone FROM Contact where (LastName = 'Yona')];     
        contact1.Phone='999999999';
        update contact1;
        system.debug(contact1);
    }
}
public with sharing class createContact extends fetchContact{
    Contact contact2;
    public override void getcontact(){
        Contact contact1 = [Select Id,FirstName,LastName,Phone from Contact where Name = 'Yona'];
        Contact contact2 = new Contact();
        contact2.FirstName = 'Lydia';
        contact2.LastName = Contact1.LastName;
        contact2.Phone = '9876543210';
        contact2.AccountId =('0012x000005ydxwAAA') ;
        contact2.Description = ('Created using existing contact but with differet Phone number and Account');
        Insert contact2;
        System.debug(contact2);
    }
} 
fetchContact objfetch, objcreate, objupdate; 
objfetch = new fetchContact(); 
objfetch.getContact();
objupdate = new contactUpdation(); 
objupdate.getContact(); objcreate = new createContact();
objcreate.getContact();

**

0

As the error says, the class is already virtual, so just remove the virtual keyword on the class:

public class fetchContact {
    public virtual void getContact(){
        Contact contact1 = [Select Id,Name FROM Contact where (LastName = 'Yona')];
        system.debug(contact1);
    }
}

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.