1

I need developer a Trigger to match a field in object a (Contact) with another object b (Ponto__c) and populate a relationship lookup (Contato__c).

Details

1- I have a custom object Ponto__c related to the standard object Contact via lookup field Contato__c.

I need to check if Contact.Inscricao_Numero__c = Ponto__c.Inscricao_Numero__c and update the lookup field Ponto__c.Contato__c in object Ponto__c.

I Tried this, but i am a little lost.

Trigger updateContatoPonto6 on Ponto__c (after insert, after update) {

// build a set of receivable Inscricao_Numero__c
Set <Decimal> receberInscricaoNumero = new Set <Decimal> ();

for(Ponto__c pontos : trigger.New)
{
    receberInscricaoNumero.add(pontos.Inscricao_Numero__c);
}

// Map the contacts Inscricao_Numero__c and the actual contacts records based on the receivable Inscricao_Numero__c
Map <Decimal, Contact> matchingcontactsMap = new Map <Decimal, Contact> ();

for (Contact contacts : [Select Id, Inscricao_Numero__c From Contact Where Inscricao_Numero__c IN :receberInscricaoNumero])
{
    matchingcontactsMap.put(contacts.Inscricao_Numero__c, contacts);
}

List <Ponto__c> receivablesToUpdate = new List <Ponto__c> ();

// go through the records in the trigger again and check whether we have found a matching receivable and inscricao_numero__c
for(Ponto__c pontos : trigger.New)
{
    if (matchingcontactsMap.get(pontos.Inscricao_Numero__c) == null)
    {
        // we found a mathing one
        pontos.inscricao_Numero__c = matchingcontactsMap.get(pontos.Inscricao_Numero__c).Inscricao_Numero__c;

        // add it to a separate list and update it
        receivablesToUpdate.add(pontos);
    }
} 
update receivablesToUpdate;
}

I hope have been clear, the trigger runs, but i cant update lookup field Ponto__c.Contact__C

Please someone can help?

1

You're almost there. I would start by changing the trigger from an after trigger to a before trigger since you're modifying the same records you're inserting/updating. Then there'll be no need for an explicit "update" call.

Then this line

if (matchingcontactsMap.get(pontos.Inscricao_Numero__c) == null)

is wrong because you want it to match if the key can be found in the map, i.e. your condition is non-null.

And in order to populate the Contact on the Ponto__c, you just need to do:

if (matchingcontactsMap.get(pontos.Inscricao_Numero__c) != null)
{
   // we found a matching one
   pontos.Contato__c = matchingcontactsMap.get(pontos.Inscricao_Numero__c).Id;
}

since it's the Contact's Id you want to assign.

| improve this answer | |
  • Hi George S., tks for your attention, I think I was not clear enough, I have an excel file, I will import the excel file the records of Ponto__c , the only relationship of this worksheet is a Ponto__c.inscricao_numero__c column. At the time importing files will be manually. I need to validate that Contact.Inscricao_Numero__c = Ponto__c.Inscricao_Numero__c if = true, then update the lookup Ponto__c.Contato__c field with the Contact Id, to create the relationship between Contact and the records of Ponto__c. – Coronel Jun 9 '16 at 3:28
  • That use case should be covered by my answer. The trigger will fire when you import your Pontos, and populate the matching Contacts. If this is not what you want, could you be more specific about the problem you are having? – George S. Jun 9 '16 at 3:30
  • Error: Invalid Data. Review all error messages below to correct your data. Apex trigger updateContatoPonto6 caused an unexpected exception, contact your administrator: updateContatoPonto6: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.updateContatoPonto6: line 23, column 1 23: pontos.Contato__c = matchingcontactsMap.get(pontos.Inscricao_Numero__c).Id; I need this update: Ponto__c.Contato__c = Contact.Id,,, if have the same inscricao_numero__c between Contact and Ponto__c, ok? – Coronel Jun 9 '16 at 3:37
  • As I stated in my answer, you should change the trigger to "before insert, before update" and remove the explicit update. – George S. Jun 9 '16 at 3:39
  • i.e.,,, if you create a object named Pontos__c and create 2 fields, contato__c lookup field object Contact and another field Inscricao_numero__c, and create in object Contact inscricao_numero__c, you can simulate this scenario. – Coronel Jun 9 '16 at 3:40

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.