0

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++;
                }
            }
        }
    }
}

}

0

As the error says to you your field Genre__c is not in your object aggrsvar, you will need to change your code aggrsvar.Genre__c for the following aggrsvar.Song__r.Genre__c

0

This is because you cannot access Genre__c field of Song__c object from Track__c object, so you need to change your code

tempmixvar.Primary_Genre__c=aggrsvar.Genre__c;

to

tempmixvar.Primary_Genre__c=aggrsvar.Song__r.Genre__c;

and

tempmixvar.Secondary_Genre__c=aggrsvar.Genre__c;

to

tempmixvar.Secondary_Genre__c=aggrsvar.Song__r.Genre__c;

Hope this fix your issue.

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.