I have a trigger before insert
, update before
, but it does not work when I import a csv file
. But if I edit and save the record it works.
Any idea?
Trigger updateContatoPonto on Ponto__c (before insert, before update) {
Set <Decimal> receberInscricaoNumero = new Set <Decimal> ();
for(Ponto__c pontos : trigger.New)
{
receberInscricaoNumero.add(pontos.Inscricao_Numero__c);
system.debug(receberInscricaoNumero);
}
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> ();
for(Ponto__c pontos : trigger.New)
{
if (matchingcontactsMap.get(pontos.Inscricao_Numero__c) != null)
{
pontos.Contato__c = matchingcontactsMap.get(pontos.Inscricao_Numero__c).Id;
system.debug(matchingcontactsMap);
}
else {
pontos.Contato__c = null;
}
}
update receivablesToUpdate;
}
If I insert records in Developer Console, works too;
Example,
List<Ponto__c> testpontoList = new List<Ponto__c>();
for(integer i=122;i<125;i++){
testpontoList.add(new ponto__c(Inscricao_Numero__c = i));
}
insert testpontoList;
The inscricao_numero__c = 123
exists in contact, so the record is updated with contactid
from Contact
, but when i import the CSV file, do not update the record.