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
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
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'); }
Let us know if this will help you
All Answers
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();
}
}
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
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>
<apex:inputText value="{!LastName}"/><br/><br/>
<apex:outputLabel >Company</apex:outputLabel>
<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>
Let us know if this will help you