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 

need to break this into class

I want the following code which does 2 different things to be in class and want to write a trigger to call the class.

Can someone suggest best way to do it or how to do it.

 

thanks

 

 

trigger Checkcase on Case (before update) {

/*
On Update contact name field, update web email field.
On change of status to values given below, check case team is there, if not throw message.
*/

public Set<Id> setContactId = new Set<Id>();
public Map<Id,Contact> mapContact;

 

Public Set<Id> setCaseId = new Set<Id>();
Public Map<Id, CaseTeamMember> mapCaseWithteamMember=new Map<Id, CaseTeamMember>();

 

Id recTypename = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Escalation').getRecordTypeId();

for (Case c : Trigger.new)
{
    if(c.Status != Trigger.oldMap.get(c.Id).Status && c.RecordTypeId==recTypename && (c.status == 'Active Engaged'
    || c.status == 'Active Wait' || c.status == 'Analysis'|| c.status == 'Closed'))
        {
        setCaseId.add(c.Id);
        }
setContactId.add(c.ContactId);

}

 

// 1. on contact change, change the contact email
 mapContact =new Map<Id,Contact>([SELECT id, email FROM contact  WHERE Id IN: setContactId]);
    if (!mapContact.ISEMPTY()){
        for(Case c: Trigger.New){
             if(mapContact.get(c.ContactId) !=null && mapContact.get(c.ContactId).Email !=null){
                 c.SuppliedEmail = mapContact.get(c.ContactId).Email;
             }   
        }
   }


   if(setCaseId.ISEMPTY())   
      return;

 

 

// 2. Find case team, if empty, throw message before status change.


For(CaseTeamMember ct: [SELECT parentid FROM CaseTeamMember  WHERE parentid IN :setCaseId]){
mapCaseWithteamMember.put(ct.ParentId,ct);
}

for(Case c: Trigger.New){
              if(mapCaseWithTeamMember.get(c.Id) ==null && c.RecordTypeId==recTypename) {
                   c.addError('No case team is assigned, please assign a team member.');
              }
     }
    
     }

KaminiKamini

Please use the following code for Trigger and its Apex Class:

 

// Trigger

trigger Checkcase on Case (before update) {
/*
On Update contact name field, update web email field.
On change of status to values given below, check case team is there, if not throw message.
*/
public Set<Id> setContactId = new Set<Id>();

Public Set<Id> setCaseId = new Set<Id>();

Id recTypename = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Escalation').getRecordTypeId();
for (Case c : Trigger.new)
{
if(c.Status != Trigger.oldMap.get(c.Id).Status && c.RecordTypeId==recTypename && (c.status == 'Active Engaged'
|| c.status == 'Active Wait' || c.status == 'Analysis'|| c.status == 'Closed'))
{
setCaseId.add(c.Id);
}
setContactId.add(c.ContactId);
}

UpdateCase u = new UpdateCase();
u. updateCaseRecord(setContactId, setCaseId);

}

 

// Apex Class
public class UpdateCase{

public Map<Id,Contact> mapContact;
Public Map<Id, CaseTeamMember> mapCaseWithteamMember=new Map<Id, CaseTeamMember>();
public UpdateCase(){
}

public void updateCaseRecord(set<Id> setContactId, set<Id> setCaseId){
// 1. on contact change, change the contact email
mapContact =new Map<Id,Contact>([SELECT id, email FROM contact WHERE Id IN: setContactId]);
if (!mapContact.ISEMPTY()){
for(Case c: Trigger.New){
if(mapContact.get(c.ContactId) !=null && mapContact.get(c.ContactId).Email !=null){
c.SuppliedEmail = mapContact.get(c.ContactId).Email;
}
}
}

if(setCaseId.ISEMPTY())
return;


// 2. Find case team, if empty, throw message before status change.
Id recTypename = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Escalation').getRecordTypeId();
For(CaseTeamMember ct: [SELECT parentid FROM CaseTeamMember WHERE parentid IN :setCaseId]){
mapCaseWithteamMember.put(ct.ParentId,ct);
}

for(Case c: Trigger.New){
if(mapCaseWithTeamMember.get(c.Id) ==null && c.RecordTypeId==recTypename) {
c.addError('No case team is assigned, please assign a team member.');
}
}
}
}

sieb4mesieb4me
class was giving error since trigger.new is not allowed it seems.

loop variable must be of typesObject for line where there is trigger.new in class.