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
sieb4mesieb4me 

what is wrong in this code

this code below works fine and throws error when case team is not there when status is changed per code.

however it still throws error when someone updates case and case team is there, what is wrong here?

 

thanks

 

//start ***** checks if case team is there, if not, wont allow change of status
  public  void caseteam(){
 
          setCaseId = new Set<Id>();
     
          for (Case c : mescal){  
           if(c.Status != moldmap.get(c.Id).Status && c.Record_Type_Name__c=='Escalation'
            && (c.status == 'Active Engaged' || c.status == 'Active Wait' || c.status == 'Analysis'||
             c.status == 'Closed'))     
             {
                  setCaseId.add(c.Id);
              }   
              }
             mapCaseWithteamMember = new  Map<Id,CaseTeamMember>();
              if(!(setCaseId.ISEMPTY())){
                  for(CaseTeamMember ct: [SELECT Parentid FROM CaseTeamMember  WHERE parentid IN :setCaseId])
                  {mapCaseWithteamMember.put(ct.ParentId,ct);}
                       }
       if(mapCaseWithTeamMember.ISEMPTY()){  
          for(Case c: mescal)
                  { 
          if(mapCaseWithTeamMember.get(c.Id) ==null &&  c.Record_Type_Name__c=='Escalation' &&
           (c.status == 'Active Engaged' || c.status == 'Active Wait' || c.status == 'Analysis'||
             c.status == 'Closed'))
                          {  
              c.addError('No case team is assigned, please assign a team member before changing status');
                           }
        
                 }
                        }
                       
                         }


  //end ------------------------------------------

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
public  void caseteam()
{
	setCaseId = new Set<Id>();

	for (Case c : mescal)
	{
		if(c.Status != moldmap.get(c.Id).Status
		   && c.Record_Type_Name__c=='Escalation' 
		   && (c.status == 'Active Engaged' || c.status == 'Active Wait' || c.status == 'Analysis'|| c.status == 'Closed'))     
		{
			setCaseId.add(c.Id);
		}
	}

	mapCaseWithteamMember = new  Map<Id,CaseTeamMember>();
	
	if(!(setCaseId.ISEMPTY()))
	{
		for(CaseTeamMember ct: [SELECT Parentid FROM CaseTeamMember  WHERE parentid IN :setCaseId])
		{
			mapCaseWithteamMember.put(ct.ParentId,ct);
		}
	}

	if(mapCaseWithTeamMember.ISEMPTY())
	{
		for(Case c: mescal)
		{
			if(c.Status != moldmap.get(c.Id).Status
			   &&c.Record_Type_Name__c=='Escalation' 
			   && (c.status == 'Active Engaged' || c.status == 'Active Wait' || c.status == 'Analysis'|| c.status == 'Closed'))
			{
				if(mapCaseWithTeamMember.get(c.Id) == null ) // You need to check for the teamMember only for the records which satisfy the above criteria.
				{
					c.addError('No case team is assigned, please assign a team member before changing status');
				}
			}
		}
	}
}

 try this.

All Answers

Bhawani SharmaBhawani Sharma
What error and on which line?
Naidu PothiniNaidu Pothini
public  void caseteam()
{
	setCaseId = new Set<Id>();

	for (Case c : mescal)
	{
		if(c.Status != moldmap.get(c.Id).Status
		   && c.Record_Type_Name__c=='Escalation' 
		   && (c.status == 'Active Engaged' || c.status == 'Active Wait' || c.status == 'Analysis'|| c.status == 'Closed'))     
		{
			setCaseId.add(c.Id);
		}
	}

	mapCaseWithteamMember = new  Map<Id,CaseTeamMember>();
	
	if(!(setCaseId.ISEMPTY()))
	{
		for(CaseTeamMember ct: [SELECT Parentid FROM CaseTeamMember  WHERE parentid IN :setCaseId])
		{
			mapCaseWithteamMember.put(ct.ParentId,ct);
		}
	}

	if(mapCaseWithTeamMember.ISEMPTY())
	{
		for(Case c: mescal)
		{
			if(c.Status != moldmap.get(c.Id).Status
			   &&c.Record_Type_Name__c=='Escalation' 
			   && (c.status == 'Active Engaged' || c.status == 'Active Wait' || c.status == 'Analysis'|| c.status == 'Closed'))
			{
				if(mapCaseWithTeamMember.get(c.Id) == null ) // You need to check for the teamMember only for the records which satisfy the above criteria.
				{
					c.addError('No case team is assigned, please assign a team member before changing status');
				}
			}
		}
	}
}

 try this.

This was selected as the best answer
sieb4mesieb4me
thanks that did help.
also other way to avoid was add
if(setCaseId.ISEMPTY()) return; and that works too.

Thanks a lot.