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
Kumar Rao 1Kumar Rao 1 

Trigger not firing, Help

Dear Folks,

Kindly help to debug below code as the trigger not firing.

Trigger Overview:

Update Case Owner of an Account when following conditions meet:

1.When Account Record Type is either Client or PA Client Only
2.Case Status is Not CLOSED
3.case Record Type is Client Review
====================================================================

Trigger UpdateCaseOwner on Account(After Update){
//Get Record typeID for Account & Case
    ID AC_RecTypeID_Client    = Schema.SObjectType.Account.GetRecordTypeInfosByName().Get('Client').GetRecordTypeID();
    ID AC_RecTypeID_PAclient  = Schema.SObjectType.Account.GetRecordTypeInfosByName().get('PA Client').GetRecordTypeID();
    ID Case_RecTypeID         = Schema.SObjectType.Case.GetRecordTypeInfosByName().Get('Client Review').getRecordTypeID();

//Get Account Before/After trigger values
    Account AcBeforeUpdt = trigger.old[0];
    Account AcAfterUpdt = Trigger.new[0];
    id ac_id;
    
    for(account a:Trigger.new){
    ac_id = a.id;
    }
    system.debug('Account ID is ==========>>>>>>'+ac_id);
    
//List to update
    list<case> AC_Cases = new list<case>();
    
    if(AcBeforeUpdt.OwnerID != AcAfterUpdt.OwnerID){
      if(AcAfterUpdt.RecordTypeID == AC_RecTypeID_Client || AcAfterUpdt.RecordTypeID == AC_RecTypeID_PAclient){
      
//Query to retrieve list of cases where Accountid is same as trigger account and status is not CLOSED
    list<Case> cs = [select ID, CaseNumber, OwnerID, RecordTypeID from case where OwnerID=:AcBeforeUpdt.OwnerID and Status!='Closed'];
    system.debug('List of cases ==========>>>>>>'+cs);
    for(case css:cs){
    if(css.RecordTypeID==Case_RecTypeID){
        css.OwnerID=AcAfterUpdt.OwnerID;        
        AC_Cases.add(css);
        }
    }
    system.debug('Total Case Owner Updates ==========>>>>>>'+AC_Cases);
    Update AC_Cases;
//    AC_Cases.add(cs);
      }
    }

}

Debug Log:
33.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
11:29:45.210 (210504132)|ENTERING_MANAGED_PKG|SFSSDupeCatcher
11:29:45.210 (210686998)|HEAP_ALLOCATE|[72]|Bytes:3
11:29:45.210 (210744012)|HEAP_ALLOCATE|[77]|Bytes:152
11:29:45.210 (210771851)|HEAP_ALLOCATE|[342]|Bytes:408
11:29:45.210 (210796510)|HEAP_ALLOCATE|[355]|Bytes:408
11:29:45.210 (210819392)|HEAP_ALLOCATE|[467]|Bytes:48
11:29:45.210 (210860061)|HEAP_ALLOCATE|[139]|Bytes:6
11:29:45.212 (212570657)|HEAP_ALLOCATE|[50]|Bytes:5
11:29:45.212 (212602349)|HEAP_ALLOCATE|[56]|Bytes:5
11:29:45.212 (212615233)|HEAP_ALLOCATE|[64]|Bytes:7
11:29:45.477 (477886364)|ENTERING_MANAGED_PKG|SFSSDupeCatcher
11:29:45.488 (488609867)|SOQL_EXECUTE_BEGIN|[550]|Aggregations:0|select Id, isPersonType from RecordType where Id = '012200000001WjPAAU'
11:29:45.491 (491663911)|SOQL_EXECUTE_END|[550]|Rows:1
11:29:45.492 (492737441)|SOQL_EXECUTE_BEGIN|[550]|Aggregations:0|select Id, isPersonType from RecordType where Id = '012200000001WjPAAU'
11:29:45.495 (495473710)|SOQL_EXECUTE_END|[550]|Rows:1
11:29:45.528 (528511455)|SOQL_EXECUTE_BEGIN|[131]|Aggregations:0|SELECT Id, Name, SFSSDupeCatcher__Match_On_Insert_Action__c, SFSSDupeCatcher__Match_On_Update_Action__c, SFSSDupeCatcher__Create_Tasks_for_Warnings__c, SFSSDupeCatcher__Blocked_Duplicates__c, SFSSDupeCatcher__Merged_Duplicates__c, SFSSDupeCatcher__Converted_Duplicates__c, SFSSDupeCatcher__Error_Message__c, SFSSDupeCatcher__Scenario_Type__c, SFSSDupeCatcher__Deployed__c, SFSSDupeCatcher__Bypass_Security__c, SFSSDupeCatcher__Person_Account_Filter__c, OwnerID, CreatedById FROM Scenario__c WHERE (Deployed__c = TRUE AND Person_Account_Filter__c = :tmpVar1) LIMIT 100
11:29:45.533 (533708751)|SOQL_EXECUTE_END|[131]|Rows:0
11:29:45.537 (537545110)|SOQL_EXECUTE_BEGIN|[214]|Aggregations:0|SELECT Id, Name, SFSSDupeCatcher__Match_On_Insert_Action__c, SFSSDupeCatcher__Match_On_Update_Action__c, SFSSDupeCatcher__Create_Tasks_for_Warnings__c, SFSSDupeCatcher__Blocked_Duplicates__c, SFSSDupeCatcher__Merged_Duplicates__c, SFSSDupeCatcher__Converted_Duplicates__c, SFSSDupeCatcher__Error_Message__c, SFSSDupeCatcher__Scenario_Type__c, SFSSDupeCatcher__Deployed__c, SFSSDupeCatcher__Bypass_Security__c, SFSSDupeCatcher__Person_Account_Filter__c, OwnerID, CreatedById FROM Scenario__c WHERE Deployed__c = TRUE LIMIT 100
11:29:45.540 (540787406)|SOQL_EXECUTE_END|[214]|Rows:0
11:29:45.553 (553519695)|CODE_UNIT_STARTED|[EXTERNAL]|01qM0000000DJ6J|UpdateCaseOwner on Account trigger event AfterUpdate for [001M000000plRQo]
11:29:45.553 (553551619)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:29:45.553 (553567227)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:29:45.553 (553728791)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
11:29:45.553 (553833982)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:32
11:29:45.553 (553849916)|VARIABLE_SCOPE_BEGIN|[1]|this|UpdateCaseOwner|true|false
11:29:45.553 (553916987)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x658b3ad4
11:29:45.553 (553962274)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:32
11:29:45.553 (553974197)|VARIABLE_SCOPE_BEGIN|[1]|this|UpdateCaseOwner|true|false
11:29:45.553 (553998547)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x658b3ad4
11:29:45.554 (554005782)|STATEMENT_EXECUTE|[1]
11:29:45.554 (554009907)|STATEMENT_EXECUTE|[3]
11:29:45.554 (554248549)|HEAP_ALLOCATE|[3]|Bytes:4
11:29:45.558 (558036743)|HEAP_ALLOCATE|[3]|Bytes:52
11:29:45.558 (558047650)|HEAP_ALLOCATE|[3]|Bytes:6
11:29:45.558 (558269718)|HEAP_ALLOCATE|[3]|Bytes:4
11:29:45.558 (558370675)|VARIABLE_ASSIGNMENT|[3]|this.AC_RecTypeID_Client|"012200000001WjPAAU"|0x658b3ad4
11:29:45.558 (558379309)|STATEMENT_EXECUTE|[4]
11:29:45.558 (558455405)|HEAP_ALLOCATE|[4]|Bytes:4
11:29:45.558 (558494620)|HEAP_ALLOCATE|[4]|Bytes:52
11:29:45.558 (558501596)|HEAP_ALLOCATE|[4]|Bytes:9
11:29:45.558 (558552523)|HEAP_ALLOCATE|[4]|Bytes:4
11:29:45.558 (558603073)|VARIABLE_ASSIGNMENT|[4]|this.AC_RecTypeID_PAclient|"012M0000000DFGUIA4"|0x658b3ad4
11:29:45.558 (558610838)|STATEMENT_EXECUTE|[5]
11:29:45.558 (558617164)|HEAP_ALLOCATE|[5]|Bytes:4
11:29:45.559 (559340303)|HEAP_ALLOCATE|[5]|Bytes:4
11:29:45.562 (562094350)|HEAP_ALLOCATE|[5]|Bytes:116
11:29:45.562 (562109706)|HEAP_ALLOCATE|[5]|Bytes:13
11:29:45.562 (562197461)|HEAP_ALLOCATE|[5]|Bytes:4
11:29:45.562 (562298839)|VARIABLE_ASSIGNMENT|[5]|this.Case_RecTypeID|"012D0000000QhkHIAS"|0x658b3ad4
11:29:45.562 (562307318)|STATEMENT_EXECUTE|[8]
11:29:45.565 (565996177)|VARIABLE_ASSIGNMENT|[8]|this.AcBeforeUpdt|{"LastModifiedDate":"2015-10-20T10:29:46.000Z","SFSSDupeCatcher__Ove (20 more) ...":false,"PersonDoNotCall":false,"Total_Daily_Rate__c":0.00,"Name":"My Client!!!**","Eric_Strong_Accounts (3 more) ...":false,"Google_Maps_Link__c":"http://maps.google.c (44 more) ...","Have_not_discussed_w (20 more) ...":false,"ConvFlowRun__c":false,"PersonHasOptedOutOfF (2 more) ...":false,"CreatedById":"005D00000052fO4IAI","OwnerId":"005D0000001VlzmIAC","Legal_Advice_Needed_ (2 more) ...":false,"Phone":"00110011","RecordTypeId":"012200000001WjPAAU","PersonHasOptedOutOfE (4 more) ...":false,"Financial_Advice_Nee (6 more) ...":false,"Couples_care__c":false,"Family_Dispute_block (20 more) ...":false,"Launch_Convert_Flow_ (2 more) ...":true,"First_Aid_box__c":false,"Region_New__c":"001M000000mCJ78IAG","IsDeleted":false,"Next_Review_Stage_Du (7 more) ...":"2016-04-17T00:00:00.000Z","IsPersonAccount":false,"Next_Review_Stage__c":"6 Monthly Review","Business_Development (10 more) ...":"_IM1_/s.gif_IM2_rati (7 more) ...","Respite_Care_needed_ (2 more) ...":false,"Regon_and_Descriptio (4 more) ...":"My Client!!!**  ()","SystemModstamp":"2015-10-20T10:29:46.000Z","Assessing_care_home_ (19 more) ...":false,"Webroster_Area__c":"%","CreatedDate":"2015-10-20T08:39:45.000Z","Id":"001M000000plRQoIAM","LastModifiedById":"005D00000052fO4IAI"}|0x658b3ad4
11:29:45.566 (566015999)|STATEMENT_EXECUTE|[9]
11:29:45.567 (567132347)|VARIABLE_ASSIGNMENT|[9]|this.AcAfterUpdt|{"LastModifiedDate":"2015-10-20T10:26:53.000Z","SFSSDupeCatcher__Ove (20 more) ...":false,"PersonDoNotCall":false,"Total_Daily_Rate__c":0.00,"Name":"My Client!!!**","Eric_Strong_Accounts (3 more) ...":false,"Google_Maps_Link__c":"http://maps.google.c (44 more) ...","Have_not_discussed_w (20 more) ...":false,"ConvFlowRun__c":false,"PersonHasOptedOutOfF (2 more) ...":false,"CreatedById":"005D00000052fO4IAI","OwnerId":"005D0000004dNfWIAU","Legal_Advice_Needed_ (2 more) ...":false,"Phone":"00110011","RecordTypeId":"012200000001WjPAAU","PersonHasOptedOutOfE (4 more) ...":false,"Financial_Advice_Nee (6 more) ...":false,"Couples_care__c":false,"Family_Dispute_block (20 more) ...":false,"Launch_Convert_Flow_ (2 more) ...":true,"First_Aid_box__c":false,"Region_New__c":"001M000000mCJ78IAG","IsDeleted":false,"Next_Review_Stage_Du (7 more) ...":"2016-04-17T00:00:00.000Z","IsPersonAccount":false,"Next_Review_Stage__c":"6 Monthly Review","Business_Development (10 more) ...":"_IM1_/s.gif_IM2_rati (7 more) ...","Respite_Care_needed_ (2 more) ...":false,"Regon_and_Descriptio (4 more) ...":"My Client!!!**  ()","SystemModstamp":"2015-10-20T10:26:53.000Z","Assessing_care_home_ (19 more) ...":false,"Webroster_Area__c":"%","CreatedDate":"2015-10-20T08:39:45.000Z","Id":"001M000000plRQoIAM","LastModifiedById":"005D00000052fO4IAI"}|0x658b3ad4
11:29:45.567 (567149794)|STATEMENT_EXECUTE|[10]
11:29:45.567 (567174005)|VARIABLE_ASSIGNMENT|[10]|this.ac_id|null|0x658b3ad4
11:29:45.567 (567274031)|HEAP_ALLOCATE|[12]|Bytes:5
11:29:45.567 (567301451)|VARIABLE_SCOPE_BEGIN|[12]|a|Account|true|false
11:29:45.568 (568038818)|VARIABLE_ASSIGNMENT|[12]|a|{"LastModifiedDate":"2015-10-20T10:26:53.000Z","SFSSDupeCatcher__Ove (20 more) ...":false,"PersonDoNotCall":false,"Total_Daily_Rate__c":0.00,"Name":"My Client!!!**","Eric_Strong_Accounts (3 more) ...":false,"Google_Maps_Link__c":"http://maps.google.c (44 more) ...","Have_not_discussed_w (20 more) ...":false,"ConvFlowRun__c":false,"PersonHasOptedOutOfF (2 more) ...":false,"CreatedById":"005D00000052fO4IAI","OwnerId":"005D0000004dNfWIAU","Legal_Advice_Needed_ (2 more) ...":false,"Phone":"00110011","RecordTypeId":"012200000001WjPAAU","PersonHasOptedOutOfE (4 more) ...":false,"Financial_Advice_Nee (6 more) ...":false,"Couples_care__c":false,"Family_Dispute_block (20 more) ...":false,"Launch_Convert_Flow_ (2 more) ...":true,"First_Aid_box__c":false,"Region_New__c":"001M000000mCJ78IAG","IsDeleted":false,"Next_Review_Stage_Du (7 more) ...":"2016-04-17T00:00:00.000Z","IsPersonAccount":false,"Next_Review_Stage__c":"6 Monthly Review","Business_Development (10 more) ...":"_IM1_/s.gif_IM2_rati (7 more) ...","Respite_Care_needed_ (2 more) ...":false,"Regon_and_Descriptio (4 more) ...":"My Client!!!**  ()","SystemModstamp":"2015-10-20T10:26:53.000Z","Assessing_care_home_ (19 more) ...":false,"Webroster_Area__c":"%","CreatedDate":"2015-10-20T08:39:45.000Z","Id":"001M000000plRQoIAM","LastModifiedById":"005D00000052fO4IAI"}|0x7b42cc68
11:29:45.568 (568059264)|STATEMENT_EXECUTE|[12]
11:29:45.568 (568062507)|STATEMENT_EXECUTE|[13]
11:29:45.568 (568126881)|VARIABLE_ASSIGNMENT|[13]|this.ac_id|"001M000000plRQoIAM"|0x658b3ad4
11:29:45.568 (568150321)|HEAP_ALLOCATE|[12]|Bytes:5
11:29:45.568 (568168171)|VARIABLE_ASSIGNMENT|[12]|a|null|
11:29:45.568 (568174165)|STATEMENT_EXECUTE|[15]
11:29:45.568 (568180439)|HEAP_ALLOCATE|[15]|Bytes:30
11:29:45.568 (568248977)|HEAP_ALLOCATE|[15]|Bytes:18
11:29:45.568 (568274934)|HEAP_ALLOCATE|[15]|Bytes:48
11:29:45.568 (568295149)|USER_DEBUG|[15]|DEBUG|Account ID is ==========>>>>>>001M000000plRQoIAM
11:29:45.568 (568304282)|STATEMENT_EXECUTE|[18]
11:29:45.568 (568316580)|HEAP_ALLOCATE|[18]|Bytes:4
11:29:45.568 (568428604)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
11:29:45.568 (568451570)|VARIABLE_ASSIGNMENT|[18]|this.AC_Cases|{"s":1,"v":[]}|0x658b3ad4
11:29:45.568 (568537054)|STATEMENT_EXECUTE|[20]
11:29:45.568 (568624908)|STATEMENT_EXECUTE|[21]
11:29:45.568 (568628967)|STATEMENT_EXECUTE|[24]
11:29:45.568 (568635203)|HEAP_ALLOCATE|[24]|Bytes:104
11:29:45.568 (568645762)|HEAP_ALLOCATE|[24]|Bytes:4
11:29:45.569 (569007245)|SOQL_EXECUTE_BEGIN|[24]|Aggregations:0|SELECT ID, CaseNumber, OwnerID, RecordTypeID FROM case WHERE (OwnerID = :tmpVar1 AND Status != 'Closed')
11:29:45.576 (576572115)|SOQL_EXECUTE_END|[24]|Rows:0
11:29:45.576 (576594657)|HEAP_ALLOCATE|[24]|Bytes:4
11:29:45.576 (576608340)|HEAP_ALLOCATE|[24]|Bytes:0
11:29:45.576 (576637261)|HEAP_ALLOCATE|[24]|Bytes:4
11:29:45.576 (576646776)|VARIABLE_SCOPE_BEGIN|[24]|cs|List<Case>|true|false
11:29:45.576 (576672651)|VARIABLE_ASSIGNMENT|[24]|cs|{"s":1,"v":[]}|0x6ef4e51
11:29:45.576 (576679418)|STATEMENT_EXECUTE|[25]
11:29:45.576 (576686570)|HEAP_ALLOCATE|[25]|Bytes:30
11:29:45.576 (576740442)|HEAP_ALLOCATE|[25]|Bytes:2
11:29:45.576 (576768059)|HEAP_ALLOCATE|[25]|Bytes:32
11:29:45.576 (576785654)|USER_DEBUG|[25]|DEBUG|List of cases ==========>>>>>>()
11:29:45.576 (576965446)|HEAP_ALLOCATE|[26]|Bytes:5
11:29:45.576 (576989534)|VARIABLE_ASSIGNMENT|[26]|css|null|
11:29:45.576 (576995177)|STATEMENT_EXECUTE|[26]
11:29:45.576 (576999657)|STATEMENT_EXECUTE|[30]
11:29:45.577 (577005424)|HEAP_ALLOCATE|[30]|Bytes:41
11:29:45.577 (577060414)|HEAP_ALLOCATE|[30]|Bytes:2
11:29:45.577 (577081894)|HEAP_ALLOCATE|[30]|Bytes:43
11:29:45.577 (577096166)|USER_DEBUG|[30]|DEBUG|Total Case Owner Updates ==========>>>>>>()
11:29:45.577 (577104779)|STATEMENT_EXECUTE|[31]
11:29:45.577 (577198789)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
11:29:45.577 (577224531)|CUMULATIVE_LIMIT_USAGE
11:29:45.577 (577224531)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 14 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

11:29:45.577 (577224531)|LIMIT_USAGE_FOR_NS|SFSSDupeCatcher|
  Number of SOQL queries: 4 out of 100
  Number of query rows: 2 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

11:29:45.577 (577224531)|CUMULATIVE_LIMIT_USAGE_END

11:29:45.577 (577365537)|CODE_UNIT_FINISHED|UpdateCaseOwner on Account trigger event AfterUpdate for [001M000000plRQo]
11:29:45.581 (581390788)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Account
11:29:45.593 (593252577)|WF_RULE_EVAL_BEGIN|Workflow
11:29:45.593 (593300676)|WF_CRITERIA_BEGIN|[Account: My Client!!!** 001M000000plRQo]|Converted Client Account Flow|01QD0000000QQqq|ON_CREATE_OR_TRIGGERING_UPDATE|0
11:29:45.602 (602362178)|WF_RULE_NOT_EVALUATED
11:29:45.602 (602384045)|WF_CRITERIA_BEGIN|[Account: My Client!!!** 001M000000plRQo]|Converted Client Account|01QD0000000QQpx|ON_CREATE_ONLY|0
11:29:45.602 (602392820)|WF_RULE_NOT_EVALUATED
11:29:45.602 (602444364)|WF_SPOOL_ACTION_BEGIN|Workflow
11:29:45.602 (602454721)|WF_ACTION| None
11:29:45.602 (602457983)|WF_RULE_EVAL_END
11:29:45.602 (602471026)|WF_TIME_TRIGGERS_BEGIN
11:29:45.608 (608582907)|WF_ACTIONS_END| None
11:29:45.608 (608598195)|CODE_UNIT_FINISHED|Workflow:Account
Best Answer chosen by Kumar Rao 1
ManojjenaManojjena
Try with below code and let me know ,
 
Trigger UpdateCaseOwner on Account(After Update){
//Get Record typeID for Account & 
    ID AC_RecTypeID_Client    = Account.sObjectType.getDescribe().getRecordTypeInfosByName().get('Client').getRecordTypeId();
	ID AC_RecTypeID_PAclient  = Account.sObjectType.getDescribe().getRecordTypeInfosByName().get('PA Client').getRecordTypeId();
	ID Case_RecTypeID         =Case.sObjectType.getDescribe().getRecordTypeInfosByName().get('Client Review').getRecordTypeId();
	
	Map<Id,Account> accMap=new Map<Id,Account>();
    for(account acc:Trigger.new){
        if(acc.OwnerId != Trigger.oldMap.get(acc.id).OwnerId){
			if(acc.RecordTypeId== AC_RecTypeID_Client || acc.RecordTypeId ==AC_RecTypeID_PAclient){
			   accMap.put(acc.id,acc);
			}
	    } 
    }
    if(!accMap.isEmpty()){
	  List<Case> caseListToUpdate=new List<Case>();
	  for(Case cs : [SELECT id,OwnerId,AccountId FROM Case WHERE AccountId IN :accMap.keySet() AND Status != Closed AND RecordTypeId=: Case_RecTypeID]){
	     cs.OwnerId=accMap.get(cs.AccountId).OwnerId;
		 caseListToUpdate.add(cs);
	  }
	  if(!caseListToUpdate.isEmpty()){
	    try{
		  update caseListToUpdate;
		}catch(DmlException de){
		  System.debug(de);
		}
	  }
	}
}

 

All Answers

logontokartiklogontokartik
Well. The trigger is getting fired from what I see in the Debug Log. Its just not meeting the criteria for your conditions in the trigger. You can see your debug statements.

Please check your criteria and make sure if its what you need
 
ManojjenaManojjena
Hi Kumar,

Can you explain your requirment properly so that I will write teh trigger for you .
You trigger has some issue ,basicaly it is not bulkified .

 
Kumar Rao 1Kumar Rao 1
Hello Manoj/Karthik,

Update Case Owner of an Account when following conditions meet:

1. When Th Account Owner Changed to new owner
2.When Account Record Type is either Client or PA Client Only
3.Case Status is Not CLOSED
4.case Record Type is Client Review
ManojjenaManojjena
Hi Kumar ,

If this condition met in Account and related Case then you need to update account owner with case Owner ?
 
Kumar Rao 1Kumar Rao 1

Hi Manoj,

No it's the other way around. When A/C owner changes, cases which are on the name of (OwnerID) old owner must moved to the new owner. The change manually makes in A/C, then case owner should be changed by trigger.

Thanks,Srujan

ManojjenaManojjena
Hi Kumar,
Try with below code it wil help !!
Trigger UpdateCaseOwner on Account(After Update){
//Get Record typeID for Account & Case
    ID AC_RecTypeID_Client    = Schema.SObjectType.Account.GetRecordTypeInfosByName().Get('Client').GetRecordTypeID();
    ID AC_RecTypeID_PAclient  = Schema.SObjectType.Account.GetRecordTypeInfosByName().get('PA Client').GetRecordTypeID();
    ID Case_RecTypeID         = Schema.SObjectType.Case.GetRecordTypeInfosByName().Get('Client Review').getRecordTypeID();
	Map<Id.Account> accMap=new Map<Id,Account>();
    for(account acc:Trigger.new){
        if(acc.OwnerId != Trigger.oldMap.get(acc.id).OwnerId){
			if(acc.RecordTypeId== AC_RecTypeID_Client || acc.RecordTypeId ==AC_RecTypeID_PAclient){
			   accMap.put(acc.id,Account);
			}
	    } 
    }
    if(!accMap.isEmpty()){
	  List<Case> caseListToUpdate=new List<Case>();
	  for(Case cs : [SELECT id,OwnerId FROM Case WHERE AccountId IN :accMap.keySet() AND Status != Closed AND RecordTypeId=: Case_RecTypeID]){
	     cs.OwnerId=accMap.get(cs.AccountId).OwnerId;
		 caseListToUpdate.add(cs);
	  }
	  if(!caseListToUpdate.isEmpty()){
	    try{
		  update caseListToUpdate;
		}catch(DmlException de){
		  System.debug(de);
		}
	  }
	}
}

Let me know if it helps !!
Thanks 
Manoj
Kumar Rao 1Kumar Rao 1
Error: Compile Error: map must have exactly 2 type arguments at line 1 column 1
ManojjenaManojjena
Hi Kumar ,

Check belwo code!!
Trigger UpdateCaseOwner on Account(After Update){
//Get Record typeID for Account & Case
    ID AC_RecTypeID_Client    = Schema.SObjectType.Account.GetRecordTypeInfosByName().Get('Client').GetRecordTypeID();
    ID AC_RecTypeID_PAclient  = Schema.SObjectType.Account.GetRecordTypeInfosByName().get('PA Client').GetRecordTypeID();
    ID Case_RecTypeID         = Schema.SObjectType.Case.GetRecordTypeInfosByName().Get('Client Review').getRecordTypeID();
	Map<Id.Account> accMap=new Map<Id,Account>();
    for(account acc:Trigger.new){
        if(acc.OwnerId != Trigger.oldMap.get(acc.id).OwnerId){
			if(acc.RecordTypeId== AC_RecTypeID_Client || acc.RecordTypeId ==AC_RecTypeID_PAclient){
			   accMap.put(acc.id,acc);
			}
	    } 
    }
    if(!accMap.isEmpty()){
	  List<Case> caseListToUpdate=new List<Case>();
	  for(Case cs : [SELECT id,OwnerId FROM Case WHERE AccountId IN :accMap.keySet() AND Status != Closed AND RecordTypeId=: Case_RecTypeID]){
	     cs.OwnerId=accMap.get(cs.AccountId).OwnerId;
		 caseListToUpdate.add(cs);
	  }
	  if(!caseListToUpdate.isEmpty()){
	    try{
		  update caseListToUpdate;
		}catch(DmlException de){
		  System.debug(de);
		}
	  }
	}
}

 
Kumar Rao 1Kumar Rao 1
Still Same error manjo.
Kumar Rao 1Kumar Rao 1
Hi Manoj,

Error Sorted but When I change to Account Owner, case Owner is not updating. Still same error.
ManojjenaManojjena
Try with below code and let me know ,
 
Trigger UpdateCaseOwner on Account(After Update){
//Get Record typeID for Account & 
    ID AC_RecTypeID_Client    = Account.sObjectType.getDescribe().getRecordTypeInfosByName().get('Client').getRecordTypeId();
	ID AC_RecTypeID_PAclient  = Account.sObjectType.getDescribe().getRecordTypeInfosByName().get('PA Client').getRecordTypeId();
	ID Case_RecTypeID         =Case.sObjectType.getDescribe().getRecordTypeInfosByName().get('Client Review').getRecordTypeId();
	
	Map<Id,Account> accMap=new Map<Id,Account>();
    for(account acc:Trigger.new){
        if(acc.OwnerId != Trigger.oldMap.get(acc.id).OwnerId){
			if(acc.RecordTypeId== AC_RecTypeID_Client || acc.RecordTypeId ==AC_RecTypeID_PAclient){
			   accMap.put(acc.id,acc);
			}
	    } 
    }
    if(!accMap.isEmpty()){
	  List<Case> caseListToUpdate=new List<Case>();
	  for(Case cs : [SELECT id,OwnerId,AccountId FROM Case WHERE AccountId IN :accMap.keySet() AND Status != Closed AND RecordTypeId=: Case_RecTypeID]){
	     cs.OwnerId=accMap.get(cs.AccountId).OwnerId;
		 caseListToUpdate.add(cs);
	  }
	  if(!caseListToUpdate.isEmpty()){
	    try{
		  update caseListToUpdate;
		}catch(DmlException de){
		  System.debug(de);
		}
	  }
	}
}

 
This was selected as the best answer
Kumar Rao 1Kumar Rao 1
Yeah, It's working.. Thanks a lot :)