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
NevDevNevDev 

How Do I Make The Opportunity Name Field Mandatory When Converting A Lead

When I convert a lead in lightning experience I need to force a user to create an Opportunity. Is there any validation that will allow me to do this?
Best Answer chosen by NevDev
parth jollyparth jolly
Hi Nevin ,
Just post the below code and trigger will gain 100%code coverage
@isTest
public class ObjectTriggerHandler_Test{
    @isTest
    public static void testDoAfterUpdate() {
           
        List<Lead> leadlist = new List<Lead> ();   
        List<Lead> oldleadlist = new List<Lead> ();
        Map<Id,Lead> oldleadMap = new Map<Id,Lead>();
        Lead ld = new Lead();
        ld.LastName = 'Test';
        ld.Company = 'Mnaual';
     
               
        insert ld;
        
        ld.LastName = 'Test1';
        
        update ld;
        
        leadlist.add(ld);
        oldleadlist.add(ld);
        
        
     ObjectTriggerHandler cont = new ObjectTriggerHandler();
     cont.OnAfterUpdate(leadlist,oldleadlist,oldleadMap);
        
    }
}

All Answers

NevDevNevDev
User-added image
Dutta SouravDutta Sourav
Hi Nevin,

I would recommend to write a Apex Trigger on Lead.
Try This:
trigger PreventLeadConvertNoOppTrigger on Lead (after update) {
    for(Lead record: Trigger.new) {
        if(record.IsConverted && !Trigger.oldMap.get(record.Id).IsConverted && record.ConvertedOpportunityId == null) {
            record.ConvertedOpportunityId.addError('Opportunity must be provided while converting leads.');
        }
    }
}

Best Regards,
Sourav
NevDevNevDev
Thanks Dutta,

I needed to refresh the sandbox so as soon as this has been completed I will test this.
NevDevNevDev
Hi Dutta,

I've tried this on my dev org and it works fine. Can you help me with the Class so that I can migrate it to the Production Org?
parth jollyparth jolly
Hi Nevin 

Are yo talking about Trigger handler class?

Thanks

Regards

Paarth Jolly
NevDevNevDev
Hi Parth,

Yes, I'm guessing it's a Trigger Handler. I need a class for the above trigger so that I can deploy it into the production org. 
parth jollyparth jolly
Hi ,

Please check handler class .I hope it helps you out .And thanks to dutta for trigger .
 
trigger PreventLeadConvertNoOppTrigger on Lead ( after update) {
       objectTriggerHandler handler = new objectHandler();

       After Update */
       if(Trigger.isUpdate && Trigger.isAfter){
               handler.OnAfterUpdate(Trigger.old, Trigger.new, Trigger.oldMap);

       }
}

handler class 
public with sharing class ObjectTriggerHandler {

 public void OnAfterUpdate(List<Lead> oldleadList , List<Lead> newleadList,Map<Id,Lead> oldleadMap ){

        for(Lead record: leadList) {
       
              if(record.IsConverted && ! oldleadMap.get(record.Id).IsConverted &&record.ConvertedOpportunityId ==          null) {
           
                     record.ConvertedOpportunityId.addError('Opportunity must be provided while converting leads.');
                     }
             }

        }
}

 Best Regards,
 Paarth Jolly
NevDevNevDev
Hi Parth,

I'm getting the following error message:

Error: Compile Error: Variable does not exist: leadList at line 5 column 26
parth jollyparth jolly
Hi Nevin ,

I am soorry just replace leadlist by newleadList in line 5
 
NevDevNevDev
Hi Parth,

I'm getting the below error message when I try to deploy. I don't understand why as the trigger shows 100% coverage.

Code Coverage Failure
Your organization's code coverage is 74%. You need at least 75% coverage to complete this deployment. Also, the following triggers have 0% code coverage. Each trigger must have at least 1% code coverage.
PreventLeadConvertNoOppTrigger
parth jollyparth jolly
Hi Nevin ,
For that you need to write a test class for the trigger .
Does the handler class got any code coverage .

Thanks
NevDevNevDev
What is the difference between a handler class and a test class? Will the Class provided above not work? 
parth jollyparth jolly
Handler Class - It is used when you have mor ethen one trigger on the same object .
Test class - its a class which is used for coverage of the code written in trigger or apex classes .
 
NevDevNevDev
Ah ok,

Are you able to help me with the Test Class?
parth jollyparth jolly
Hi Nevin ,
Just post the below code and trigger will gain 100%code coverage
@isTest
public class ObjectTriggerHandler_Test{
    @isTest
    public static void testDoAfterUpdate() {
           
        List<Lead> leadlist = new List<Lead> ();   
        List<Lead> oldleadlist = new List<Lead> ();
        Map<Id,Lead> oldleadMap = new Map<Id,Lead>();
        Lead ld = new Lead();
        ld.LastName = 'Test';
        ld.Company = 'Mnaual';
     
               
        insert ld;
        
        ld.LastName = 'Test1';
        
        update ld;
        
        leadlist.add(ld);
        oldleadlist.add(ld);
        
        
     ObjectTriggerHandler cont = new ObjectTriggerHandler();
     cont.OnAfterUpdate(leadlist,oldleadlist,oldleadMap);
        
    }
}
This was selected as the best answer