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
KnewTVsKnewTVs 

Apex Trigger on Many to Many

Have tried all kinds of combinations and can not get this trigger to work... trigger secondupdate on Content_to_Playlist__c ( after insert, before update) { if (Trigger.isInsert) { for(Content_to_Playlist__c mycontent: Trigger.new) mycontent.ri1__Content_length_in_seconds__c = mycontent.ri1__Content__r.ri1__Length_in_Seconds__c; } if (Trigger.isUpdate) { for(Content_to_Playlist__c mycontent: Trigger.old) mycontent.ri1__Content_length_in_seconds__c = mycontent.ri1__Content__r.ri1__Length_in_Seconds__c; } }
BritishBoyinDCBritishBoyinDC

That's not going to work as desired I'm afraid...for performance reasons, related objects are not included in trigger.new or old. So mycontent.ri1__Content__r.ri1__Length_in_Seconds__c will always be null;

 

Easiest option is probably to add a cross object lookup on Content_to_Playlist__c that looks up to Content__c.ri1__Length_in_Seconds__c - because that is considered a field on the Content_to_Playlist__c object, it will be available in the trigger.

 

In the case of the second one, you can just use trigger.oldmap to retrieve the old value and overwrite the new one.

 

 

KnewTVsKnewTVs

After reading your reply I checked out the scheme and was amazed to find that the custom object Content_to_Playlist__c did not show up in the schema using SOQL explorer. In fact non of the many-to-many objects are listed. Is this typical or a bug in SOQL explorer?

 

Not sure what you mean by creating a cross object lookup on Content_to_Playlist__c that looks up to Content__c.ri1__Length_in_Seconds__c 

 

Perhaps more information will clarify my problem....

 

The Content_to_Playlist__c is a many to many that one specifies a relationship between content and playlists. The total number of seconds for each content item is desired as a roll up with out having to have the user respecify the number of seconds. The number of seconds are already specified in the content.

 

We have rollup summaries that count the number of entries in the playlist and they work fine. 

 

 

 

 

 

 

 

 

BritishBoyinDCBritishBoyinDC

So as I understand it, you have a field called ri1__Content_length_in_seconds__c on your intersection object that you need to set to the seconds value on the parent Content__c object so you can use a rollup summary field on the parent picklist object.

 

Couple of thoughts - if the seconds doesn't change on the Content__c object, you can just a workflow rule to set the seconds on insert of the intersection record - workflow rules can use formulas to set fields, so you can have a field update that references the parent content__c object on insert into the Content_to_Playlist__c object and sets the field = to the seconds field on the parent Content__c record.

 

If that isn't an option, what I am suggesting is that you add a new field to the Content_to_Playlist__c that is a formula field that displays the value stored in the seconds field on the parent Content object (i.e. a cross object lookup field)

 

Because that field is part of the Content_to_Playlist__c object, it is also then part of trigger.new/trigger.old even though it is actually referencing the parent Content object, so achieves the same function as if you could access the mycontent.ri1__Content__r.ri1__Length_in_Seconds__c field in trigger.new on the intersection object.

 

At that point, as you process the records, you can just set the ri1__Content_length_in_seconds__c field = to the new formula field on the Content_to_Playlist__c object, and now you can rollup the seconds to the playlist.

 

Ideally, you would be able to rollup a formula field, and save all this processing, but I think that's just an idea under consideration at present...

 

KnewTVsKnewTVs

Thanks for the clarificatin of using the formula. The formila is working and the code now looks like this...

 

 trigger secondupdate on Content_to_Playlist__c ( after insert, before update) { if (Trigger.isInsert) { for(Content_to_Playlist__c mycontent: Trigger.new) mycontent.ri1__Content_length_in_seconds__c = mycontent.ri1__Content_Length__c; } if (Trigger.isUpdate) { for(Content_to_Playlist__c mycontent: Trigger.old) mycontent.ri1__Content_length_in_seconds__c = mycontent.ri1__Content_Length__c; }

}

 

 I get the following errors...

 

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger ri1.secondupdate caused an unexpected exception, contact your administrator: ri1.secondupdate: execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.ri1.secondupdate: line 5, column

 

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger ri1.secondupdate caused an unexpected exception, contact your administrator: ri1.secondupdate: execution of BeforeUpdate caused by: System.FinalException: Record is read-only: Trigger.ri1.secondupdate: line 9, column 6

BritishBoyinDCBritishBoyinDC

You need to be using before insert if you want to update the record...

 

Trigger.Old is also read only collecction - what's the reason for using trigger.old instead of trigger.new?

 

If it's because need to set the value on Content_to_Playlist__c to the old value of the formula field, you need to access the old value via the trigger.oldmap, but update the field in the trigger.new collection...