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?