+ Start a Discussion
krish@123krish@123 

Please suggest me getting the record count from contact and update the fields in contact

Hi 

Please see the below Trigger

trigger CountFemales1 on Contact (after insert,after update,after delete) {

  List<Contact> contacts = Trigger.isdelete ? trigger.old : trigger.new;
  Set<Id> accountIds = new Set<Id>();

  for (Contact c : contacts) {
     if (c.lastname!= null) {
       accountIds.add(c.id);
    } 
  }

list<Contact> FemaleList = [select id from contact where  id In :accountIds AND Customer_status__c ='buyer broker'];

 for(Contact a :FemaleList)  {
   a.No_of_Converted_Contacts__c= FemaleList .size();
 
 }
 update FemaleList ;
}

when I try edit and save the record iam getting below error

Error: Invalid Data. 
Best Answer chosen by krish@123
AbdelhakimAbdelhakim
hi,
Change this :
for(Contact a :FemaleList)  {
         a.No_of_Converted_Contacts__c= FemaleList .size(); 
}
With this :
for(Contact a :FemaleList)  {
         a.No_of_Converted_Contacts__c = integer.ValueOf(FemaleList .size()); 
}

this field "No_of_Converted_Contacts__c" msut be a Nmber.