• Wizno
  • NEWBIE
  • 0 Points
  • Member since 2011

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

I'm having trouble figuring out what needs to be done to test a trigger. I've looked at any tutorials/examples I could find and tried the concepts but I'm just not getting any code coverage from what I've been doing.

 

Would really appreciate some guidance here... there's something I'm not understanding.

 

Thanks in advance!

 

 

Overview:

Switching to a new data model where different category items are now stored in 1 object instead of across multiple objects.

So the trigger checks if 11 CategoryItems exist for a certain building, and adds them if they don't.

 

I can't figure out how to get coverage for all the 'categoryItems.add' statements. Best way I could think of was to do the IF statement and assertEquals... But that's not working.

 

 

Here is the trigger:

trigger addCategoryItemsToOldData on Building__c (after update) {   
    
    for(Building__c b : trigger.new)
    {
        List<CategoryItem__c> cats = [SELECT Id, categoryType__c, Name FROM CategoryItem__c WHERE building__c = :b.Id];
        
        if( cats.size() == 0 )
        {
            
            List<CategoryItem__c> categoryItems = new List<CategoryItem__c>{};
            
                    //1.1 This is for the new data model
            
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat1', Name = 'Cat1'));
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat2', Name = 'Cat2'));
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat3', Name = 'Cat3'));
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat4', Name = 'Cat4'));
            
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat5', Name = 'Cat5'));            
            
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat6', Name = 'Cat6'));            
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat7', Name = 'Cat7'));
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat8', Name = 'Cat8'));
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat9', Name = 'Cat9'));
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat10', Name = 'Cat10'));
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat11', Name = 'Cat11'));            
          
            
                insert categoryItems;
            
        }
        
    }
    

}

 

 

Here is my test Class:

@istest
public class testAddCategoryItemsToOldData
{
    static testMethod void addCategoryItemsToOldDataModel()
    {
        List<Building__c> bldg = new List<Building__c>{};
        List<Account> acct = new List<Account>{};
        
test.startTest();        
        for(Integer i = 0; i < 20; i++)
        {
            Account newAcct = new Account(Name = 'Test Account ABC00_' +i, County__c = 'TheCounty');
            acct.add(newAcct);
        }
        insert acct;
        
        for(Integer i = 0; i < 20; i++)
        {
            Building__c bldgs = new Building__c(Name = 'Test Building BL00_'+i, State__c = 'FL', Account__c = acct[i].Id, City__c = 'TheCity', Zip_Code__c = '99999');
            bldg.add(bldgs);
            insert bldgs;
            
            //bldgs.Zip_Code__c = '01234';
            //update bldgs;
        }
        //insert bldg;
        /*
        for(Building__c b : bldg)
        {
            //b.Zip_Code__c = '01234';
        }        
        //update bldg;
        */
test.stopTest();        
        for(Building__c bb : bldg)
        {
            List<CategoryItem__c> cats = [SELECT Name, categoryType__c FROM CategoryItem__c WHERE Building__c = :bb.Id];
            system.AssertEquals(11, cats.size());
            
            for(CategoryItem__c c: cats)
            {
                
                system.Debug('*****************************************************************************************************************');
                system.Debug('* * * * * * * * * * * * *' + c.Name);
                system.Debug('*****************************************************************************************************************');
                
                if(c.Name == 'cat1')
                {                    
                    system.AssertEquals('cat1', c.Name);
                }
                if(c.Name == 'cat2')
                {
                    system.AssertEquals('cat2', c.Name);                    
                }
                if(c.Name == 'cat3')
                {
                    system.AssertEquals('cat3', c.Name);                    
                }
                if(c.Name == 'cat4')
                {
                    system.AssertEquals('cat4', c.Name);                    
                }
                if(c.Name == 'cat5')
                {
                    system.AssertEquals('cat5', c.Name);                    
                }
                if(c.Name == 'cat6')
                {
                    system.AssertEquals('cat6', c.Name);                    
                }
//Add in other IF's if code coverage increases...
                
            }
            
        }
            
            
        
    }
}

 

 

  • December 19, 2011
  • Like
  • 0

Hey all,

 

I'm having a hard time figure out what I'm doing wrong.

 

I'm trying to have a lead get auto convertted if the lead source is of a specific type but, something is going wrong and I'm really not sure what it is.

 

If I try making a lead with the specified LeadSource and click save, the next page says that the lead was converted. The only problem is that there is no new Account and Contact object record related to that lead, and the lead also gets deleted.

 

Just ran apex tests in the sandbox and I'm not getting any errors.

 

Really would appreciate some guidance here... I've looked at like 5 tutorials and all of them have had essentially the same code, if not identical.

 

Thanks

 

 

if ( trigger.isAfter && trigger.isInsert)
    {
    	for( Lead l : trigger.new)
    	{
    		if( l.LeadSource == 'CustomPortal' && l.IsConverted == false)
            {
            	Database.Leadconvert lc = new Database.LeadConvert();
            	
            	lc.setLeadId(l.Id);
            	lc.setDoNotCreateOpportunity(true);
            	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());
            	
            	Account newA = new Account( id = l.ConvertedAccountId, Primary_Contact__c = l.ConvertedContactId, Name = l.FirstName + ' ' + l.LastName);
            	//UpdateAccts.add(newA);
            	insert newA;
            }
    	}
    	//Insert UpdateAccts;
    }

 

  • December 15, 2011
  • Like
  • 0

I'm having trouble figuring out what needs to be done to test a trigger. I've looked at any tutorials/examples I could find and tried the concepts but I'm just not getting any code coverage from what I've been doing.

 

Would really appreciate some guidance here... there's something I'm not understanding.

 

Thanks in advance!

 

 

Overview:

Switching to a new data model where different category items are now stored in 1 object instead of across multiple objects.

So the trigger checks if 11 CategoryItems exist for a certain building, and adds them if they don't.

 

I can't figure out how to get coverage for all the 'categoryItems.add' statements. Best way I could think of was to do the IF statement and assertEquals... But that's not working.

 

 

Here is the trigger:

trigger addCategoryItemsToOldData on Building__c (after update) {   
    
    for(Building__c b : trigger.new)
    {
        List<CategoryItem__c> cats = [SELECT Id, categoryType__c, Name FROM CategoryItem__c WHERE building__c = :b.Id];
        
        if( cats.size() == 0 )
        {
            
            List<CategoryItem__c> categoryItems = new List<CategoryItem__c>{};
            
                    //1.1 This is for the new data model
            
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat1', Name = 'Cat1'));
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat2', Name = 'Cat2'));
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat3', Name = 'Cat3'));
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat4', Name = 'Cat4'));
            
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat5', Name = 'Cat5'));            
            
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat6', Name = 'Cat6'));            
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat7', Name = 'Cat7'));
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat8', Name = 'Cat8'));
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat9', Name = 'Cat9'));
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat10', Name = 'Cat10'));
                    categoryItems.add(new CategoryItem__c(Building__c = b.id,
                        categoryType__c = 'Cat11', Name = 'Cat11'));            
          
            
                insert categoryItems;
            
        }
        
    }
    

}

 

 

Here is my test Class:

@istest
public class testAddCategoryItemsToOldData
{
    static testMethod void addCategoryItemsToOldDataModel()
    {
        List<Building__c> bldg = new List<Building__c>{};
        List<Account> acct = new List<Account>{};
        
test.startTest();        
        for(Integer i = 0; i < 20; i++)
        {
            Account newAcct = new Account(Name = 'Test Account ABC00_' +i, County__c = 'TheCounty');
            acct.add(newAcct);
        }
        insert acct;
        
        for(Integer i = 0; i < 20; i++)
        {
            Building__c bldgs = new Building__c(Name = 'Test Building BL00_'+i, State__c = 'FL', Account__c = acct[i].Id, City__c = 'TheCity', Zip_Code__c = '99999');
            bldg.add(bldgs);
            insert bldgs;
            
            //bldgs.Zip_Code__c = '01234';
            //update bldgs;
        }
        //insert bldg;
        /*
        for(Building__c b : bldg)
        {
            //b.Zip_Code__c = '01234';
        }        
        //update bldg;
        */
test.stopTest();        
        for(Building__c bb : bldg)
        {
            List<CategoryItem__c> cats = [SELECT Name, categoryType__c FROM CategoryItem__c WHERE Building__c = :bb.Id];
            system.AssertEquals(11, cats.size());
            
            for(CategoryItem__c c: cats)
            {
                
                system.Debug('*****************************************************************************************************************');
                system.Debug('* * * * * * * * * * * * *' + c.Name);
                system.Debug('*****************************************************************************************************************');
                
                if(c.Name == 'cat1')
                {                    
                    system.AssertEquals('cat1', c.Name);
                }
                if(c.Name == 'cat2')
                {
                    system.AssertEquals('cat2', c.Name);                    
                }
                if(c.Name == 'cat3')
                {
                    system.AssertEquals('cat3', c.Name);                    
                }
                if(c.Name == 'cat4')
                {
                    system.AssertEquals('cat4', c.Name);                    
                }
                if(c.Name == 'cat5')
                {
                    system.AssertEquals('cat5', c.Name);                    
                }
                if(c.Name == 'cat6')
                {
                    system.AssertEquals('cat6', c.Name);                    
                }
//Add in other IF's if code coverage increases...
                
            }
            
        }
            
            
        
    }
}

 

 

  • December 19, 2011
  • Like
  • 0

Hey all,

 

I'm having a hard time figure out what I'm doing wrong.

 

I'm trying to have a lead get auto convertted if the lead source is of a specific type but, something is going wrong and I'm really not sure what it is.

 

If I try making a lead with the specified LeadSource and click save, the next page says that the lead was converted. The only problem is that there is no new Account and Contact object record related to that lead, and the lead also gets deleted.

 

Just ran apex tests in the sandbox and I'm not getting any errors.

 

Really would appreciate some guidance here... I've looked at like 5 tutorials and all of them have had essentially the same code, if not identical.

 

Thanks

 

 

if ( trigger.isAfter && trigger.isInsert)
    {
    	for( Lead l : trigger.new)
    	{
    		if( l.LeadSource == 'CustomPortal' && l.IsConverted == false)
            {
            	Database.Leadconvert lc = new Database.LeadConvert();
            	
            	lc.setLeadId(l.Id);
            	lc.setDoNotCreateOpportunity(true);
            	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());
            	
            	Account newA = new Account( id = l.ConvertedAccountId, Primary_Contact__c = l.ConvertedContactId, Name = l.FirstName + ' ' + l.LastName);
            	//UpdateAccts.add(newA);
            	insert newA;
            }
    	}
    	//Insert UpdateAccts;
    }

 

  • December 15, 2011
  • Like
  • 0

Hi everyone,

 

I am hoping that someone could help me out and I apologize if I posted this in the wrong section. I am new to the developer force. My company has the Enterprise version of SFDC.

 

I am trying to find a way to have new leads that come in to SFDC convert automatically if there is an existing contact or if the leads company is an existing account. I don't know Apex code all that well so anything would help.

 

Thank you!