0

Identity is a related list in the Contact object

In my trigger I am trying to update a Contact field called PCID_Name__c with the name of an identity when the identity field EE_Identity_Type__c is equal to pcid. in this case I get the name of that identity record and put it on PCID_Name__c on the related contact record

however my trigger is not working and I get the following error, could you please help

PS: how can I do that using before update? I have tried it as well and removed the update lp but nothing happened.

    trigger PcidIdentityName on Contact (after update) {

// this trigger will check if we have a PCID Identity on the contact, if it is the case we will bring the name of 
//that identity to the current contact - Field updated in contact PCID_Name__c

list <contact>lp=new list<contact>();    


list <contact>lc=new list<contact>([select id,PCID_Name__c,(select Name from Identities__r where EE_Identity_Type__c='PCID')
                from contact where id in: trigger.new]);




for(contact c : lc )
{



    if(c.Identities__r.size()>0)
    {

        c.PCID_Name__c=c.Identities__r[0].Name;
        lp.add(c);


    }


update lp;    

}

}

ERROR

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger PcidIdentityName caused an unexpected exception, contact your administrator: PcidIdentityName: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0033C000001S7nMQAS; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, PcidIdentityName: maximum trigger depth exceeded Contact trigger event AfterUpdate for [0033C000001S7nM] Contact trigger event AfterUpdate for [0033C000001S7nM] Contact trigger event AfterUpdate for [0033C000001S7nM] Contact trigger event AfterUpdate for [0033C000001S7nM] Contact trigger event AfterUpdate for [0033C000001S7nM] Contact trigger event AfterUpdate for [0033C000001S7nM] Contact trigger event AfterUpdate for [0033C000001S7nM] Contact trigger event AfterUpdate for [0033C000001S7nM] Contact trigger event AfterUpdate for [0033C000001S7nM] Contact trigger event AfterUpdate for [0033C000001S7nM] Contact trigger event AfterUpdate for [0033C000001S7nM] Contact trigger event AfterUpdate for [0033C000001S7nM] Contact trigger event AfterUpdate for [0033C000001S7nM] Contact trigger event AfterUpdate for [0033C000001S7nM] Contact trigger event AfterUpdate for [0033C000001S7nM] Contact trigger event AfterUpdate for [0033C000001S7nM]: []: Trigger.PcidIdentityName: line 33, column 1

1 Answer 1

2

Your trigger should be on Identity, not Contact. You should also look at:

Anyway. You might just want to implement a service that updates the Contact records based on a Set<Id>.

public with sharing class ContactService
{
    public static void setMostRecentPcidName(Set<Id> contactIds)
    {
        List<Contact> contacts = [
            SELECT (SELECT Name FROM Identities__r ORDER BY CreatedDate DESC LIMIT 1)
            FROM Contact WHERE Id IN :contactIds
        ];
        try
        {
            update contacts;
        }
        catch (DmlException d)
        {
            // handle exception
            // DO NOT LEAVE THIS BLOCK EMPTY
        }
    }
}

And possibly a service that gets the Contact ids in the first place.

public with sharing class IdentityService
{
    public Set<Id> getContactIds(List<Identity__c> records)
    {
        Set<Id> contactIds = new Set<Id>();
        for (Identity__c record : records)
            contactIds.add(record.Contact__c);
        return contactIds;
    }
}

Then your trigger can look something like:

trigger Identity on Identity__c (after insert, after update, after delete, after undelete)
{
    // move this logic to a trigger handler
    if (trigger.isAfter && trigger.isInsert)
    {
        ContactService.setMostRecentPcidName(IdentityService.getContactIds(trigger.new));
    }
    if (trigger.isAfter && trigger.isUpdate)
    {
        Set<Id> contactIds = IdentityService.getContactIds(trigger.new);
        contactIds.addAll(IdentityService.getContactIds(trigger.old);
        ContactService.setMostRecentPcidName(contactIds);
    }
    if (trigger.isAfter && trigger.isDelete)
    {
        ContactService.setMostRecentPcidName(IdentityService.getContactIds(trigger.old));
    }
    if (trigger.isAfter && trigger.isUndelete)
    {
        ContactService.setMostRecentPcidName(IdentityService.getContactIds(trigger.new));
    }
}
0

You must log in to answer this question.

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