• adambreen.ax1546
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 4
    Replies

I have this (working) trigger:

 

trigger convertLeadToImplementationOpportunity on Lead (after update) {
    Set<Lead> leadsToConvert = new Set<Lead>();
    for (Lead lead : Trigger.new){
    	leadsToConvert.add(lead);
    }
    
	for(Lead lead : leadsToConvert){   
		     
	    if(lead.Status == 'Qualified' && !lead.isConverted && lead.isApproved__c == true){	    	
	    		        
	        Database.LeadConvert leadConvert = new database.LeadConvert();
	        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];

            leadConvert.setConvertedStatus(convertStatus.MasterLabel); // ("Closed - Converted");
            leadConvert.setLeadId(lead.id);

	        Database.LeadConvertResult leadConvertResult = Database.convertLead(leadConvert);
	        System.assert(leadConvertResult.isSuccess());
	    }
    }
}

 

However, I want to specify (or set) each new Opportunity's record type and stage.

 

At the minimum, it's ok to rely on default SF behaviour, but for some reason my new Opportunities are currently created with "Closed - Lost" Stage, which is NOT the first Stage in my stages list.

 

For example purposes, let's say that I want the Opportunity to be created as a Record Type = "New Sale" and have a Stage = "First Presentation".

 

 

I have this trigger defined:

 

trigger convertLeadToImplementationOpportunity on Lead (before update) {
    for (Lead lead : Trigger.new){

      if(lead.Status == 'Qualified' && !lead.isConverted && lead.isApproved__c == true){
          Database.LeadConvert leadConvert = new database.LeadConvert();
          LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];

          leadConvert.setConvertedStatus(convertStatus.MasterLabel); // ("Closed - Converted");
          leadConvert.setLeadId(lead.id);

          Database.LeadConvertResult leadConvertResult = Database.convertLead(leadConvert);
          System.assert(leadConvertResult.isSuccess());
      }
    }
}

 

When I execute the main trigger code anonymously via the IDE, it succeeds.


I have this (failing) test defined for the trigger:

 

@isTest
privateclass tests {
static testMethod void approvedLeadShouldConvertToImplementationOpportunity() {

      Lead testLead = new Lead(FirstName='John', LastName='Smith', Status='Qualified');
      insert testLead;

      test.startTest();

       testlead.isApproved__c = true;
       update testlead;  //trigger convertLeadToImplementationOpportunity

       test.stopTest();

       Lead isConverted = [SELECT id, isConverted FROM Lead WHERE Id=:testlead.id];
       System.assert(isConverted.isConverted);    // fails - i.e. isConverted == false
    }
}

 

Can anyone please tell me why the assert is failing? I'm quite new to Apex, so forgive any obvious stupidity.


Thanks!

Adam Breen

 

I have this (working) trigger:

 

trigger convertLeadToImplementationOpportunity on Lead (after update) {
    Set<Lead> leadsToConvert = new Set<Lead>();
    for (Lead lead : Trigger.new){
    	leadsToConvert.add(lead);
    }
    
	for(Lead lead : leadsToConvert){   
		     
	    if(lead.Status == 'Qualified' && !lead.isConverted && lead.isApproved__c == true){	    	
	    		        
	        Database.LeadConvert leadConvert = new database.LeadConvert();
	        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];

            leadConvert.setConvertedStatus(convertStatus.MasterLabel); // ("Closed - Converted");
            leadConvert.setLeadId(lead.id);

	        Database.LeadConvertResult leadConvertResult = Database.convertLead(leadConvert);
	        System.assert(leadConvertResult.isSuccess());
	    }
    }
}

 

However, I want to specify (or set) each new Opportunity's record type and stage.

 

At the minimum, it's ok to rely on default SF behaviour, but for some reason my new Opportunities are currently created with "Closed - Lost" Stage, which is NOT the first Stage in my stages list.

 

For example purposes, let's say that I want the Opportunity to be created as a Record Type = "New Sale" and have a Stage = "First Presentation".