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
SavithaSavitha 

Help in writing test class

Can someone help me in writing the test class for this Apex class..

public class ClsChatterfeed{

    public static void accountCreditCheck(Map<Id, Account> newAccountMap) {
        set<Id> AccountCreditCheckPendingIds = new set<Id>();
        for (Account acc : newAccountMap.values()) {
            if(acc.Please_do_Credit_Check__c == true) {
                AccountCreditCheckPendingIds.add(acc.Id);
            }
        }
        creditCheckUpdate(AccountCreditCheckPendingIds);
    }
   
    public static void accountCreditCheck(Map<Id, Account> newAccountMap, Map<Id, Account> oldAccountMap) {
        set<Id> AccountCreditCheckPendingIds = new set<Id>();
        if(Trigger.isUpdate && Trigger.IsAfter) {
            for (Account acc : newAccountMap.values()) {
                if(oldAccountMap.get(acc.Id).Please_do_Credit_Check__c != acc.Please_do_Credit_Check__c && acc.Please_do_Credit_Check__c == true) {
                    AccountCreditCheckPendingIds.add(acc.Id);
                }
            }
        }
        creditCheckUpdate(AccountCreditCheckPendingIds);
    }
   
    public static void creditCheckUpdate(Set<Id> AccountCreditCheckPendingIds) {
        if(!AccountCreditCheckPendingIds.IsEmpty()) {
            List<Account> accList = new List<Account>();
            for(Account acc : [Select Id, Name, Last_Credit__c, Credit_Check_Requestor1__c from Account where Id =: AccountCreditCheckPendingIds]) {
                acc.Credit_Check_Requestor1__c = UserInfo.getUserId();
                accList.add(acc);
            }
            if (!(accList.isEmpty())) {
                try {
                    update accList;
                } catch(Exception e) {
                }
            }
        }
    }
   
    public static void feedItemCreate(Set<Id> AccountCreditCheckPendingIds) {
        if(runOnce()) {
            if(!AccountCreditCheckPendingIds.IsEmpty()) {
                List<FeedItem> feedItemList = new List<FeedItem>();
                for(Account acc : [Select Id, Name, Last_Credit__c, SystemModstamp, Credit_Check_Requestor1__c, Please_do_Credit_Check__c from Account where Id =: AccountCreditCheckPendingIds]) {
                    if(acc.Last_Credit__c != null && acc.Credit_Check_Requestor1__c != null && acc.Please_do_Credit_Check__c == true) {
                        FeedItem fitem = new FeedItem();
                        fitem.type = 'LinkPost';
                        fitem.ParentId = acc.Credit_Check_Requestor1__c;
                        fitem.LinkUrl = '/' + acc.id;
                        fitem.Title = acc.Name + ' Account';  //This is the title that displays for the LinkUrl
                        fitem.Body = ('Last Credit has been entered at '+acc.SystemModstamp);
                        feedItemList.add(fItem);
                    }
                }
                if(feedItemList.size() > 0 && !feedItemList.isEmpty()) {
                    try {
                        insert feedItemList;
                    } catch(Exception e) {
                        system.debug('Exception Occurred : '+feedItemList);
                    }
                }
            }
        }
    }
   
    private static boolean recursiveCheck = true;
    public static boolean runOnce() {
        if(recursiveCheck){
            recursiveCheck = false;
            return true;
        }else{
            return recursiveCheck;
        }
    }
}
Pavan Kumar KajaPavan Kumar Kaja
Can u tel me how ur calling the above class. through trigger or vf page or button? post that code also. it will be helpful to strat.
SavithaSavitha
This is my trigger

trigger tgrchatterfeed on Account (after insert, after update) {
Map<Id,Account> oldAccMap = new Map<Id,Account>();
Map<Id,Account> newAccMap = new Map<Id,Account>();
Set<Id> accIds = new Set<Id>();
    if(Trigger.isInsert) {
        for(Account temp : trigger.new) {
            if(temp.Please_do_Credit_Check__c == true) {
                newAccMap.put(temp.Id,temp);
            }
            if(temp.Last_Credit__c != null) {
                accIds.add(temp.Id);
            }
        }
        if(newAccMap.size() > 0 && newAccMap != null) {
            ClsChatterfeed.accountCreditCheck(newAccMap);
        }
        if(accIds.size() > 0 && accIds != null) {
            ClsChatterfeed.feedItemCreate(accIds);
        }
    } else {
        for(Account temp : trigger.new) {
            if(temp.Please_do_Credit_Check__c != null && temp.Please_do_Credit_Check__c != trigger.oldMap.get(temp.Id).Please_do_Credit_Check__c) {
                newAccMap.put(temp.Id,temp);
                oldAccMap.put(temp.Id,trigger.oldMap.get(temp.Id));
            }
           
            if(temp.Last_Credit__c != null && temp.Last_Credit__c != trigger.oldMap.get(temp.Id).Last_Credit__c) {
                accIds.add(temp.Id);
            }
        }
        if((newAccMap.size() > 0 && newAccMap != null) && (oldAccMap.size() > 0 && oldAccMap != null)) {
            ClsChatterfeed.accountCreditCheck(newAccMap,oldAccMap);
        }
        if(accIds.size() > 0 && accIds != null) {
            ClsChatterfeed.feedItemCreate(accIds);
        }
    }
   
}
Pavan Kumar KajaPavan Kumar Kaja
 try this and fill all the required fileds on the aaccount. and let me know is this filed (Please_do_Credit_Check__c) is  checkbox ?

@isTest
public class TesttgrchatterfeedTrigger{
static testMethod void TestClsChatterfeed(){
   boolean recursiveCheck = true;
 
  Account acc = new Account();
  acc.Name = 'Test';
  acc.Please_do_Credit_Check__c = true;
  acc.Last_Credit__c = 35;
  acc.Credit_Check_Requestor1__c =56; (fill value)
  insert acc;
 
 
  acc.Last_Credit__c = 365;
  acc.Please_do_Credit_Check__c = false;
  update acc;
}
}