1

I have a field "DA Name" in Case object in which I am populating the value from "CaseTeamMember" object only when "Status" changes. This I am doing through Trigger. I do not want anyone to update this field manually or from back-end. For this I am trying to add a error message via TRIGGER on the "DA Name" field when anyone is trying to update it. But I am receiving the trigger error instead of my Custom Error.

enter image description here

Below is the Trigger:

Trigger CaseTrigger on Case (After Insert , After Update , Before Update) 
{
     If(Trigger.Is After)
     {
         IF(Trigger.isUpdate)
         {
            IF((C.Record_Type_Name__c == 'Delivery Escalation')  && (Trigger.oldMap.get(C.Id).Status != Trigger.newMap.get(C.Id).Status))
            {
                CaseTriggerHelper.CaseAuditMethod(Trigger.Old,Trigger.New);
            }

            IF((Trigger.oldMap.get(C.Id).Status == Trigger.newMap.get(C.Id).Status) && (Trigger.oldMap.get(C.Id).DA_Name__c != Trigger.newMap.get(C.Id).DA_Name__c))  
            {
                CaseTriggerHelper.ErrorOnModifyingDAName(Trigger.New);
            } 
         }
     }
}

Below is the Trigger Class

Public Class CaseTriggerHelper
{
     //This method is written to create new "Case Status History" record whenever the Case status is updated. And to update DA Name field from CaseTeamMember object
    Public Static Void CaseAuditMethod(List<Case> CaseOldIds , List<Case> CaseNewIds)
    {       

    //**Code to Update DA Name on Case Detail Page**
    Set<Id> CaseId = New Set<Id>();

    For(Case C : CaseNewIds)
    {
        CaseId.add(C.ID);
    }

    List<Case> CaseList = [Select Id,Status,DA_Name__c from Case where Id =: CaseId];
    List<CaseTeamMember> CaseTM = [Select Id,MemberId,Member.Name,ParentId,TeamRoleId,TeamRole.Name from CaseTeamMember where ParentId =: CaseId];

    Try{
    For(Case C : CaseList)
    {
        For(CaseTeamMember CTM : CaseTM)
        {
            If(C.Id == CTM.ParentId)
            {
                If(C.Status == 'DA Working' && CTM.TeamRole.Name == 'DA')
                {
                    C.DA_Name__c = CTM.Member.Name;
                    System.debug('---C.DA_Name__c---'+C.DA_Name__c);
                } 
            }
        }
        Update C;
    }
   }
    Catch(Exception e){
        } 
   //**Update ends here**     
}

    //To avoid updating the field DA Name
    Public Static Void ErrorOnModifyingDAName(List<Case> CaseIds)
    {
        Set<Id> CaseId = New Set<Id>();
        String DAName = 'Null';

        For(Case C : CaseIds)
        {
            CaseId.add(C.ID);
        }

        List<Case> CaseList = [Select Id,Status,DA_Name__c from Case where Id =: CaseId];
        List<CaseTeamMember> CaseTM = [Select Id,MemberId,Member.Name,ParentId,TeamRoleId,TeamRole.Name from CaseTeamMember where ParentId =: CaseId];

        Try{
        For(Case C : CaseList)
        {
            For(CaseTeamMember CTM : CaseTM)
            {
                If(C.Id == CTM.ParentId)
                {
                    If(C.Status == 'DA Working' && CTM.TeamRole.Name == 'DA')
                    {
                        DAName = CTM.Member.Name;
                    } 

                    If(C.DA_Name__c != DAName)
                    {
                        C.DA_Name__c.adderror('DA Name field should not be modified.');    
                       ***//I am getting the Error in above line. This is line 208.***
                    }
                }
            }
        }}
        Catch(Exception e){
            }
    }
}

How can I resolve this issue ?

Thanks!

  • 1
    C.DA_Name__c.adderror('DA Name field should not be modified.'); instead C.adderror('DA Name field should not be modified.'); try like this – Ratan Paul Feb 1 '16 at 12:27
  • also why are you quering it again? – Tushar Sharma Feb 1 '16 at 12:31
  • @Ratan : I had tried initially C.adderror('DA Name field should not be modified.');. But I had received the same error. – Ruchi Feb 1 '16 at 12:36
  • 1
    @Ruchi instead of caselist use CaseNewIds – Tushar Sharma Feb 1 '16 at 12:45
  • 1
    @TusharSharma : Thanks Tushar...This resolved my problem. – Ruchi Feb 1 '16 at 15:35
1

@Ruchi instead of caselist use CaseNewIds. Also don't query all records again.

because adderror works with trigger syntax. If you query the records then you will lost the syntax thats why you are facing this error.

0

Why are you querying the case object again, and also DML should be out of for loop in the first method.

    Public Class CaseTriggerHelper
    {
         //This method is written to create new "Case Status History" record whenever the Case status is updated. And to update DA Name field from CaseTeamMember object
        Public Static Void CaseAuditMethod(List<Case> CaseOldIds , List<Case> CaseNewIds)
        {       

        //**Code to Update DA Name on Case Detail Page**
        Set<Id> CaseId = New Set<Id>();

        For(Case C : CaseNewIds)
        {
            CaseId.add(C.ID);
        }

        List<Case> CaseList = [Select Id,Status,DA_Name__c from Case where Id =: CaseId];
        List<CaseTeamMember> CaseTM = [Select Id,MemberId,Member.Name,ParentId,TeamRoleId,TeamRole.Name from CaseTeamMember where ParentId =: CaseId];
    list<Case> updatedCases = new list<Case>();
        Try{
        For(Case C : CaseList )
        {
            For(CaseTeamMember CTM : CaseTM)
            {
                If(C.Id == CTM.ParentId)
                {
                    If(C.Status == 'DA Working' && CTM.TeamRole.Name == 'DA')
                    {
                        C.DA_Name__c = CTM.Member.Name;
                        System.debug('---C.DA_Name__c---'+C.DA_Name__c);
                        updatedCases.add(C);
                     } 
                }
            }

        }
update updatedCases ;
       }
        Catch(Exception e){
            } 
       //**Update ends here**     
    }

        //To avoid updating the field DA Name
        Public Static Void ErrorOnModifyingDAName(List<Case> CaseIds)
        {
            Set<Id> CaseId = New Set<Id>();
            String DAName = 'Null';

            For(Case C : CaseIds)
            {
                CaseId.add(C.ID);
            }

           // List<Case> CaseList = [Select Id,Status,DA_Name__c from Case where Id =: CaseId];
            List<CaseTeamMember> CaseTM = [Select Id,MemberId,Member.Name,ParentId,TeamRoleId,TeamRole.Name from CaseTeamMember where ParentId =: CaseId];

            Try{
            For(Case C : CaseIds)
            {
                For(CaseTeamMember CTM : CaseTM)
                {
                    If(C.Id == CTM.ParentId)
                    {
                        If(C.Status == 'DA Working' && CTM.TeamRole.Name == 'DA')
                        {
                            DAName = CTM.Member.Name;
                        } 

                        If(C.DA_Name__c != DAName)
                        {
                            C.adderror('DA Name field should not be modified.');    
                           ***//I am getting the Error in above line. This is line 208.***
                        }
                    }
                }
            }}
            Catch(Exception e){
                }
        }
    }
  • C.adderror('DA Name field should not be modified.'); also gives me the same issue. I had tried this even before. – Ruchi Feb 1 '16 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.