I have Created three Custom object. 1.Song With fields Song Name (Name field) Artist (Text field) Length (m) (Number field) Genre (Picklist field) Track Count (Number field) Track Licenses(Number field) 2.Mix With fields Mix Name (Name Field) Customer (Lookup to contact) Primary Genre (Text field) Secondary Genre (Text field) 3.Track With fields Track Auto Number(Name field) Mix (Master-Detail ) Song (Lookup to song)
Now i am first Creating Trigger on track object named "TrackTrigger". With methods 1.after insert- In this method i have to Update Primary and secondary Genre in Mix obj whenever there is entry in track object.So i have wriiten below code but stucked with error "Invalid field Genre__c for SObject AggregateResult"....... Any help is appreciated
Plz review my below code
trigger TrackTrigger on Track__c (after insert, before delete ) { if(trigger.isInsert){ Set mixIdSet = new Set(); Set songIdSet = new Set(); List aggrs; for(Track__c trackvar:Trigger.new){ mixIdSet.add(trackvar.Mix__c); songIdSet.add(trackvar.Song__c); }
Map <Id,Mix__c> mixMap = new Map <Id,Mix__c>();
Mix__c mixVar=([SELECT Id,Name,Customer__c,Primary_Genre__c,Secondary_Genre__c
FROM Mix__c
where Id IN:mixIdSet]);
mixMap.put(mixVar.id,mixVar);
Map <Id,Song__c> songMap = new Map <Id,Song__c>();
Song__c songVar=([SELECT Id,Name,Artist__c,Length_min__c,Genre__c,Track_Count__c,Track_Licenses__c
FROM Song__c
where Id IN:songIdSet]);
songMap.put(songVar.id,songVar);
aggrs=([SELECT Mix__r.Name Mixname, Song__r.Genre__c genre,COUNT(Song__r.Id)
FROM Track__c
GROUP BY Song__r.Genre__c ,Mix__r.Name
ORDER BY Mix__r.Name asc, COUNT(Song__r.Id) desc]);
for(Id idSobj :mixIdSet ){
Integer tempCount=0;
Mix__c tempmixvar=mixMap.get('Id');
for(AggregateResult aggrsvar : aggrs){
if(tempmixvar==aggrsvar.Id){
if(tempCount==0){
tempmixvar.Primary_Genre__c=aggrsvar.Genre__c;
tempCount++;
}
if(tempCount==1){
tempmixvar.Secondary_Genre__c=aggrsvar.Genre__c;
tempCount++;
}
}
}
}
}
}