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
Arun AdepuArun Adepu 

my requirement please resolve this immediately

requirement:
first take two custom objects like customer and payment.
you should create one record in customer object with sam name.
you have to create look up relationship of customer object in payment object
after that you need to create 10 records in payment but with different users
one user has 6 records and another user has 4 records.
these two users connect with only customer who's name is sam.

condition:
if:
when ever sam's record is deactivate in customer object automatically
who has more records in payment that person should be owner of that particular sam's record

using only map <id, list<user>> 

please resolve this immediately

thanks
arun
Pankaj_GanwaniPankaj_Ganwani
Hi Arun,

Please use below mentioned code:
 
Map<Id, Id> mapcustIdToMaxUser = new Map<Id,Id>();
for(AggregateResult arg : [select count(id) custCount, OwnerId, Customer__c customerId from Payment group by OwnerId, Customer__c])
{
    Id cusId = Id.valueOf(String.valueOf(arg.get('customerId'))); 
    if(!mapcustIdToMaxUser.containskey(cusId))
          mapcustIdToMaxUser.put(cusId, Integer.valueOf(arg.get('custCount'));
    else if(mapcustIdToMaxUser.get(cusId) < Integer.valueOf(arg.get('custCount'))
            mapcustIdToMaxUser.put(cusId, Integer.valueOf(arg.get('custCount'));
}