+ Start a Discussion
Redmanx03Redmanx03 

Test Class Help: Cannot Get Code Coverage Up For Opportunity Trigger

Hello Have A trigger on Opportunity but I cannot Get Code Coverage over 37%. What Am I Doing Wrong?

Trigger:

 

trigger ChatterWonOpportunitySAPremise on Opportunity (After insert, After update) {

String status;
String OppAccName;
String OppOwnerName;
FeedItem post = new FeedItem();



   Set<Id> ownerIds = new Set<Id>();
   
       for (Opportunity o : trigger.new) {
        ownerIds.add(o.OwnerId);
    }
 
    Map<Id, User> mapUsers = new Map<Id, User>([SELECT Id, Profile.Name,User_Market__c FROM User WHERE Id IN :ownerIds ]);
 
    for (Opportunity o : trigger.new) {
    
        User oOwner = mapUsers.get(o.OwnerId);
 
        if (oOwner.Profile.Name != 'Hearst Premise/ DMS User '& oOwner.User_Market__c!='San Antonio') {
            Return;
        }
        
        
    
/*    for(Opportunity o : Trigger.new) {
        if(o.OwnerId == '005d0000001TUC8') { //It will not post record for for this user to group.
            return;
        }    */
        else {
            if(Trigger.isInsert ) { 
                if(o.IsWon == true ) { //This will be executed on any Closed Won Record
                    for (Opportunity oppty : [SELECT Account.Name, Owner.Name,Amount FROM Opportunity WHERE Id =:o.Id] ) {
                        OppAccName = oppty.Account.Name;
                        OppOwnerName = oppty.Owner.Name;
                    }    
                    status = OppOwnerName + ' just won ' + OppAccName + ' for ' + o.Amount + '!';

                    post.ParentId = '0F9J00000008VHB';
                    post.Title = o.Name;
                    post.Body = status;
                    
                    insert post;
                }
            }    
            else {
                if ( Trigger.isUpdate ) {
                    if(o.IsWon == true && Trigger.oldMap.get(o.id).IsWon == false) { //This will be executed on update to existing record
                        for (Opportunity oppty : [SELECT Account.Name, Owner.Name FROM Opportunity WHERE Id =:o.Id] ) {
                            OppAccName = oppty.Account.Name;
                            OppOwnerName = oppty.Owner.Name;
                        }    
                        status = OppOwnerName + ' just won ' + OppAccName + ' for ' + o.Amount + '!';
                            
                        post.ParentId = '0F9J00000008VHB';
                        post.Title = o.Name;
                        post.Body = status;
                        
                        insert post;      
                    }
                }
            }
        }
    }    
}

 Test Class:

@isTest
private class TestChatterWonOpportunitySAPremise{




    static testMethod void TestChatterWonOpportunitySAPremise() {
//        Profile p = [SELECT Id FROM profile WHERE name='Hearst Premise/ DMS User']; //

Profile pp = [Select id from Profile where Name = 'Hearst Premise/ DMS User' limit 1];
        User u = new User(
            Alias = 'Prem', 
            Email='Premiseuser@testorg.com',
            EmailEncodingKey='UTF-8',
            LastName='PremiseTesting',
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US',
            ProfileId = pp.Id,
            TimeZoneSidKey='America/Los_Angeles',
            UserName='Premiseuser@testorg.com',
            User_Market__c='San Antonio'            
        );
        Insert u;
        
        


    
       
        
        Account account = new Account(Name = 'Test Account');
    Database.insert(account);
        Opportunity o = new Opportunity(Account=Account,Amount =1500,Name='Test', StageName='Closed - Won',CloseDate= system.Today());
//o.Ownerid=u.id;//
        insert o; 
Opportunity oppCreated = [Select Name, Account.Name, StageName, Owner.Name, Amount From Opportunity Where Id =: o.Id LIMIT 1];        
        
        
String status;
String OppAccName;
String OppOwnerName;
FeedItem post = new FeedItem(); 
 
             
        
                           for (Opportunity oppty1 : [SELECT Account.Name,StageName, Owner.Name,Amount FROM Opportunity WHERE Id =:o.Id] ) {
                        OppAccName = oppty1.Account.Name;
                        OppOwnerName = oppty1.Owner.Name;
                            //Test that Stage = 'Closed - Won'
        system.assertEquals(o.StageName, 'Closed - Won');           
                    } 

     

        
                            status = OppOwnerName + ' just won ' + OppAccName + ' for ' + o.Amount + '!';

                    post.ParentId = '0F9J00000008VHB';
                    post.Title = o.Name;
                    post.Body = status;
    
            insert post;
}
}

 

Atul111Atul111

You have hardcoded the User ID in trigger. This should not be. Also update the opportunity so your coverage will be increased. Please let me know if need any help

Redmanx03Redmanx03

In the test class? Sorry I am new to Apex. I notice User id in trigger unt i have commented out..

Atul111Atul111

In trigger you have hardcoded the value. It should not be there. For testing this i need the access so that i can help

Redmanx03Redmanx03
I don't understand.. Which Part of the trigger? I would appreciate your
help but would prefer to learn to Fish. That way one day i can help people
like you are helping me :)

--

Thanks,


Carlos


[image: sf_cert_adm_rgb.png]
Atul111Atul111
we can discuss about this issue
Redmanx03Redmanx03
Where on the code is the problem? I'm sorry im a little confused on your
response

--

Thanks,


Carlos


[image: sf_cert_adm_rgb.png]