I have a custom object Hire_Form__c that has a contact lookup. It has the following fields:

  1. First Name
  2. Last name
  3. Email
  4. Phone
  5. Status (In Progress , Completed , Rejected)
  6. Candidate (Contact Lookup)
  7. Description

Create Trigger on Hire_form__c

  1. When hire_form__c record is insert set status = ‘In Progress’. Create new contact record with firstname , lastname , email, phone. Set contact id in Candidate field on Hire_form object. Create new case record for new created contact record. Set status in case = ‘New’.

  2. When Hire_form is updated with status = ‘Completed’ , then update the status of related case ‘Closed’. This is where I'm stuck

Create Trigger on Case

  1. When case is updated with Status = ‘Closed’ , then check if parent contact’s related hire form status is not ‘Completed’ , then show error message ‘You can not close the case until hire form is completed’.

This is my Trigger so far and I'm stuck in the 2nd part of the first trigger:

trigger HireProcessTrigger on Hire_Form__c (before insert, before update) {

    List<Contact> contList = new List<Contact>();
    List<Case> caseList = new List<Case>();
   // Map<ID, Hire_Form__c> formId = new Map<ID, Hire_Form__c>();

        for(Hire_Form__c form : Trigger.new){

            form.status__c = 'In Progress';
            Contact cont = new Contact();
            cont.FirstName = form.First_Name__c;
            cont.LastName = form.Last_Name__c;
            cont.Email = form.Email__c;
            cont.Phone = form.Phone__c;

            insert contList;

            form.Candidate__c = cont.Id;

        for(Contact con : contList){
            Case cases = new Case();
            cases.Status = 'New';
            cases.ContactId = con.Id;
            insert caseList;

        for(Hire_Form__c form : Trigger.new){



How do I change the status of the case related to this candidate? Will I have to go through the contact and then the case? I'm confused.

1 Answer 1


First, collect contact Id on hire_form__c which status is 'Complete'. Then query all case related with those contact.

  List<Case> list_case = new List<Case>();
  List<Id> list_conId = new List<Id>();
  for(Hire_Form__c form : Trigger.new){
    if(form.Status__c == 'Completed'){
  if(list_conId.size() > 0){
    for(Case caseItem:[select id,ContactId,Status from Case where ContactId in:list_conId]){
        caseItem.Status = 'Closed';

    if(list_case.size() > 0){
        update list_case;

And I suggest do not use any SOQL and DML inside for loop.

For case Trigger, you can refer to below. Since I haven't test the code in my org, so there might be some errors, you can try to fix them.

trigger CaseTrigger on Case (before insert, before update) {
    Map<Id, case> map_contactId_case = new Map<Id, case>;
    for(Case caseItem:trigger.new){
        Case oldCase = trigger.oldMap.get(caseItem.id);
        if(caseItem.Status == 'Closed' && oldCase.Status != 'Closed'){
            if(caseItem.ContactId != null){
                map_contactId_case.put(caseItem.ContactId, caseItem);
    for(Hire_Form__c form:[select id,Status__c,Candidate__c from Hire_Form__c where Candidate__c in:map_contactId_case.keySet()]){
        if(form.Status__c != 'Completed' && map_contactId_case.containsKey(form.Candidate__c)){
            case tempCase = map_contactId_case.get(form.Candidate__c);
            tempCase.addError('You can not close the case until hire form is completed');
  • This makes sense. Thank You. I was having a similar problem in the 2nd trigger, the trigger on case. Could you help me with it as well? Commented Dec 14, 2018 at 6:41
  • 1
    @vedantgupta I have updated my post.
    – Zas
    Commented Dec 14, 2018 at 13:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .