1
public class CountryrealignUpdate{
    @InvocableMethod
    public static void NewcountyUpdate(List<id> countyIds){

        Map<Id, New_Company__c> NewCompanyId = new Map<Id, New_Company__c>([select id,Alumni_Present__c,Alumni_contact_count__c from New_Company__c where id = :countyIds]);
        Map<Id, AggregateResult> AggregMap = new Map<Id, AggregateResult>([select id, count(Name) Name From Contact where Company_Realign__c = :countyIds GROUP BY id]);
        list<New_Company__c> newval = new list<New_Company__c>();

        for (Id NewCountyId : AggregMap.keySet())
        {
            newval.add(new New_Company__c(
                Id = NewCountyId, 
                Alumni_contact_count__c = AggregMap.get(NewCountyId).get('Name')
            ));
        }
        update newval;
    }
}

I am trying to assigning count of related contacts of Every New_Company__c's record.

Alumni_contact_count__c is a field in New_Company__c Object. Contact and New_Company__c is having lookup relationship.

I need to use process builder to update related New_Company__c's record whenever any changes happen in contact.

But from above code, I am getting Illegal assignment from Object to Decimal Error

  • I think you need to use Company_Realign__c instead of "Id" in AggregMap query. Because Id you currently use there is contact id. Also cast count as decimal while assigning to a field on New_Company__c – Barış Arslan Oct 8 '18 at 13:38
1

I don't think your code has been written as per your requirement. Not sure why are you querying the New_Company__c records and also you should group by the records based on the parent lookup. Check the following code where I have used the type-casting so you should not get the above error.

public class CountryrealignUpdate{
@InvocableMethod
public static void NewcountyUpdate(List<id> countyIds){

    List<New_Company__c> newval = new List<New_Company__c>();

    for (AggregateResult ar : [select count(Id) totalCount, Company_Realign__c countyId From Contact where Company_Realign__c = :countyIds GROUP BY Company_Realign__c ] ){
        newval.add(new New_Company__c(
            Id = (Id) ar.get('countyId'), 
            Alumni_contact_count__c = (Decimal) ar.get('totalCount')
        ));
    }
    update newval;
}
  • Hi Sharma, Your post helped me but It is updating the New record only. It is not updating the old record. Ex: Suppose I have New_Company__c record A and it is having 7 contacts And New_Company__c record B having 2 contacts. Now I am changing one contact from Record A to Record B (using lookup I am changing parent). Now the Record B is Having 4 contacts(Correct) But Record A is having 7 records(Wrong). Record A should also update. I mean Record A should show 6 records. – Frank Oct 8 '18 at 16:27
  • In that case, you will have to pass both parent record Ids in the countyIds parameter of the function. Not sure, if this is something possible using the process builder. – Naval Sharma Oct 9 '18 at 13:40
  • Process builder is not accepting two parameters from a single method. But I created another class and used in process builder Another action where i used a parameter in formula PRIORVALUE(Parameter). Now it is working. But do you have any idea on How many records can we update through process builder? Ex: if I update 50000 records will it work? I wondering about this – Frank Oct 9 '18 at 16:46
1

When you get from a map, the compiler only knows that the type of the value is Object rather than a more specific data type such as Decimal. You have to add code to confirm the type conversion and keep the compiler happy.

In this case, I'm not sure of the pattern that will work given that the count is an integer value. The first one to try is this simple cast:

Alumni_contact_count__c = (Decimal) AggregMap.get(NewCountyId).get('Name')

but there are also methods such as Decimal.valueOf and Integer.valueOf that can be applied.

  • System.NullPointerException: Attempt to de-reference a null object Error is coming at "newval.add(new New_Company__c( Id = NewCountyId, Alumni_contact_count__c = AggregMap.get(NewCountyId).get('Name')" – Frank Oct 8 '18 at 12:57
  • @Frank That is a different error related to your maps and map key logic. – Keith C Oct 8 '18 at 12:59

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.