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
sudhirn@merunetworks.comsudhirn@merunetworks.com 

test class for lead conversion on opportunity update

Hi, 

 I wrote a trigger on lead which will fire when lead is converted and opportunity contact gets updated.  trigger is working fine I need to create a test class for deployment
 
trigger updateopportunitycontact on Lead (before update) 
{
for(Lead lead:System.Trigger.new) 
{ 

  if (lead.IsConverted) 
 { 
   Contact con = [SELECT Id FROM Contact WHERE Contact.Id = :lead.ConvertedContactId];
   Opportunity opp = [SELECT Contact__c from Opportunity where opportunity.id = :lead.ConvertedOpportunityId];
   
   opp.Contact__c = lead.ConvertedContactId;
   
   update opp;
  }
  
}

}

Below is the trigger which i wrote and i dont see any code coverage please suggest me how to make the changes. 
 
@isTest(SeeAllData = true)
private class   auto_lead_assignment {

    public static testmethod void testautolead()
    {
    
test.startTest();

Account A;

  A =  [ select Id,Email_Domain__c,OwnerId,name
                  from Account
                  where  
                  BillingState = 'CA' and 
                  Email_Domain__c = 'walmartlabs.com' limit 1 ];
        
        
    
Lead l = new Lead(lastname='sudhir', company=a.name,Account__c = a.id,run_assn_rules_yn__c = true);
insert l;
 
Lead ld=[Select company from Lead where id = :l.Id];

update ld; 
 
 
  test.stopTest(); 
 
}

}

Thanks
Sudhir


 
Best Answer chosen by sudhirn@merunetworks.com
RAM AnisettiRAM Anisetti
try this one..
 
@isTest(SeeAllData = true)
private class   auto_lead_assignment {

public static testmethod void testautolead()
{
test.startTest();
Lead testLead = new Lead(
   LastName='Ram',
   Company = 'Ram inc'
  );
 insert testLead;


Database.LeadConvert lc = new database.LeadConvert();

lc.setLeadId(testLead.id);

 
LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];

lc.setConvertedStatus(convertStatus.MasterLabel);

Database.LeadConvertResult lcr = Database.convertLead(lc);

System.assert(lcr.isSuccess());

 
test.stopTest(); 
 
}

}

 

All Answers

sudhirn@merunetworks.comsudhirn@merunetworks.com
Sorry there was a wrog test class copied in the post please find the correct test class 
 
@isTest(SeeAllData = true)
private class   Test_Update_Converted_Opportunity  {

    public static testmethod void testupdateopp()
    {
    
    Account acc = new Account(Name = 'Test Account', CurrencyIsoCode = 'USD', Email_Domain__c ='sudhir.com' );
    insert acc;
    
    Contact con = new Contact(firstname = 'sudhir', lastname = 'samarth',AccountId = acc.Id,LeadSource='Community');
    insert con;

    Opportunity oppRec = new Opportunity (Name = 'Test Opportunity', AccountId = acc.Id, Type= 'New Customer',
                                          CloseDate = system.today(), ForecastCategoryName='Omitted', LeadSource='Community',
                                          StageName='Booked');
    insert oppRec;
    
    Test.startTest();

    Opportunity opp1a = [SELECT Id,Contact__c
                        FROM Opportunity
                        WHERE Id =: oppRec.Id];
  
   opp1a.Contact__c = con.id;
   
   update opp1a;
  
   test.stopTest(); 
    
  }   
}

Thanks
Sudhir
RAM AnisettiRAM Anisetti
try this one..
 
@isTest(SeeAllData = true)
private class   auto_lead_assignment {

public static testmethod void testautolead()
{
test.startTest();
Lead testLead = new Lead(
   LastName='Ram',
   Company = 'Ram inc'
  );
 insert testLead;


Database.LeadConvert lc = new database.LeadConvert();

lc.setLeadId(testLead.id);

 
LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];

lc.setConvertedStatus(convertStatus.MasterLabel);

Database.LeadConvertResult lcr = Database.convertLead(lc);

System.assert(lcr.isSuccess());

 
test.stopTest(); 
 
}

}

 
This was selected as the best answer
sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi Ram, 

  Thanks for you reply its working perfect I have a confict with this class below class is giving me the error while deploying


System.DmlException: Update failed. First exception on row 0 with id 00Q180000028DDHEA2; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: []

Class.ClassRunLeadAssignmentRules.assign: line 20, column 1

Please suggest me how to fix this issue 
public class ClassRunLeadAssignmentRules {
 public static Boolean assignAlreadyCalled=FALSE;

    public static boolean assignAlreadyCalled(){
        return assignAlreadyCalled;
    }

  @future
  public static void assign(List<Id> lIds){
    assignAlreadyCalled=TRUE;
    List<Lead> leads=[SELECT Id FROM Lead WHERE Id IN: lIds];
    For (lead l:leads){
      Database.DMLOptions dmo = new Database.DMLOptions();
      dmo.assignmentRuleHeader.useDefaultRule= true;
      system.debug('dmo.EmailHeader.triggerUserEmail ' +dmo.EmailHeader.triggerUserEmail);
      dmo.EmailHeader.triggerUserEmail = true;
       l.setOptions(dmo);

    }
    update(leads);
  }}

Thanks
Sudhir
RAM AnisettiRAM Anisetti
Try this one..if it is works make it as best answer
public class ClassRunLeadAssignmentRules {
 public static Boolean assignAlreadyCalled=FALSE;

    public static boolean assignAlreadyCalled(){
        return assignAlreadyCalled;
    }

  @future
  public static void assign(List<Id> lIds){
    assignAlreadyCalled=TRUE;
	/*=====================================Change the below line======add IsConverted=false **condition==================================*/
    List<Lead> leads=[SELECT Id FROM Lead WHERE Id IN: lIds and IsConverted=false];
    For (lead l:leads){
      Database.DMLOptions dmo = new Database.DMLOptions();
      dmo.assignmentRuleHeader.useDefaultRule= true;
      system.debug('dmo.EmailHeader.triggerUserEmail ' +dmo.EmailHeader.triggerUserEmail);
      dmo.EmailHeader.triggerUserEmail = true;
       l.setOptions(dmo);

    }
    update(leads);
  }}

 
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks for your reply this class which you modified now actually runs a autoassignment rule adding this condition will it affect the assignment rule 

Thanks
Sudhir
RAM AnisettiRAM Anisetti
yes it will work for assignment rule also.
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thanks Ram Just a suggestion is there way we can modify the class you wrote instead of modifying the other class just want to know if you can suggest alternative way 
 
@isTest(SeeAllData = true)
private class   auto_lead_assignment {

public static testmethod void testautolead()
{
test.startTest();
Lead testLead = new Lead(
   LastName='Ram',
   Company = 'Ram inc'
  );
 insert testLead;


Database.LeadConvert lc = new database.LeadConvert();

lc.setLeadId(testLead.id);

 
LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];

lc.setConvertedStatus(convertStatus.MasterLabel);

Database.LeadConvertResult lcr = Database.convertLead(lc);

System.assert(lcr.isSuccess());

 
test.stopTest(); 
 
}

}

Thanks
Sudhir
RAM AnisettiRAM Anisetti
this is the best...
sudhirn@merunetworks.comsudhirn@merunetworks.com
Thank you Very Much Ram Give I am testing all the scenarios Apprecialte your help 
sudhirn@merunetworks.comsudhirn@merunetworks.com
Hi Rama,

  I am getting apex exception from below class looks like some for the converted lead when they run the lead assignment from below class its taking to exception can you please suggest how to fix this issue. 
 
public class ClassRunLeadAssignmentRules {
 public static Boolean assignAlreadyCalled=FALSE;

    public static boolean assignAlreadyCalled(){
        return assignAlreadyCalled;
    }

  @future
  public static void assign(List<Id> lIds){
    assignAlreadyCalled=TRUE;
	/*=====================================Change the below line======add IsConverted=false **condition==================================*/
    List<Lead> leads=[SELECT Id FROM Lead WHERE Id IN: lIds and IsConverted=false];
    For (lead l:leads){
      Database.DMLOptions dmo = new Database.DMLOptions();
      dmo.assignmentRuleHeader.useDefaultRule= true;
      system.debug('dmo.EmailHeader.triggerUserEmail ' +dmo.EmailHeader.triggerUserEmail);
      dmo.EmailHeader.triggerUserEmail = true;
       l.setOptions(dmo);

    }
    update(leads);
  }}

Thanks
Sudhir