[SalesForce] SOQL query not returning any record even when it is present. Trigger failing due to this

I have a trigger where I have to Insert a new Record in the "CaseTeamMember" object whenever a new "Case" is created. For this I have to query the "CaseTeamRole = CSO" .

In my developer console when I give the query only as below then I get the CSO record.

Select Id,Name from CaseTeamRole

enter image description here

But when I try to fetch the Id of only the "CSO record then it is not fetching. I have tried queries in multiple ways as below :

1) Select Id,Name from CaseTeamRole where Name LIKE 'CS%' (Before this query used to get the CSO record. Now after sandbox refresh this is also failing)

2) Select Id,Name from CaseTeamRole where Name = 'CSO'

3) Select Id,Name from CaseTeamRole where Name LIKE 'C%'

enter image description here

If I give any other record then those are getting fetched .

enter image description here

This is not allowing me to even create a case now due to SOQL not fecthing any record in trigger 🙁

Please help me in understanding what might be the issue.

Edit
Trigger

Trigger CaseTrigger on Case (After Insert) 
{
    If(Trigger.isAfter)
    {
        If(Trigger.isInsert)
        {
            For(Case C : Trigger.New)
            {
                IF((C.Record_Type_Name__c == 'Delivery Escalation' || C.Record_Type_Name__c.startsWith('CSP')) && (C.CreatedDate == C.LastModifiedDate))
                {
                    CaseTriggerHelper.CreateNewCaseTeamMember(Trigger.New);
                }
            }
        }
    }
}

Class:

Public Class CaseTriggerHelper
{
    Public Static Void CreateNewCaseTeamMember(List<Case> CaseIds)
    {  
        List<CaseTeamMember> CTMList = New List <CaseTeamMember>(); 

        CaseTeamRole CTR = [Select Id,Name from CaseTeamRole where Name LIKE 'CS%'];
        system.debug('---CTR---'+CTR);

        Try
        {
            For(Case C : CaseIds)
            {
                CaseTeamMember CTM = New CaseTeamMember();

                CTM.ParentId = C.Id;
                CTM.MemberId = C.CreatedById;
                CTM.TeamRoleId = CTR.Id;
                CTMList.Add(CTM);
            }
            Insert CTMList;
        }
        Catch(Exception e){
            } 
    }
}

UPDATE

Should I pass a list of cases in my trigger handler in this way?

Trigger CaseTrigger on Case (After Insert) { 
  Set<ID> ids = Trigger.newMap.keySet(); 
  If(Trigger.isAfter && Trigger.isInsert) { 
    For(Case C : Trigger.New) { 
       List<Case> C = [SELECT Id FROM Case 
                          WHERE ((Record_Type_Name__c == 'Delivery Escalation' || 
                                  Record_Type_Name__c.startsWith('CSP')) &&
                                 (CreatedDate == LastModifiedDate))];  
       CaseTriggerHelper.CreateNewCaseTeamMember(C); 
    } 
  } 
 }

Thanks!
Ruchi

Best Answer

I observed the same issue in our full sandbox after a refresh. I believe it is a bug, though it is not yet on Salesforce's known issues list.

If you change your query to Select Id,Name from CaseTeamRole where Name != 'CSO', your query will probably return all results except the record where the Name is CSO.

If you change your query to Select Id,Name from CaseTeamRole where Name like '%s%', your query will probably return two records: CSO and Global Sales Manager.

It looks to me that the results only return if > 1 row is in the result set.

The workaround that could work for you is to query for all CaseTeamRole records and then loop through each result looking for Name = 'CSO'. For example:

for (CaseTeamRole ctr: [select id, name from CaseTeamRole])
{
 if(ctr.Name == 'CSO')
 {..do stuff.. }
}

If you happened to open a case with Salesforce, would be curious to know if they confirmed whether it is a bug or not.

Related Topic