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
TexasWheelingTexasWheeling 

Deploying an Apex Class for a Custom Button

I have a custom button that I have created which will send an email when it is clicked, however I am very new at this and can't seem to figure out how to deploy the new Apex Class to my Production environment. I have everything functioning the way I need in my Sandbox, but I'm struggling trying to create a test for this new Apex Class. Again, I am very new to this, so please forgive my ignorance on this subject.

 



The button has the behavior set to "Execute JavaScript" with the following OnClick JavaScript code:


{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
sforce.apex.execute("ReadyForTraining","SendEmailNotification", {Account:"{!Production__c.Account__c}",Production:"{!Production__c.Name}",Owner_FirstName:"{!User.FirstName}",Owner_LastName:"{!User.LastName}",Go_Live_Date:"{!Production__c.Actual_Go_Live_Date__c}",Product:"{!Production__c.Product__c}",FUSION_Tools_User:"{!Production__c.FusionTools_UserName__c}",FUSION_Tools_Pass:"{!Production__c.FusionTools_Password__c}",CallSource_User:"{!Production__c.CallSource_Username__c}",CallSource_Pass:"{!Production__c.CallSource_Password__c}",GA_User:"{!Production__c.Google_Analytics_Dealer_User_Name__c}",GA_Pass:"{!Production__c.Google_Analytics_Dealer_Password__c}",Production_Link:"{!Production__c.Link}"});
window.alert("Training email has been sent." );



The Apex Class has the following code:


global class ReadyForTraining {

WebService static void SendEmailNotification(string Account, string Production, string Owner_FirstName, string Owner_LastName, string Go_Live_Date, string Product, string FUSION_Tools_User, string FUSION_Tools_Pass, string CallSource_User, string CallSource_Pass, string GA_User, string GA_Pass, string Production_Link) {

//create a mail object to send a single email.
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

//set the email properties
mail.setToAddresses(new string[] {'bpitts@clickmotive.com'});
mail.setSenderDisplayName('Implementation Team');
mail.setSubject('' + Account + ' - Ready For Training');
mail.setHtmlBody('Support,<br /><br />' + Account + ' is ready to have their training scheduled.<br /><br />Production: ' + Production + '<br />Owner: ' + Owner_FirstName + ' ' + Owner_LastName + '<br />Go Live Date: ' + Go_Live_Date + '<br />Product(s): ' + Product + '<br /><br /><table border="1" bordercolor="black"><tr bgcolor="#D4D4D4"><td><strong>Tool</strong></td><td><strong>Username</strong></td><td><strong>Password</strong></td></tr><tr><td>FUSION Tools</td><td>' + FUSION_Tools_User + '</td><td>' + FUSION_Tools_Pass + '</td></tr><tr><td>CallSource</td><td>' + CallSource_User + '</td><td>' + CallSource_Pass + '</td></tr><tr><td>Google Analytics</td><td>' + GA_User + '</td><td>' + GA_Pass + '</td></tr></table><br /><br />Production Record Link: ' + Production_Link + ''
);

//send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail } );

}
}



How would I properly write a test for this Apex Class so that I can deploy it to my Production environment?

Best Answer chosen by Admin (Salesforce Developers) 
TexasWheelingTexasWheeling

I was able to get this corrected by changing the Apex Class API version to 24.0 and change the first line of the class' code from "@isTest" to "@isTest(seeAllData=false)". This resolved my issues.

All Answers

kiranmutturukiranmutturu

try this hope this will work for you

 

@isTest
private class TestGlobalClassEmail {

public static final String Account= 'Account of Test Email';
public static final String Production= 'Production of Test Email';
public static final String Owner_FirstName = 'Owner_FirstName of Test Email';
public static final String Owner_LastName = 'Owner_LastName of Test Email';
public static final String Go_Live_Date = 'Go_Live_Date of Test Email';
public static final String Product = 'Product of Test Email';
public static final String FUSION_Tools_User = 'FUSION_Tools_User of Test Email';
public static final String FUSION_Tools_Pass = 'FUSION_Tools_Pass of Test Email';
public static final String CallSource_User = 'CallSource_User of Test Email';
public static final String CallSource_Pass = 'CallSource_Pass of Test Email';
public static final String GA_User = 'GA_User of Test Email';
public static final String GA_Pass = 'GA_Pass of Test Email';
public static final String Production_Link = 'Production_Link of Test Email';

static testMethod void myUnitTest() {


ReadyForTraining.SendEmailNotification(Account, Production, Owner_FirstName, Owner_LastName, Go_Live_Date, Product, FUSION_Tools_User, FUSION_Tools_Pass, CallSource_User, CallSource_Pass, GA_User, GA_Pass, Production_Link);

}
}

TexasWheelingTexasWheeling

Thanks, that worked for the test of the Apex Class that I am deploying, however now I have another test class that is failing on me now. It seems to be one that was already in my organization's environment and I am not familiar with it's purpose. Here is the portion of the code that is failing for the test class; this code is the beginning of line 246 for this class that is failing.

 

    static testMethod void getCasesTest() {
        for (Case obj : [select Id from Case]) {
            delete obj;
        }

        System.runAs(createUser('test4356')){
          String queueName = 'TestQueue1';
          Group queue = new Group(Type='Queue', Name=queueName);
          insert queue;
          QueueSobject sobj = new QueueSobject(QueueId = queue.Id, SobjectType = 'Case');
          insert sobj;
            Case case1 = new Case(Subject='TestCase1', OwnerId = queue.Id, V1Association__c = true);
            Case case2 = new Case(Subject='TestCase2', OwnerId = queue.Id, V1Association__c = true);
            Case case3 = new Case(Subject='TestCase3', OwnerId = queue.Id, V1Association__c = true);
            Case case4 = new Case(Subject='TestCase4', V1Association__c = true);
            insert new Case[] {case1, case2, case3, case4};

            Case[] cases = V1CaseCollector.getCases(queue.Id, false);
            System.assertEquals(3, cases.size(), 'Incorrect numbers of cases.');

            case2.status = 'Closed';
            case3.V1Association__c = false;
            update new Case[] {case2, case3};
            Case[] newCases = V1CaseCollector.getCases(queue.Id, true);
            System.assertEquals(1, newCases.size(), 'Incorrect numbers of cases. One cases have to be closed');
            assertContains(case2, newCases, 'Incorrect data in result.');
            newCases = V1CaseCollector.getCases(queue.Id, false);
            System.assertEquals(1, newCases.size(), 'Incorrect numbers of cases. Two classes have to be not closed');
            assertContains(case1, newCases, 'Incorrect data in result.');
            newCases = V1CaseCollector.getCases(null, false);
            System.assertEquals(2, newCases.size(), 'Incorrect numbers of cases.');
        }
    }

 

 

When I try to deploy the new Apex Class change set, I am getting the following error on this other test class:

 

API Name = V1CaseCollectorTestSuite.getCasesTest()

Type = Class

Line = 248

Column = 1

Problem = Failure Message: "System.Exception: Too many DML statements: 151", Failure Stack Trace: "Class.V1CaseCollectorTestSuite.getCasesTest: line 248, column 1"

 

 

Any ideas as to why I might be getting this error, and how I can go about correcting it so that I can correctly deploy my original change set?

TexasWheelingTexasWheeling

Anyone have any thoughts on how to correct the last issue stated in the previous post?

TexasWheelingTexasWheeling

I'm not sure if this will help, but this is an excert from the debug log on the test for this Apex Class.

 

10:46:35.166 (56166029000)|EXCEPTION_THROWN|[248]|System.Exception: Too many DML statements: 151
10:46:35.166 (56166257000)|FATAL_ERROR|System.Exception: Too many DML statements: 151

Class.V1CaseCollectorTestSuite.getCasesTest: line 248, column 1
10:46:35.166 (56166276000)|FATAL_ERROR|System.Exception: Too many DML statements: 151

Class.V1CaseCollectorTestSuite.getCasesTest: line 248, column 1
10:46:25.018 (56166301000)|CUMULATIVE_LIMIT_USAGE
10:46:25.018|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 200 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 151 out of 150 ******* CLOSE TO LIMIT
  Number of DML rows: 150 out of 10000
  Number of script statements: 151 out of 200000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

 

TexasWheelingTexasWheeling

I'm still at a loss on this one. Anyone out there with any suggestions?

TexasWheelingTexasWheeling

I was able to get this corrected by changing the Apex Class API version to 24.0 and change the first line of the class' code from "@isTest" to "@isTest(seeAllData=false)". This resolved my issues.

This was selected as the best answer