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
KitaSanKitaSan 

Trigger works in Dev but not Prod... (static variable)

Trigger posts a chatter update when 'post to chatter' checkbox is selected. Workflow is causing trigger to fire twice in Prod so change was made in dev (passed tests) but fails on validation in Prod. 

HelperClass: 
public class HelperClass {
   public static boolean firstRun = true;
}



Trigger

trigger newsToChatter on News_Catalyst__c (after update) {
    
Set<ID> newsIds = new Set<ID>();
       for (News_Catalyst__c n : Trigger.new) {
       
        if (trigger.isUpdate && n.Post_to_chatter__c != FALSE && trigger.oldMap.get(n.id).Post_To_Chatter__c != TRUE) {
            if(HelperClass.firstRun){
                newsIds.add(n.ID);
            }
        }
      }
      
      List<News_Catalyst__c> updatedNewsCats = [SELECT Id, News_Description__c, Name, Wordpress_Post_Hyperlink__c FROM News_Catalyst__c WHERE Id in :newsIds];
      
      List<FeedItem> feedItems = new List<FeedItem>();
    
    for (News_Catalyst__c nc : updatedNewsCats){
        // Loop through each Related Deal record
        List<Customer_News_Connector__c> relatedConnectors = [SELECT Id, Account__c, Chatter_Ticker_Tag__c, Customer_News__c FROM Customer_News_Connector__c WHERE Customer_News__c =: nc.Id];
        for(Customer_News_Connector__c connector : relatedConnectors){
            //logic to add chatter posting 
            FeedItem fitem = new FeedItem();

            //what if the description is less than 200 characters?
            Integer i = nc.News_Description__c.replaceAll('<[^>]+>',' ').length();
              if (i > 200) { i = 200;}
            
            fitem.type = 'TextPost';
            fitem.ParentId = connector.account__c;
            fitem.body = '#CustomerNews - '+nc.name + '\n\n' + nc.News_Description__c.replaceAll('<[^>]+>',' ').substring(0,i) +'...' + '\n\n Read More: \n '+nc.Wordpress_Post_Hyperlink__c+'\n\n'  +connector.Chatter_Ticker_Tag__c ;
            feedItems.add(fitem);
            
        }
    }
    //Save the FeedItems all at once.
    if (feedItems.size() != 0) {
        insert(feedItems);
    } 
    //set the helperclass
    HelperClass.firstRun=false;
 }

Test Class: 

@isTest 
private class News_to_Trigger_TestClass {
    
/*
This class tests that checking the Add_to_Chatter__c box creates a chatter posing with details of the activity

+Revision History+
2014 08 29 - Created by Justin Kitagawa        
*/  
   
     static testMethod void testChatterActivity() {
        //create the test account
        Account a = new Account(name='Test Account');
        a.End_User_Number__c = NULL;
        a.RecordTypeId = '012300000004wx8';
        a.Type = 'Prospect';
        a.Site_Type__c = 'Division Headquarters';
        a.Country__c ='United States';
        a.Region__c = 'NAmer';
        a.Primary_Vertical__c = 'Auto';
        a.Primary_Sub_Vertical__c ='Services';
        a.Primary_Industry_Description__c = '4214 - Trucking and storage, local';
        insert a;
        
        //test that no chatter was created
        list<AccountFeed> posts1 = [select ID from AccountFeed where ParentId =: a.id];
        system.assertEquals(0,posts1.size());

        //create a news_catalyst on that account
        News_Catalyst__c n = new News_Catalyst__c(Name = 'Test News');
        n.News_Description__c = 'Aint nobody got time for that';
        n.Post_To_Chatter__c = FALSE;
        insert n;

        //Test if Acount was created
        list<Account> acct = [select id from Account where id = :a.id];
        system.AssertEquals(1,acct.size());

        //Create the connector record
        Customer_News_Connector__c connector = new Customer_News_Connector__c(); 
        connector.Account__c = a.Id; 
        connector.Customer_News__c = n.Id;
        insert connector;
        
        //Test if Customer_News_Connector__c was created
        list<Customer_News_Connector__c> newsConnector = [select id from Customer_News_Connector__c where id = :connector.id];
        system.AssertEquals(1,newsConnector.size());

        //Update the news catalyst so Post_To_Chatter__c = TRUE
        n.Post_To_Chatter__c = TRUE; 
        update n; 

        //Test if News_Catalyst__c was created
        list<News_Catalyst__c> news = [select id from News_Catalyst__c where id = :n.id];
        system.AssertEquals(1,news.size());
        
        //Test if chatter feed was created
        list<AccountFeed> posts2 = [select id from AccountFeed where parentid =: a.id];
        system.assertEquals(1,posts2.size());
        
    }
}


Trigger passes without the HelperClass.firstRun check. 

How is HelperClass.firstRun returning false? 

Any help is greatly appreciated. 
 

KitaSanKitaSan
Line that is failing in trigger if 59. It keeps coming back with posts2.size = 0
Gaurav NirwalGaurav Nirwal
I can run your code but I can't taking any type of problem in your code 

Please run it again
KitaSanKitaSan
Thanks, Matthews. 

That is the stragne thing, it only fails in production. 

Do you know of any reason that a static class would not work in production? 

-Justin