+ Start a Discussion
affuaffu 

Code Coverage

I am getting 50% code coverage plz help urgent

 

Trigger

trigger UpdateOfDealerName On Opportunity (before insert,before update) {
    for(Opportunity o: Trigger.New) {
        if((o.RecordTypeName__c == 'Petty Sales')&&((o.User_ID__c =='Custom Partner User - Karnataka')||
            (o.User_ID__c =='Custom Partner User - Non Karnataka'))) {       
            
            o.AccountId = [select Account_Name__c from User where ID =: UserInfo.getUserId() Limit 1].Account_Name__c;
         }
    }
}

 Testclass

 

@istest
private class TestClass {
    public static testmethod void testing() { 
        Account a = new Account(Name='Test Account');
        insert a;
        Opportunity o = new opportunity(name='test opportunity', Accountid=a.id, stagename='Payment Collected', CloseDate=System.Today());
        insert o;
        o.AccountId = [select Account_Name__c from User where ID =: UserInfo.getUserId() Limit 1].Account_Name__c;
        update a;
        update o;
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet for code coverage 

 

@isTest

private class TestTriggerFrUserOppoutunity

{

    public static testMethod void testUserOppoutunity()

    {

       Account acc=new Account();

       acc.Name='DummyAcc';

       insert acc;

      

       Profile p=[select id from Profile where Name='System Administrator' Limit 1];

           

       User us=new User(UserName='aa@nav.dev1',IsActive=true,LastName='Raj',Email='aj1234@nav.com',Alias='AAgni',TimeZoneSidKey='America/Los_Angeles',LocaleSidKey='en_US',EmailEncodingKey='ISO-8859-1',LanguageLocaleKey='en_US');

       us.ProfileId=p.id;

       us.Account_Name__c=acc.id;

       insert us;

      

       Opportunity  op=new Opportunity();

       op.Name='dummyOpp';

       op.CloseDate=date.valueOf('2011-12-12');

       op.StageName='Prospecting';

       op.User_ID__c='Custom Partner User - Non Karnataka';

       op.RecordTypeName__c='Petty Sales';

       insert op;

      

        System.runAs(us)

        {

           update op;

        }

           

    }

}

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

When you are inserting the opportunity you need to assign RecordTypeName__c And User_ID__c in your test method.

For Example :

Opportunity o = new opportunity(name='test opportunity', RecordTypeName__c = 'Petty Sales', User_ID__c = “Custom Partner User - Non Karnataka”,Accountid=a.id, stagename='Payment Collected', CloseDate=System.Today());

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

affuaffu

Hi,

Yes i have done tat but iam getting an error as

 

Error: Compile Error: Field is not writeable: Opportunity.RecordTypeName__c at line 10 column 85

 

Thank u Jain.

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet for code coverage 

 

@isTest

private class TestTriggerFrUserOppoutunity

{

    public static testMethod void testUserOppoutunity()

    {

       Account acc=new Account();

       acc.Name='DummyAcc';

       insert acc;

      

       Profile p=[select id from Profile where Name='System Administrator' Limit 1];

           

       User us=new User(UserName='aa@nav.dev1',IsActive=true,LastName='Raj',Email='aj1234@nav.com',Alias='AAgni',TimeZoneSidKey='America/Los_Angeles',LocaleSidKey='en_US',EmailEncodingKey='ISO-8859-1',LanguageLocaleKey='en_US');

       us.ProfileId=p.id;

       us.Account_Name__c=acc.id;

       insert us;

      

       Opportunity  op=new Opportunity();

       op.Name='dummyOpp';

       op.CloseDate=date.valueOf('2011-12-12');

       op.StageName='Prospecting';

       op.User_ID__c='Custom Partner User - Non Karnataka';

       op.RecordTypeName__c='Petty Sales';

       insert op;

      

        System.runAs(us)

        {

           update op;

        }

           

    }

}

This was selected as the best answer