Forum Discussion
Hi there,
yes this is certainly possible. If I understand you correctly, you can achieve the functionality you need by implementing an onBeforeAdd (or similar) method on the relationship ItemType between Project and MEL (I'm assuming this is called PMEL from here on).
One thing to note before I start: I am assuming with "saving" you are referring to saving the changes on the Project after you have added the MELs to it, not the "Confirm selection" tick mark you click when selecting related MELs. Since you are not committing any relationship or property changes on the Project Item to the database before you save it, I do not believe it makes much sense to put the logic on the selection event for related MELs.
In any case, here's how I would go about implementing the logic triggered onSave of the Project (which is when the relationship items get created, hence onBeforeAdd triggers for PMEL):
- Create a method that will contain the creation logic for the PEL objects, here I will call it CreatePels
- For some thoughts on what would be going on in CreatePels, see below
- Open ItemType PMEL and lock (edit) it
- Go to the tab "Server Events"
- Search for and add CreatePels
- Set "Event" to be "onBeforeAdd"
- Set "Event Version" to "Version 1"
- Save, unlock and close ItemType PMEL
Now, when you save a project after adding MELs to it, CreatePels will trigger and create your corresponding PELs.
In CreatePels, you have access to the Project ID via this.getProperty("source_id", "") and the MEL ID via this.getProperty("related_id", ""). Via this related_id you would also obtain all the MEL attributes that you need to map to your new PEL. Just be sure not to alter "this" (which is the to-be-added relationship between Project and MEL) and to "return this" at the end of the method.
Note that for each MEL you add to the Project, CreatePels gets triggered once. So if you add three MELs to your Project, CreatePels gets triggered three times.
Another thing you could do is put the method on the Project ItemType with event onBeforeUpdate or something similar. In this case, the method would only trigger once, but you'd have to get a hold of the MELs you added to the project in a different way. I find the PMEL approach more straightforward and intuitive.
Hope this helps,
C
Thank you so much for detailed response. I will try it and let you know the result .
- cogres6 years agoCreator III
No problem [emoticon:c4563cd7d5574777a71c318021cbbcc8]. Let me know how it goes!