0

I have created an object with the name ‘Account contact role’ and it has following fields: Con_Status_inactive (Checkbox), Inactivated Date(Date Time), Account (Lookup), Contact (Lookup).

Create a Status_inactive field with data type checkbox in Contact object. I need to write a trigger, it should fire whenever the Status field of Contact is inactive then Status field of Account Contact Role to be changed to inactive and inactivated date field should be updated with the date on which the contact is inactivated.

I have just started learning apex, I am unable to to this. I have tried the below code, there are no errors but fields are not getting updated.

trigger acr_statusupdate on contact(after insert,after update)
{     
    list<ID> ConId= New list<ID>();
   if(Trigger.isInsert && Trigger.isAfter)
   { 
    for(contact con : Trigger.new)
        {if(con.status_inactive__c == true)
            ConId.add(con.id);
        }

    }  

   if(Trigger.isUpdate && Trigger.isAfter)  
   {
    list<account_contact_role__c> acrlist = [SELECT id, con_status_inactive__c, inactivated_date__c FROM account_contact_role__c WHERE 
                                             id IN : Trigger.Newmap.Keyset()];
    for(integer i=0;i<acrlist.size();i++)
        {acrlist[i].con_status_inactive__c= true;
         acrlist[i].inactivated_date__c= system.today();
        }

   update acrlist;
    }
 }
  • What isn't working? Do you get an error? Does it update anything? Please update the question with additional information rather than adding a comment here. – Nick Cook Jul 25 '16 at 7:05
  • Where do you define arclist – Eric Jul 25 '16 at 7:08
  • do I have to define acrlist? @eric – ANUSHA SINGH Jul 25 '16 at 7:16
  • No. I just saw it was defined in the query. I missed it. What specifically is not working – Eric Jul 25 '16 at 7:20
  • i need to know what i am missing in this code, it seems nothing is happening ,no changes are made in the fields.those two objects are not linking somehow @eric – ANUSHA SINGH Jul 25 '16 at 7:25
0

Something like this should work.

trigger acr_statusupdate on contact(after insert,after update)
{     
    List<ID> ConId = new list<ID>();

    for(contact con : Trigger.new)
    {
        // get the previous value of the field.  need handle an insert/update
        Boolean previousStatusInactiveValue = Trigger.oldMap != null && Trigger.oldMap.containsKey(con.ID) ? Trigger.oldMap.get(con.ID).status_inactive__c : false;

        // only do this when the value changes
        if(con.status_inactive__c == true && previousStatusInactiveValue == false)
            ConId.add(con.id);
    }

    // Check that we have some records to update
    if(!ConId.isEmpty())
    {
        list<account_contact_role__c> acrlist = [SELECT id, con_status_inactive__c, inactivated_date__c FROM account_contact_role__c WHERE 
                                             contact__c IN :ConId];

        // loop through the results and update the fields
        for(account_contact_role__c acr : acrlist)
        {
            acr.con_status_inactive__c = true;
            acr.inactivated_date__c = system.today();
        }

        update acrlist;
    }
 }

NOTE:

  • The if(Trigger.isUpdate && Trigger.isAfter) statement isn't required because this trigger only handles them. i've replaced it with a check on whether the value has changed
  • I've assumed your lookup to Contact is called contact__c.
| improve this answer | |
  • ohk..i'll try this code...hope this works. @eric – ANUSHA SINGH Jul 25 '16 at 7:34
  • i am getting this error while saving the contact____________Apex trigger acr_statusupdate caused an unexpected exception, contact your administrator: acr_statusupdate: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.acr_statusupdate: line 9, column 1 @nick – ANUSHA SINGH Jul 25 '16 at 9:14
  • Ok, I've updated the answer accordingly. – Nick Cook Jul 25 '16 at 12:18

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.