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
AlexRitsonDevAlexRitsonDev 

Help writing test class

I need to write a test class for my code before Salesforce will allow us to deploy it.

 

I'm a complete newbie and would greatly appreciate your help.

 

The class is as follows (please also let me know if you can see any basic errors):

 

public with sharing class CreatecontractonOppstageclosewon {
{
Set<Id> accId = new Set<Id>();
List<Opportunity> opplist = new List<Opportunity>();
List<contract> conrtlist = new List<contract>();
for(Opportunity opp : Trigger.new){
if(opp.StageName == 'Closed Won'){
Contract con = new Contract();
if(opp.AccountId != NULL){
con .AccountId = opp.AccountId;
}
con .Status = 'Draft';
con .StartDate = System.Today();
con .ContractTerm = opp.Length_of_project_months__c;
con.X2_RESEARCH_LPA__c = opp.X2_RESEARCH_LPA__c
con.X3_RESEARCH_site_history__c) - opp.X3_RESEARCH_site_history__c)
con.X4_RESEARCH_likely_issues__c = opp.X4_RESEARCH_likely_issues__c
con.X5_RESEARCH_Stakeholders__c - opp.X5_RESEARCH_Stakeholders__c
con.X6_RESEARCH_company_other_projects__c = opp.X6_RESEARCH_company_other_projects__c
con.Client_Code__c = opp.Client_Code__c
con.Council__c = opp.Council__c
con.Date_for_1st_Public_Exhibition__c = opp.Date_for_1st_Public_Exhibition__c
con.Date_for_2nd_Public_Exhibition__c = opp.Date_for_2nd_Public_Exhibition__c
con.Date_to_Call__c = opp.Date_to_Call__c
con.Date_for_Submission__c = opp.Date_for_Submission__c
con.Details_of_Lead__c = opp.Details_of_Lead__c
con.First_Meeting_Report__c = opp.First_Meeting_Report__c
con.IPA_Pitch_Member_1__c = opp.IPA_Pitch_Member_1__c
con.IPA_Pitch_Member_2__c = opp.IPA_Pitch_Member_2__c
con.IPA_Pitch_Member_3__c = opp.IPA_Pitch_Member_3__c
con.Indigo_Email__c = opp.Indigo_Email__c
con.Planning_Committee_Date_Time__c = opp.Planning_Committee_Date_Time__c
con.Previous_contact__c = opp.Previous_contact__c
con.Referersemail__c = opp.Referersemail__c
con.Region__c = opp.Region__c
con.Relevant_Work__c = opp.Relevant_Work__c
con.Sales_Notes__c = opp.Sales_Notes__c
con.Sector__c = opp.Sector__c
con.Is_there_a_URL_where_you_found_this_lead__c = opp.Is_there_a_URL_where_you_found_this_lead__c
conrtlist .add(con );
}
}
if(conrtlist.size() > 0){
insert conrtlist ;
}
}
}

 

And the trigger is as follows:

 

trigger CreatecontractonOppstageclosewon on Opportunity (after insert, after update) {

}

 

Please, please help.  Essentially, what this will do is when we get to the Opportunity Closed/Won stage in Opportunities, a Contract will automatically be created with our custom fields fully copied over.  I know lots of other people wish they could do this, will share anything you give me and what we have created already with the world.

 

Best regards,

 

Alex.

 

 

Daniel B ProbertDaniel B Probert

i'm no expert but in theory what you need to do is something like this:

 

@isTest
public class testcreatecontractonoppstageclosewon
{ 
// create a account
Account account = new Account();
account.Name = 'test';
// include any other fields that are required
insert account;

// create contact ( if needed )
Contact contact = new Contact();
contact.FirstName = 'test';
contact.LastName = 'test';
contact.Account = contact.id;
// include any other fields that are required
insert contact

// create opportunity
Opportunity opp = new Opportunity();
opp.Name = 'Test';
opp.Account = account.id;
opp.Contact = contact.id;
opp.stagename = 'Closed Won';
// include all the field that you want copied over from the opportunity to the contract
insert opp
update opp
}

 that's how i'd approach it anyway as in theory the creation of the opportunity with the stagename = closed won should invoke the trigger.

Karthik SalesforceKarthik Salesforce

Hi Alex,

 

  Please Find the Below Class

 

  private class TestTestClass {

    static testMethod void myUnitTest()
    {
        // TO DO: implement unit test
        CreatecontractonOppstageclosewon Createcon =new CreatecontractonOppstageclosewon();
        Account acc =new Account();
        acc.Name='Test';
        insert acc;
        List<Opportunity> oppli = new List<Opportunity>();
        Opportunity opp=new Opportunity();
        opp.StageName='Closed Won';
        opp.AccountId=acc.id;
        insert opp;
        oppli.add(oppli);
        List<Contract> conrtlist = new List<Contract>();
        Contract cn=new Contract();
        cn.AccountId=acc.id;
        cn.Status = 'Draft';
        cn.StartDate = System.Today();
        cn.ContractTerm =2;
        cn.X2_RESEARCH_LPA__c = 2;
        cn.X4_RESEARCH_likely_issues__c = 3;
        cn.X6_RESEARCH_company_other_projects__c = 'test';
        cn.Client_Code__c = 'Code';
        cn.Council__c = 'Con';
        cn.Date_for_1st_Public_Exhibition__c = system.today();
        cn.Date_for_2nd_Public_Exhibition__c = system.today();
        cn.Date_to_Call__c = 'Call';
        cn.Date_for_Submission__c = system.today();
        cn.Details_of_Lead__c = 'Lead';
        cn.First_Meeting_Report__c = 'Hyd';
        cn.IPA_Pitch_Member_1__c = '5';
        cn.IPA_Pitch_Member_2__c = '7';
        cn.IPA_Pitch_Member_3__c = '10';
        cn.Indigo_Email__c = 'test@test.com'
        cn.Planning_Committee_Date_Time__c = system.today();
        cn.Previous_contact__c = 'Tesst';
        cn.Referersemail__c = 'Test@test.com'
        cn.Region__c = 'USA';
        cn.Relevant_Work__c = 'Test';
        cn.Sales_Notes__c = 'abc';
        cn.Sector__c = 'IT';
        cn.Is_there_a_URL_where_you_found_this_lead__c = 'test';
        insert cn;
        conrtlist.add(cn);
        
    }
}

AlexRitsonAlexRitson

Dear Dan and SaiTaj,

 

Firstly, thank you both for taking the time to do this for us.

 

I am now trying to understand what I need to do to get this deployed.

 

Salesforce have told me to read the following, and they will then help me on Monday.

 

http://na1.salesforce.com/help/doc/en/code_dev_console_tests_coverage.htm

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_deploying_ide.htm

 

I will update you then.

 

Really appreciate your kindness - I am on a very steep learning curve here.

 

Have a great weekend and speak soon,

 

Best regards,

 

Alex.

Daniel B ProbertDaniel B Probert

Hi Alex,

 

The deployment part is actually much easier than the coding part - i'm also on a steep learning curve at the moment :)

 

Good luck

 

Dan