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
harish reddy 50harish reddy 50 

Error: Compile Error: Illegal assignment from Integer to String

trigger avgtrigger on Contact (after insert,after update) {
LIST<id> ids=new LIST<id>();
integer value=0;
for(contact conn:trigger.new)
{
   ids.add(conn.accountid);
}
LIST<account> acc=[select avgrating__c,(SELECT id,rating__c from contacts where rating__c!=null)from account];
LIST<account> acc1=new LIST<account>();
for(account acc2:acc)
{
    for(contact con:acc.contact)
    {
         value=value+con.rating__c;
    
    }
  
    acc2.avgrating__c=value/acc2.contacts.size();//here its showing error
    acc1.add(acc);
}
update acc1;
}

can anyone help me..
thanks in advance. 
 
Pavit SiddhuPavit Siddhu
hello, plz check avgrating__c, rating__c field type.
 
Amit Chaudhary 8Amit Chaudhary 8
Make sure Data type of avgrating__c, rating__c should be number

Try to update your code like below
trigger avgtrigger on Contact (after insert,after update) 
{
	LIST<id> ids=new LIST<id>();
	integer value=0;
	for(contact conn:trigger.new)
	{
	   ids.add(conn.accountid);
	}
	LIST<account> acc=[select avgrating__c,(SELECT id,rating__c from contacts where rating__c!=null)from account];
	LIST<account> acc1=new LIST<account>();
	for(account acc2:acc)
	{
		for(contact con:acc.contact)
		{
			 value=value+ Integer.valueOf(con.rating__c);
		
		}
	  
		acc2.avgrating__c=Integer.valueOf( value/acc2.contacts.size()) ; //here its showing error
		acc1.add(acc);
	}
	update acc1;
}

Let us know if this will help you