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
raginireddy thoutireddyraginireddy thoutireddy 

Hi! I am new to salesforce and I have a requirement to show all the cases related to an Account on Account Page Layout. Can anyone please help me on doing this in Visual Flow.

Best Answer chosen by raginireddy thoutireddy
Abdul KhatriAbdul Khatri
OK Great just change the below line
Case testCase = new Case(Status='New', Origin='Email', Issue_type__c='Customization', Account = acc);
to 
Case testCase = new Case(Status='New', Origin='Email', Issue_type__c='Customization', AccountId = acc.Id);

Here is the corrected code with 100% coverage
@isTest
private class countCases_Test {

    @isTest static void createAccountwithCase() {
        // Create an account
        Account acc = new Account();
        acc.Name = 'Test123';
        insert acc;

        // Create a case associated with the test account
        Case testCase = new Case(Status='New', Origin='Email', Issue_type__c='Customization', AccountId = acc.Id);
        insert testCase;
    }
}

All Answers

Abdul KhatriAbdul Khatri
That is available Out of Box, if you navigate to any individual Account. it may not be visible though but you can make it visible Edit Page Layout and drag down the Related List.

Why you want to Visual Flow route when it is available on Point and Click?
raginireddy thoutireddyraginireddy thoutireddy
Sorry I have posted it wrong. I want the total number of cases on the account pagelayout. As case and account object do not have master detail relationship. I would like to use visual flow for it.
Abdul KhatriAbdul Khatri
Is there any specific scenario that you are looking to see through visual flow. I mean how you want the Account to be chosen with visual flow and you are looking to call through a button passing the accountId. Please little detail on you requirement.
raginireddy thoutireddyraginireddy thoutireddy
There is a number field count case on account and I want the number of cases on that account to populate on the case count field. If there are 5 cases on the account it should populate as 5 in the case count field
Abdul KhatriAbdul Khatri
I think this could be done easily with trigger, if you want.
raginireddy thoutireddyraginireddy thoutireddy
trigger countCases on Case (after insert, after update, after delete, after undelete) {
    List<Case> caseList = (trigger.isDelete)? trigger.old : trigger.new;
        set<Id> accountIdSet = new set<Id>();
        for(Case cs: caseList ){
            accountIdSet .add(cs.AccountId);
        }

    if(accountIdSet.size() > 0){
        List<Account> lstAccount = [select id, No_of_Cases__c, (select id from Cases) from Account where id IN: accountIdSet ];
        if(lstAccount.size() > 0){
            for(Account acc: lstAccount){
                acc.No_of_Cases__c = acc.Cases.size();
            }
            
            update lstAccount;
        }
    }
}


The test class for the trigger is 
@isTest
private class countCases_Test {

    @isTest static void createAccountwithCase() {
        // Create an account
        Account acc = new Account();
        acc.Name = 'Test123';
        insert acc;

        // Create a case associated with the test account
        Case testCase = new Case(Status='New', Origin='Email', Issue_type__c='Customization', Account = acc);
        insert testCase;
    }
}


But I get code coverage error. Can you please look at the trigger and test class and suggest me the changes.
Abdul KhatriAbdul Khatri
OK Great just change the below line
Case testCase = new Case(Status='New', Origin='Email', Issue_type__c='Customization', Account = acc);
to 
Case testCase = new Case(Status='New', Origin='Email', Issue_type__c='Customization', AccountId = acc.Id);

Here is the corrected code with 100% coverage
@isTest
private class countCases_Test {

    @isTest static void createAccountwithCase() {
        // Create an account
        Account acc = new Account();
        acc.Name = 'Test123';
        insert acc;

        // Create a case associated with the test account
        Case testCase = new Case(Status='New', Origin='Email', Issue_type__c='Customization', AccountId = acc.Id);
        insert testCase;
    }
}
This was selected as the best answer
raginireddy thoutireddyraginireddy thoutireddy
Thank You! that worked.