1

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.

Exemple insert in console

map

DEBUG

  • Check the logs .Looks like all the records in CSV none of them actually match your logic .Make sure to add debug and view logs – Mohith Shrivastava Jul 30 '16 at 22:42
  • Check that you're inserting exact values in you csv. Decimal values may differ between UI and DB. Try to add setscale to decimals. – Ilya Lepesh Jul 30 '16 at 22:47
  • But if I edit and save the record it works. – Coronel Jul 30 '16 at 22:48
  • As @Mohith mentioned. Try to add system.debug and check what came to SF, what is stored in contact etc. – Ilya Lepesh Jul 30 '16 at 22:50
  • 2
    @Coronel Hi there. I've made a gist that may or may not fix your problem. If it does solve your problem, I'll be happy to add it as an answer. – sfdcfox Jul 31 '16 at 6:08
1

While using import wizard I have following logs:

DEBUG|{123.0}
{Id=0033600000MIRZOAA5, Inscricao_Numero__c=123}}

So the is no problem in selecting from contact, but we can't get values from map.

You can change key field type to String or use type conversion:

if (matchingcontactsMap.get((integer)pontos.Inscricao_Numero__c) != null)
{
    pontos.Contato__c = matchingcontactsMap.get((integer)pontos.Inscricao_Numero__c).Id;
  • Tks a lot! works! @IlyaLepesh – Coronel Jul 31 '16 at 10: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.