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
ShunnShunn 

Apex class deployment without trigger or test class?

Hi All,

I am no developer. I have this test class in sandbox. I tried to deploy it to production and I am hitting a code coverage error(Code Coverage Failure. Your code coverage is 0%. You need at least 75% coverage to complete this deployment.
PBBRunAssignmentRules)


. see the class here:

// Digital Pi - This Apex class is used to reassign a lead using standard assignment rules
public with sharing class PBBRunAssignmentRules {
@InvocableMethod(label='Re-run Assignment Rules on Lead')
public static void ReRunAssignmentRules(list<string> recordIds) {

set<id> LeadIds = new set<id>();

for (string recordId:recordIds){
id rid=id.valueOf(recordId);
Schema.SObjectType sot= rid.getSObjectType();
if (sot == Lead.sObjectType){
LeadIds.add(rid);
}
}

if (!LeadIds.isempty()){
//ID jobID = System.enqueueJob(new PBBLeadReassignQueueable(LeadIds));
if (system.isFuture()) {
system.debug('running in future already; exiting!');
return;
} else {
system.debug('starting future call');
futureLeadReassign(LeadIds);
}
}
}

@future
public static void futureLeadReassign(Set<ID> ReassignSet) {

system.debug('in the future, doing lead reassignment');
List<Lead> UpdList = [SELECT Id FROM Lead WHERE Id IN: ReassignSet];

for (Lead l:UpdList) {
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
// use leadAssignment rules when updating
l.setOptions(dmo);
}
system.debug(UpdList);
update(UpdList);
}
}


What do I need to do? Do I need a trigger or test class?  Any help with writing this test class please?
Best Answer chosen by Shunn
Raj VakatiRaj Vakati
Use this Test class
@isTest
private class PBBRunAssignmentRulesTest {

	static testMethod void processLead(){
		
		Test.startTest();
		List<Lead> lstLead =   new List<Lead>{
                          new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open'),
                          new Lead(Company = 'Nike', LastName = 'John', Status = 'Open'),
                          new Lead(Company = 'Miles', LastName = 'Davis', Status = 'Open'),
                          new Lead(Company = 'Reebok', LastName = 'Hillen', Status = 'Open'),
                          new Lead(Company = 'Addidas', LastName = 'Shrin', Status = 'Open')
                         };  
        insert lstLead;
        
		Map<Id,Lead> les = new Map<Id,Lead>([Select Id From Lead]);
		List<string> ids = new List<String>();
		ids.addAll(les.keySet());
		
		PBBRunAssignmentRules.ReRunAssignmentRules(ids);
		PBBRunAssignmentRules.futureLeadReassign(les.keySet());
	}
}

 

All Answers

KrishnaAvvaKrishnaAvva
Hi Shunn,

You would need a Apex Test class to deploy this in production. It should also cover 75% of the APEX Class to make a succesful deployment.
Please get started here : https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro

Regards,
Krishna Avva
Raj VakatiRaj Vakati
If you have a test class in your sandbox 
  1. Include the test class also in changeset along with class and migrate it 
  2. Make sure your test class is running successfully in your sandbox without any errors
ShunnShunn
Krishna and Raj, thanks. Unfortunately, I dont have a test class! I dont know how to write apex classes and triggers. I am not a developer. The code above is from a blog. it works fine in a sandbox and I am trying to deploy it to production. Any help?
Rishab TyagiRishab Tyagi
Hello Shunn,

In order to deploy the code, you need an APEX class. There is no workaround for this step. You can write it on your own or you may hire a developer on an hourly basis for the same. I believe that if it's a single class that needs testing then there won't be too much time required for this task.
Raj VakatiRaj Vakati
Use this Test class
@isTest
private class PBBRunAssignmentRulesTest {

	static testMethod void processLead(){
		
		Test.startTest();
		List<Lead> lstLead =   new List<Lead>{
                          new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open'),
                          new Lead(Company = 'Nike', LastName = 'John', Status = 'Open'),
                          new Lead(Company = 'Miles', LastName = 'Davis', Status = 'Open'),
                          new Lead(Company = 'Reebok', LastName = 'Hillen', Status = 'Open'),
                          new Lead(Company = 'Addidas', LastName = 'Shrin', Status = 'Open')
                         };  
        insert lstLead;
        
		Map<Id,Lead> les = new Map<Id,Lead>([Select Id From Lead]);
		List<string> ids = new List<String>();
		ids.addAll(les.keySet());
		
		PBBRunAssignmentRules.ReRunAssignmentRules(ids);
		PBBRunAssignmentRules.futureLeadReassign(les.keySet());
	}
}

 
This was selected as the best answer
ShunnShunn
@Raj Vakati! Many Thanks for this! it is splendid! Thanks everyone for your help and guidance!
Simran Saluja 14Simran Saluja 14
I have a trigger in sandbox but to reply it I need test class
can someone help me with test class?
 
Trigger->

trigger sendServiceReport on ServiceReport (after insert) {
    string emailRecipient;
    string jobNumber;
    for(ServiceReport sr : trigger.new){
     
        list<serviceappointment> service = new list<serviceappointment>([select id, appointmentnumber, contact.email from serviceappointment where id =:sr.ParentId]);
        if(!service.isEmpty()){
            emailRecipient = service[0].contact.email;
            jobNumber = service[0].appointmentnumber;
        }
        
        List<String> sendTo = new List<String>();
        sendTo.add(emailRecipient);
        string reportName = sr.ServiceReportNumber;
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(sendTo);
        mail.setSubject('Your Service Report');
        String body = 'Congratulations! A Service Report has been generated for '+jobNumber+ '. Please find it attached to this email.';
        mail.setHtmlBody(body);
        List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>{};
            List<ContentVersion> documents = new List<ContentVersion>{};
                //get service report as a file (ContentVersion record)
                documents.addAll([
                    SELECT Id, Title, FileType, VersionData, isLatest, ContentDocumentId
                    FROM ContentVersion
                    WHERE isLatest = true AND title like : reportName limit 1]);
        
        for (ContentVersion document: documents) {
            Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
            attachment.setBody(document.VersionData);
            attachment.setFileName(document.Title);
            attachment.setContentType('application/pdf');
            attachments.add(attachment);
        }
        
        mail.setFileAttachments(attachments);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    }
}

 
Simran Saluja 14Simran Saluja 14
To deploy I need to create test class?