• glynn
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 3
    Replies

I have a testMethod and most of it works fine. However, there is a part of my Apex trigger that my test case is not getting into (executing).

 

 

 //Logic to handle different email subjects for each FeedItem type
        if (f.Type == 'ContentPost')
        {
            emailSubject = 'New ContentPost';
                     
            if (f.ContentData != NULL)
            {
                Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
                efa.setBody(f.ContentData);
                efa.setFileName(f.ContentFileName);
                mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
                tiBod=tiBod+'\n'+'***See Attached File!';
            }
            else
            {
                Map<String,Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();   
                List<Schema.SObjectType> sobjects = schemaMap.values();
                List<Sobject> theObjectResults;
                Schema.DescribeSObjectResult objDescribe;
                List<Schema.SObjectField> tempFields;
                for(Schema.SObjectType objType : sobjects)
                {
                    objDescribe = objType.getDescribe(); 
                    String sobjectPrefix = objDescribe.getKeyPrefix();
                    if(parentid != null && sobjectPrefix != null && parentid.startsWith(sobjectPrefix))
                    {
                        string objectType = objDescribe.getLocalName();
                        objectType = objectType + 'Feed';
                        
                        String qryString = 'SELECT contentData FROM ' + objectType + ' WHERE id' + '=' + '\'' + f.id + '\'';  
                    
                        sobject qr = Database.query(qryString);
                        Blob resultObject = (Blob)qr.get('ContentData');
                         
                        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
                        efa.setBody(resultObject);
                        efa.setFileName(f.ContentFileName);
                        mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
                    }
                }
            }        
        }
        else if  (f.Type == 'LinkPost')
        {
            emailSubject = 'New LinkPost';
            tiBod = tiBod + '\n' + 'Link: ' + f.linkUrl;
        }
        else
        {
            emailSubject = 'New TextPost';
        }

 

 

How do I test the code within the outside IF and ELSE IF statements. The code that starts at

 

 

(f.Type == 'ContentPost')

 

 

Once in the IF statement I'm also trying to figure out how to test the MAP and LIST types.

Appreciate any help you can give this newbie. thanks

  • March 22, 2011
  • Like
  • 0

Hi, I created my first ever trigger, see below. However, I've really struggled the last two days trying to Unit Test it.  I've failed miserably so far. I wonder can anyone here help me?  Here is my trigger:

 

trigger UserStatusBefore on User (before update) {
    
/**********************************************************************
*                                                                        
* This code fires *ONLY* when a user updates status on homepage. If user posts     *
* URL or file along with status, then FeedItemAfter trigger fires. FeedItemAfter   *
* trigger also fires when a user posts to another user's feed.                     *
*                                                                                  *
************************************************************************************/ 
    
    for (User f : trigger.new)
    {
        Datetime thisDT = System.now();
        String itemCreatedDate = thisDT.format('yyyy-MM-dd HH:mm:ss');
        String tiBod;       
           
        for (Id theId : trigger.oldMap.keySet())
           {
                User oldUsr=trigger.oldMap.get(theId);
                User newUsr=trigger.newMap.get(theId);
                String oldStatus=oldUsr.CurrentStatus;
                String newStatus=newUsr.CurrentStatus;
                String UserId1 = f.id;
                
                if (oldStatus != newStatus && null != newStatus)
                {
                    //UserProfileFeed up = [SELECT Id, Body, CommentCount, ContentData, ContentDescription, ContentFileName, ContentSize, ContentType, CreatedDate, InsertedByID, IsDeleted, LastModifiedDate, LikeCount, LinkUrl, ParentId, Title, Type FROM UserProfileFeed WITH UserId = :UserId1 LIMIT 1];
                    User u = [select id, name, email from user where id = :f.Id];                                    
                    tiBod = 'Posted Against: User Home Page' + '\n' + 'Message: ' + f.CurrentStatus + '\n' + 'Created by: ' + u.name + ' - ' + u.email + '\n' + 'Created On: ' + itemCreatedDate;
        
                    //Compile and send email message 
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    String[] toAddresses = new String[] {System.Label.ChatterArchiveExternalEmailAddress};
                    mail.setToAddresses(toAddresses);
                    mail.setSubject('New UserStatus Update');
                    mail.setSaveAsActivity(false);
                    mail.setPlainTextBody(tiBod);
                    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
                }
                else
                {    
                    //do nothing
                } 
           }          
    }    
}

 

 

 

 

Can someone show me what the Unit Test for this trigger looks like? thanks

  • March 19, 2011
  • Like
  • 0

Hi,

 

I created my first ever trigger, see below. However, I've really struggled the last two days trying to Unit Test it.  I've failed miserably so far. I wonder can anyone here help me?  Here is my trigger:

 

 

trigger UserStatusBefore on User (before update) {
    
/**********************************************************************
*                                                                        
* This code fires *ONLY* when a user updates status on homepage. If user posts     *
* URL or file along with status, then FeedItemAfter trigger fires. FeedItemAfter   *
* trigger also fires when a user posts to another user's feed.                     *
*                                                                                  *
************************************************************************************/ 
    
    for (User f : trigger.new)
    {
        Datetime thisDT = System.now();
        String itemCreatedDate = thisDT.format('yyyy-MM-dd HH:mm:ss');
        String tiBod;       
           
        for (Id theId : trigger.oldMap.keySet())
           {
                User oldUsr=trigger.oldMap.get(theId);
                User newUsr=trigger.newMap.get(theId);
                String oldStatus=oldUsr.CurrentStatus;
                String newStatus=newUsr.CurrentStatus;
                String UserId1 = f.id;
                
                if (oldStatus != newStatus && null != newStatus)
                {
                    //UserProfileFeed up = [SELECT Id, Body, CommentCount, ContentData, ContentDescription, ContentFileName, ContentSize, ContentType, CreatedDate, InsertedByID, IsDeleted, LastModifiedDate, LikeCount, LinkUrl, ParentId, Title, Type FROM UserProfileFeed WITH UserId = :UserId1 LIMIT 1];
                    User u = [select id, name, email from user where id = :f.Id];                                    
                    tiBod = 'Posted Against: User Home Page' + '\n' + 'Message: ' + f.CurrentStatus + '\n' + 'Created by: ' + u.name + ' - ' + u.email + '\n' + 'Created On: ' + itemCreatedDate;
        
                    //Compile and send email message 
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    String[] toAddresses = new String[] {System.Label.ChatterArchiveExternalEmailAddress};
                    mail.setToAddresses(toAddresses);
                    mail.setSubject('New UserStatus Update');
                    mail.setSaveAsActivity(false);
                    mail.setPlainTextBody(tiBod);
                    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
                }
                else
                {    
                    //do nothing
                } 
           }          
    }    
}

 

Can someone show me what the Unit Test for this trigger looks like? thanks

 

  • March 19, 2011
  • Like
  • 0

I have a testMethod and most of it works fine. However, there is a part of my Apex trigger that my test case is not getting into (executing).

 

 

 //Logic to handle different email subjects for each FeedItem type
        if (f.Type == 'ContentPost')
        {
            emailSubject = 'New ContentPost';
                     
            if (f.ContentData != NULL)
            {
                Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
                efa.setBody(f.ContentData);
                efa.setFileName(f.ContentFileName);
                mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
                tiBod=tiBod+'\n'+'***See Attached File!';
            }
            else
            {
                Map<String,Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();   
                List<Schema.SObjectType> sobjects = schemaMap.values();
                List<Sobject> theObjectResults;
                Schema.DescribeSObjectResult objDescribe;
                List<Schema.SObjectField> tempFields;
                for(Schema.SObjectType objType : sobjects)
                {
                    objDescribe = objType.getDescribe(); 
                    String sobjectPrefix = objDescribe.getKeyPrefix();
                    if(parentid != null && sobjectPrefix != null && parentid.startsWith(sobjectPrefix))
                    {
                        string objectType = objDescribe.getLocalName();
                        objectType = objectType + 'Feed';
                        
                        String qryString = 'SELECT contentData FROM ' + objectType + ' WHERE id' + '=' + '\'' + f.id + '\'';  
                    
                        sobject qr = Database.query(qryString);
                        Blob resultObject = (Blob)qr.get('ContentData');
                         
                        Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
                        efa.setBody(resultObject);
                        efa.setFileName(f.ContentFileName);
                        mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
                    }
                }
            }        
        }
        else if  (f.Type == 'LinkPost')
        {
            emailSubject = 'New LinkPost';
            tiBod = tiBod + '\n' + 'Link: ' + f.linkUrl;
        }
        else
        {
            emailSubject = 'New TextPost';
        }

 

 

How do I test the code within the outside IF and ELSE IF statements. The code that starts at

 

 

(f.Type == 'ContentPost')

 

 

Once in the IF statement I'm also trying to figure out how to test the MAP and LIST types.

Appreciate any help you can give this newbie. thanks

  • March 22, 2011
  • Like
  • 0

Hi, I created my first ever trigger, see below. However, I've really struggled the last two days trying to Unit Test it.  I've failed miserably so far. I wonder can anyone here help me?  Here is my trigger:

 

trigger UserStatusBefore on User (before update) {
    
/**********************************************************************
*                                                                        
* This code fires *ONLY* when a user updates status on homepage. If user posts     *
* URL or file along with status, then FeedItemAfter trigger fires. FeedItemAfter   *
* trigger also fires when a user posts to another user's feed.                     *
*                                                                                  *
************************************************************************************/ 
    
    for (User f : trigger.new)
    {
        Datetime thisDT = System.now();
        String itemCreatedDate = thisDT.format('yyyy-MM-dd HH:mm:ss');
        String tiBod;       
           
        for (Id theId : trigger.oldMap.keySet())
           {
                User oldUsr=trigger.oldMap.get(theId);
                User newUsr=trigger.newMap.get(theId);
                String oldStatus=oldUsr.CurrentStatus;
                String newStatus=newUsr.CurrentStatus;
                String UserId1 = f.id;
                
                if (oldStatus != newStatus && null != newStatus)
                {
                    //UserProfileFeed up = [SELECT Id, Body, CommentCount, ContentData, ContentDescription, ContentFileName, ContentSize, ContentType, CreatedDate, InsertedByID, IsDeleted, LastModifiedDate, LikeCount, LinkUrl, ParentId, Title, Type FROM UserProfileFeed WITH UserId = :UserId1 LIMIT 1];
                    User u = [select id, name, email from user where id = :f.Id];                                    
                    tiBod = 'Posted Against: User Home Page' + '\n' + 'Message: ' + f.CurrentStatus + '\n' + 'Created by: ' + u.name + ' - ' + u.email + '\n' + 'Created On: ' + itemCreatedDate;
        
                    //Compile and send email message 
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    String[] toAddresses = new String[] {System.Label.ChatterArchiveExternalEmailAddress};
                    mail.setToAddresses(toAddresses);
                    mail.setSubject('New UserStatus Update');
                    mail.setSaveAsActivity(false);
                    mail.setPlainTextBody(tiBod);
                    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
                }
                else
                {    
                    //do nothing
                } 
           }          
    }    
}

 

 

 

 

Can someone show me what the Unit Test for this trigger looks like? thanks

  • March 19, 2011
  • Like
  • 0

Hi,

 

I created my first ever trigger, see below. However, I've really struggled the last two days trying to Unit Test it.  I've failed miserably so far. I wonder can anyone here help me?  Here is my trigger:

 

 

trigger UserStatusBefore on User (before update) {
    
/**********************************************************************
*                                                                        
* This code fires *ONLY* when a user updates status on homepage. If user posts     *
* URL or file along with status, then FeedItemAfter trigger fires. FeedItemAfter   *
* trigger also fires when a user posts to another user's feed.                     *
*                                                                                  *
************************************************************************************/ 
    
    for (User f : trigger.new)
    {
        Datetime thisDT = System.now();
        String itemCreatedDate = thisDT.format('yyyy-MM-dd HH:mm:ss');
        String tiBod;       
           
        for (Id theId : trigger.oldMap.keySet())
           {
                User oldUsr=trigger.oldMap.get(theId);
                User newUsr=trigger.newMap.get(theId);
                String oldStatus=oldUsr.CurrentStatus;
                String newStatus=newUsr.CurrentStatus;
                String UserId1 = f.id;
                
                if (oldStatus != newStatus && null != newStatus)
                {
                    //UserProfileFeed up = [SELECT Id, Body, CommentCount, ContentData, ContentDescription, ContentFileName, ContentSize, ContentType, CreatedDate, InsertedByID, IsDeleted, LastModifiedDate, LikeCount, LinkUrl, ParentId, Title, Type FROM UserProfileFeed WITH UserId = :UserId1 LIMIT 1];
                    User u = [select id, name, email from user where id = :f.Id];                                    
                    tiBod = 'Posted Against: User Home Page' + '\n' + 'Message: ' + f.CurrentStatus + '\n' + 'Created by: ' + u.name + ' - ' + u.email + '\n' + 'Created On: ' + itemCreatedDate;
        
                    //Compile and send email message 
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    String[] toAddresses = new String[] {System.Label.ChatterArchiveExternalEmailAddress};
                    mail.setToAddresses(toAddresses);
                    mail.setSubject('New UserStatus Update');
                    mail.setSaveAsActivity(false);
                    mail.setPlainTextBody(tiBod);
                    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
                }
                else
                {    
                    //do nothing
                } 
           }          
    }    
}

 

Can someone show me what the Unit Test for this trigger looks like? thanks

 

  • March 19, 2011
  • Like
  • 0