You need to sign in to do that
Don't have an account?
DBManager
Set Cloned Record's Master-Detail Parent Based on Query
I want to clone a record, and change the master-detail parent of the cloned record before inserting it.
An example:
- A and B are Item records.
- X and Y are Event/Issue records.
- The Event/Issue object is a master-detail parent of the Item object.
- X is a master-detail parent of A.
- Clone A, creating record B.
- From the date field of record X, find the Event/Issue in the next month, Y.
- Set the master-detail parent of record B to Y.
- Insert B
What I cannot seem to achieve is the query and process for step 2. I have proved steps 3 and 4 can work, by using the Id of a dummy record.
Here is my code so far:
trigger SeriesItems on Item__c (after insert) { for (integer i = 0; i < Trigger.new.size(); i++){ if (Trigger.new[i].Item_Sold__c == 'CIPI Membership' && Trigger.new[i].Cloned__c == FALSE){ for (integer n = 0; n < Trigger.new[i].Subscription_Term_Months__c; n++){ Item__c copy = Trigger.new[i].clone(false); copy.Cloned__c = TRUE;
copy.Event_Issue__c = 'a08W0000000OiVl'; insert copy; } } } }
Hope someone can help.
Fantastic - after a very small correction (ei.Date__c.monthsBetween(d)==0), your date calculation seems to be right. I checked this by printing the date, d, in a field in the clone, and it is indeed one month on.
However, the trigger as a whole did not seem to work. By eliminating one line at a time, I have found that this line was not working:
Presumably this was to do with the fact that the copy record is virtual at this stage.
Therefore, I setup a string variable to get the product from the triggering record and used it thus:
This works, but I will test further and set this as the solution if appropriate.
Thanks again, GreatG
All Answers
Firstly, as per the best practice, do not make insert calls from within the for loop. You should bulkify your trigger.
At the place where you are assigning a dummy Id, write the logic to search for the new Event which has to be assigned to the cloned Item and assign its ID over there. In MD relationship, once you have assigned a parent you cannot change it later on.
Thanks GreatG.
For your first point - how would I go about doing that? Could I add the values of 'copy' to a list, then insert the list outside of the loop, like this:
As for your second point, wouldn't that involve an SOQL query inside the for loop, which, again, is not best practice?
Thanks again.
Correct, You can add all the records in a list and outside the for loop, you can insert all the records. Also, you dont need to query records within the for loop as you can do that before you even start the loop.
You already have the information that you need to query records before the for loop hence query all the records and put them in a collection(Map) and access that collection within the for loop.
Thanks again, GreatG, this has been really helpful. What I'm struggling with is finding the right records and assigning them accordingly.
The Item record is cloned 'n' times, and for each clone, a different Event/Issue record is assigned. The Event/Issue records must have consecutive months in the 'Date' field.
For example:
Here is what I have tried, but the Event/Issue record is not updated:
Let me know if the above is not clear. Hope you can help.
Thanks again.
Made some changes, please try this:
Fantastic - after a very small correction (ei.Date__c.monthsBetween(d)==0), your date calculation seems to be right. I checked this by printing the date, d, in a field in the clone, and it is indeed one month on.
However, the trigger as a whole did not seem to work. By eliminating one line at a time, I have found that this line was not working:
Presumably this was to do with the fact that the copy record is virtual at this stage.
Therefore, I setup a string variable to get the product from the triggering record and used it thus:
This works, but I will test further and set this as the solution if appropriate.
Thanks again, GreatG