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
Jyosi jyosiJyosi jyosi 

To update contat when the when case is closed

Hello Every one,

I need to update the contact when the Case is closed.
I wrote the code but its not triggering.

Below is my code

 public void UpdateRepStatus(List<Case> CompliancesurveryDetailsLists, Map<ID,Case> oldCompliancesurveryDetailsmap)
   {
      set<Id> CaseId= new Set<Id>();
      List<Contact> LstcntUpdate= new List<Contact>();
      List<Case> LstCases=new List<Case>();
      for(Case Cases:CompliancesurveryDetailsLists)
      {
        CaseId.add(Cases.Id);
        CaseId.add(Cases.Contact__c);
      }
      LstCases=[SELECT Id,Status,Contact__c FROM Case where status='closed' AND ID IN:CaseId AND Contact__c  IN: CaseId LIMIT 1];
      system.debug('LstCases====>>>'+LstCases);
      if(LstCases.size()>0)
      {
          for(Case Cases:LstCases)
          {
               contact cnt= new contact();
               cnt.Id=Cases.Contact__c;
               cnt.Rep_Status__c='Terminated';
               LstcntUpdate.add(cnt);
         }
             update LstcntUpdate;
      }

Trigger Class


    if(Trigger.isBefore && Trigger.isUpdate) {
    CasesTriggerHandler.UpdateRepStatus(Trigger.New, Trigger.oldMap);
   }

thanks for help.

Regards,
Jyo
KaranrajKaranraj
You don't have to write trigger for this scenario, you can able to do with the process builder

1. Create a process builder on the object Opportunity and enable 'Create or edited'
2. Then set the condition as 'isClosed' = true
3. Then in the immediate action, selecte 'Update Record'

User-added image

4. Then select the fields you want to update in the contact object.
5. Then activate your process builder

To know more about process builder check this link - https://developer.salesforce.com/trailhead/force_com_declarative_beginner/business_process_automation/process_builder

Thanks,
karanrajs.com
Jyosi jyosiJyosi jyosi
Buts here i need to update the Contact picklist values when the case is closed
PandeiswariPandeiswari
Please change your trigger to "After update" instead of before update. 
Jyosi jyosiJyosi jyosi
Hello  Pandeiswari,

When i change this i am getting the below Error


Review all error messages below to correct your data.
Apex trigger QP_CaseTrigger caused an unexpected exception, contact your administrator: QP_CaseTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Class.CaseshandlerClass.UpdateRepStatus: line 86, column 1

Thanks

Regards,
Jyo
KaranrajKaranraj
@Jysoi - Yeah.. It my mistake, just change instead of Opportunity object try with the Case object. I tested in my developer org. Its working perfectly, when I close the case, its updating one of the picklist field in the contact object.

Thanks,
Karanraj
http://www.karanrajs.com
Bhanu MaheshBhanu Mahesh
Hi Jyosi,

You can try below code
if(Trigger.isAfter&& Trigger.isUpdate) {
    CasesTriggerHandler.UpdateRepStatus(Trigger.New, Trigger.oldMap);
   }
 
public void UpdateRepStatus(List<Case> CompliancesurveryDetailsLists, Map<ID,Case> oldCompliancesurveryDetailsmap){
      set<Id> contactIds = new Set<Id>();
      List<Contact> lstCntUpdate= new List<Contact>();
      for(Case Cases:CompliancesurveryDetailsLists)
      {
        if(Cases.isClosed && !oldCompliancesurveryDetailsmap.get(Cases.Id).isClosed){
				contactIds.add(Cases.Contact__c);
		}
      }
      if(!contactIds.isEmpty()){
		for(Contact con :[SELECT Id,Rep_Status__c FROM Contact WHERE Id IN : contactIds]){
			con.Rep_Status__c = 'Terminated';
			lstCntUpdate.add(con);
		}
		if(!lstCntUpdate.isEmpty()){
			update lstCntUpdate;
		}
	  }
}

Regards,
Bhanu Mahesh