function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Merry SMerry S 

trigger error when updating via dataloader

Hi,

 

I need some help. I have a rating object that holds a rating (rating__c), it is a child of the contact. My trigger updates a filed on the contact with the most recent rating (NPS_Score__c). From there I have a trigger on the contact that looks at all contact score and find the median and updates a field on the account (median_nps_score__c)

 

 

When I try mass updating the rating__c field I get this error:

 

 

UpdateContact: execution of AfterUpdate

 

caused by: System.DmlException: Update failed. First exception on row 0 with id 00330000000W5JJAA0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, NPSScore: execution of AfterUpdate

 

caused by: System.TypeException: Invalid integer: N/

 

Trigger.NPSScore: line 24, column 1: []

 

Trigger.UpdateContact: line 24, column 1

 


Here are my tirggers:

trigger UpdateContact on Rating__c (after insert, after update) {
Set<Id> RatingId = new Set<Id>(); 
for (Rating__c rating : Trigger.new) {
    if(rating.Rating_Type__c != null && rating.Rating_Type__c.equals('Overall')) {
        RatingId.add(rating.Id);
    }
} 
Set<Id> ContactId = new Set<Id>();
for(Rating__c rating1:[Select Contact__c from Rating__c Where Id IN:RatingId ]) {
    ContactId.add(rating1.Contact__c);
}
List<Contact> contactforupdate = new List<Contact>();
for(Contact c:[Select Id, NPS_Rating__c , 
 (Select Rating_Type__c, Rating__c From Product_Ratings__r Where Rating_Type__c = 'Overall' Order by Date__c Desc limit 1 ) 
 from Contact where Id IN:ContactId ]) {
    Rating__c Rating = c.Product_Ratings__r;        
    String RatingValue = Rating.Rating__c;
    System.debug(' ------------ ' + RatingValue);
    //if(c.NPS_Rating__c != null && c.NPS_Rating__c.equals(RatingValue)) {
        c.NPS_Rating__c = RatingValue;
        contactforupdate.add( c );
    //}
}
update contactforupdate;    
}

 

trigger NPSScore on Contact (after insert, after update) {
    //find out Contact list updated or inserted
    Set<Id> ContactIds = new Set<Id>();

    for (Contact con : Trigger.new) {
        if(con.NPS_Rating__c != null) {
            ContactIds.add(con.Id);
        }
    } 



//find out the parent Account list of Contact
Set<Id> AccountIds = new Set<Id>();
for(Contact con:[Select AccountId from Contact Where AccountId != null And Id IN:ContactIds ]) {
    AccountIds.add(con.AccountId);
}

//for each Account, calculate Median value and insert to the Account
List<Account> accountforupdate = new List<Account>();
for(Account acc:[Select Id, (Select NPS_Rating__c from Contacts where NPS_Rating__c != null ) from Account Where Id IN:AccountIds]) {
    List<Integer> contactvalues = new List<Integer>();
    for(Contact con:acc.Contacts) {
        contactvalues.add(Integer.valueOf(con.NPS_Rating__c));
    }

    Integer sizeOfList = contactvalues.size();
    Integer index = sizeOfList - 1;
    Decimal median = 0.0;

    // sort the list first
    contactvalues.sort();
    //Calculate median
    if (Math.mod(sizeOfList, 2) == 0) {
       median = (contactValues[(index-1)/2] + contactValues[(index/2)+1])/2;
    }else{
       median = contactvalues[(index+1)/2];
    }
    system.debug('the median is: '+median);
    acc.Median_NPS_Score__c = median;

    accountforupdate.add(acc);                  
}
update accountforupdate;  
}

 And this is my test

 

@isTest 
private class UpdateContactTestClass {
    static testMethod void validateUpdateContact() {
             
     Contact c = new Contact(MailingCountry='US', FirstName='Testing', LastName='NPSScore', LeadSource='cold call');
       
       // Insert Contact
       insert c;  
     
     Rating__c r = new Rating__c(Contact__c=c.id,Rating_Type__c='overall', Rating__c='8',Date__c=Date.today());
       
       // Insert Rating
       insert r;  
       
    c = [SELECT NPS_Rating__c FROM Contact WHERE Id =:c.Id];
    System.debug('Price after trigger fired: ' + c.NPS_Rating__c);   
     
     // Test that the trigger correctly updated the price
        System.assertEquals('8', c.NPS_Rating__c);  
    }
}

 Any help would be awesome. Thanks!

SharathChandraSharathChandra

Is there any similarity between Rating__c object(line 3) and in the query also u used Rating__c(line 14) in UpdateContact  trigger

SharathChandraSharathChandra

And what is Product_Rating__c object used in query(line 14) u didnt discuss about this object??????

Noam.dganiNoam.dgani

Hi 

 

Havent exactly counted lines but your problem is probably that you are trying to convert a string that is not a number (like '3') to an integer. look in your import file for a record with N/A in the NPS_Rating__c column

contactvalues.add(Integer.valueOf(con.NPS_Rating__c));