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
prasanth kumarprasanth kumar 

my test code for trigger is not working, please help

Hi dides, i wrote a test class for my trigger.    After inserting record in a custom object my trigger will create a salesforce user. Trigger is working well, and test code is also success, But code coverage is not coming. Please help me.
 
trigger copytouserobject on newchatter__c (before insert) {
    list<user> allusers=new list<user>();
 //   list<profile> p1=[ select id from profile where id = '00e28000000oU4t' limit 1];
    Profile p = [SELECT Id FROM Profile WHERE Name='Chatter Free User'];



    for(newchatter__c n1:trigger.new)
    {
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.EmailHeader.triggerUserEmail = true;       
dmo.EmailHeader.triggerOtherEmail = true;
dmo.EmailHeader.triggerAutoResponseEmail = true;       
dmo.optAllOrNone = true;
//above anuradha code

        //n1.first_name__c='hello';
        user u1=new user();
        u1.FirstName=n1.First_Name__c;
        u1.LastName=n1.Last_Name__c;
        u1.Alias=n1.Alias__c; 
        u1.Email=n1.Email__c;
        u1.CommunityNickname=n1.Nickname__c;
        u1.username=n1.Username__c;
     //   u1.Profileid ='00e28000000oU4t';
     u1.Profileid=p.id;
     //   u1.CurrencyIsoCode = 'USD';
u1.localesidkey='en_US';
u1.timezonesidkey='America/Los_Angeles';
u1.EmailEncodingKey = 'ISO-8859-1';
u1.LanguageLocaleKey = 'en_US';
//u1.UserPermissionsMobileUser = false;
u1.RECEIVESINFOEMAILS=true;
u1.RECEIVESADMININFOEMAILS=true;
//u1.resetpassword=true;
        allusers.add(u1);


u1.setOptions(dmo);
    }
    insert allusers;

}

 Test class code.
 
@istest
public class newchattertest {

    @istest static void mytest()
    {
newchatter__c n1=new newchatter__c();
n1.Alias__c ='mydog';
n1.Email__c='prasanth523001@gmail.com';
n1.First_Name__c='somex';
n1.Last_Name__c='somey';
n1.Nickname__c='nothing';
n1.Username__c='testing6789rtr@gmail.com';
	  
        insert n1;
        
        user u=[select id,firstname,lastname,alias,email,CommunityNickname,username,Profileid,localesidkey,timezonesidkey,EmailEncodingKey,LanguageLocaleKey,RECEIVESINFOEMAILS, RECEIVESADMININFOEMAILS from user where Alias ='mydog' limit 1];
    
        system.assertEquals('somex',u.firstname );
system.assertEquals('somey',u.lastname );
system.assertEquals('mydog',u.alias );
system.assertEquals('prasanth523001@gmail.com',u.email );
system.assertEquals('nothing',u.CommunityNickname );
system.assertEquals('00e28000000oU4t',u.Profileid );
system.assertEquals('testing6789rtr@gmail.com',u.username );
system.assertEquals('en_US',u.localesidkey );
system.assertEquals('America/Los_Angeles',u.timezonesidkey );
system.assertEquals('ISO-8859-1',u.EmailEncodingKey );
system.assertEquals('en_US',u.LanguageLocaleKey );
system.assertEquals(true,u.RECEIVESINFOEMAILS );
system.assertEquals(true,u.RECEIVESADMININFOEMAILS );
        
    }   
}

 
Best Answer chosen by prasanth kumar
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help you
@istest
public class newchattertest 
{
    static testMethod void mytest()
    {
		newchatter__c n1=new newchatter__c();
		n1.Alias__c ='mydog';
		n1.Email__c='prasanth523001@gmail.com';
		n1.First_Name__c='somex';
		n1.Last_Name__c='somey';
		n1.Nickname__c='nothing';
		n1.Username__c='testing6789rtr@gmail.com';
        insert n1;
    }   
}
NOTE:- Please click on Run All Test Button then check code coverage

Please let us know if this will help you

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help you
@istest
public class newchattertest 
{
    static testMethod void mytest()
    {
		newchatter__c n1=new newchatter__c();
		n1.Alias__c ='mydog';
		n1.Email__c='prasanth523001@gmail.com';
		n1.First_Name__c='somex';
		n1.Last_Name__c='somey';
		n1.Nickname__c='nothing';
		n1.Username__c='testing6789rtr@gmail.com';
        insert n1;
    }   
}
NOTE:- Please click on Run All Test Button then check code coverage

Please let us know if this will help you

 
This was selected as the best answer
prasanth kumarprasanth kumar
Amith, Words are not coming form my mouth........   I love u so much......  for this code coverage i am struggling from this morning onwards.,.......   love u again...........     Amith please tell me difference between "Run test" for specific test class and "Run all Test".   After clicking on "Run all Test" only i got this code coverage.......    Thank u again. 
Amit Chaudhary 8Amit Chaudhary 8
Thanks you much.

Run Test will check code coverage one on s specific Test Class. If you click on Run All Test Class That will execute All Test classes in your org to calculate the overall all code covarage.
If  you want to see overall code coverage of each class in your org. Then please download below app exchange product and check each class code coverage.
https://appexchange.salesforce.com/listingDetail?listingId=a0N3000000DXzlpEAD

I hope that will help you

Thanks
Amit Chaudhary
prasanth kumarprasanth kumar
amith Please slove this also........      the belowing code also getting problem with test class.  Both this trigger and below code works for same custom object. 

https://developer.salesforce.com/forums/ForumsMain?id=906F00000005KwUIAU