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
prime kiprime ki 

i don't have any test class called DML trigger in my org but while running another test class i am getting an error called 'line 7, column 14: Variable does not exist: LastName' What should i need to do to achieve this

Best Answer chosen by prime ki
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest
public class Task6Test {
    
    public static testmethod void Task(){   
	
		Lead L = new Lead();
		L.LastName = 'Arudra';
		L.Company = 'Prime';
		L.LeadSource  = 'web';
		L.Status = 'web';
		insert L;

		Task6 T = new Task6(new ApexPages.standardController(L));
		T.LastName='Test';
		T.Company='Test';
		T.saveandNew();
    } 
	
    public static testmethod void Task1(){   
	
		Lead L = new Lead();
		L.LastName = 'Arudra';
		L.Company = 'Prime';
		L.LeadSource  = 'web';
		L.Status = 'web';
		insert L;

		Task6 T = new Task6(new ApexPages.standardController(L));
		T.LastName='Test';
		T.Company='Test';
		T.Save();
    } 
}

Let us know if this will help you

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Can you please share your code ?
prime kiprime ki
@isTest
public class Task6Test {
    
    public static testmethod void Task(){   
       
        Lead L = new Lead();
        L.LastName = 'Arudra';
        L.Company = 'Prime';
        L.LeadSource  = 'web';
        L.Status = 'web';
         insert L;
        
         Task6 T = new Task6(new ApexPages.standardController(new Lead()));
         T.Save();
        T.saveandNew();

    } 
      
        
           
}
Amit Chaudhary 8Amit Chaudhary 8
Did you created any Apex class with name of Lead ? If yes then please rename the same and try again above code.
prime kiprime ki
No i don't have any Apex class called Lead i have a Class called Lead1
Amit Chaudhary 8Amit Chaudhary 8
Any Trigger with Lead name ?
prime kiprime ki
No but the thing is it was showing an error on Dml trigger i have a trigger called Dml trigger on contact object just now i have deactivated that trigger now it'sworking fiine but test class doesn't passed and showing an error "System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [LastName, Company]: [LastName, Company]" but i have inserted a record with all required fileds in lead,which i provided in the above code 
Amit Chaudhary 8Amit Chaudhary 8
@isTest
public class Task6Test {
    
    public static testmethod void Task(){   
       
        Lead L = new Lead();
        L.LastName = 'Arudra';
        L.Company = 'Prime';
        L.LeadSource  = 'web';
        L.Status = 'web';
         insert L;
        
         Task6 T = new Task6(new ApexPages.standardController(L));
         T.Save();
        T.saveandNew();

    } 
      
        
           
}

Update your test class like below
 
prime kiprime ki
same error Amit do u want Class and VF page of this test class
Amit Chaudhary 8Amit Chaudhary 8
yes please share
prime kiprime ki
This is Apex Class and VF page

public class Task6 {
    
    public String LastName{set;get;}
    public String Company{set;get;}
    private  ApexPages.StandardController stand {set;get;}
    public Task6(ApexPages.StandardController controller){
        this.stand= controller;
        List<Lead> L = new List<Lead>();
}
    public pagereference Save(){
        Lead L = new Lead();
        L.LastName = LastName;
        L.Company = Company;
        insert L;
        
        return new pagereference('/'+L.Id);
    }
    
    public pagereference saveandNew(){
        Save();
        return new pagereference('https://ap4.salesforce.com/00Q/e?retURL=%2F00Q%2Fo');
    }

}



<apex:page standardController="Lead" extensions="Task6">
    <apex:form >
        <apex:pageBlock title="Implementing Custom save logic using StandardController ">
            <apex:outputLabel >LastName</apex:outputLabel>&nbsp;&nbsp;
            <apex:inputText value="{!LastName}"/><br/><br/>
            <apex:outputLabel >Company</apex:outputLabel>&nbsp;&nbsp;
            <apex:inputText value="{!Company}"/>
            <apex:pageBlockButtons location="bottom">
            <apex:commandButton value="Save" action="{!Save}"/>
            <apex:commandButton value="Save&New" action="{!SaveandNew}"/>
            </apex:pageBlockButtons>
            </apex:pageBlock>
    </apex:form>
</apex:page>
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest
public class Task6Test {
    
    public static testmethod void Task(){   
	
		Lead L = new Lead();
		L.LastName = 'Arudra';
		L.Company = 'Prime';
		L.LeadSource  = 'web';
		L.Status = 'web';
		insert L;

		Task6 T = new Task6(new ApexPages.standardController(L));
		T.LastName='Test';
		T.Company='Test';
		T.saveandNew();
    } 
	
    public static testmethod void Task1(){   
	
		Lead L = new Lead();
		L.LastName = 'Arudra';
		L.Company = 'Prime';
		L.LeadSource  = 'web';
		L.Status = 'web';
		insert L;

		Task6 T = new Task6(new ApexPages.standardController(L));
		T.LastName='Test';
		T.Company='Test';
		T.Save();
    } 
}

Let us know if this will help you
This was selected as the best answer
prime kiprime ki
Thank you Amit it's working fine i get 100% code coverage, Sorry for the late reply.