You need to sign in to do that
Don't have an account?
Jan Aertgeerts
Trigger to copy object + children to other object at specific moment in approval process
Hello
I was wondering if it is possible to create a copy of the record (with all child-records) to another object when the approval process reaches a certain point.
The case is as following: I have an Invoice object, this invoice has multiple Invoice lines (child). When someone approves the invoice for payment, a copy should be made to the object Invoice_copy (& Invoice_Lines_copy) so that there is a fixed/frozen record of the invoice. This to be certain that it has been booked / paid correctly when approved (so we have proof).
Is this possible in the way that I describe with a copy to another object, if so - can someone head me in the right direction to create this trigger?
Thanks in advance.
Kind regards
I was wondering if it is possible to create a copy of the record (with all child-records) to another object when the approval process reaches a certain point.
The case is as following: I have an Invoice object, this invoice has multiple Invoice lines (child). When someone approves the invoice for payment, a copy should be made to the object Invoice_copy (& Invoice_Lines_copy) so that there is a fixed/frozen record of the invoice. This to be certain that it has been booked / paid correctly when approved (so we have proof).
Is this possible in the way that I describe with a copy to another object, if so - can someone head me in the right direction to create this trigger?
Thanks in advance.
Kind regards
1) Instead of returning a list, return a map with key as source Id and value as new object
instead of this line targetList.add(sObj ); targetMap.put (source.get('Id'), sObj)
2) Remove the insert from the above code
3) call the sample code with parent objects which will return a map.
4) insert the parent copy using insert map.getValues()
5) call the sample code with child object which will return a map
6) iterate the return map.getValues()
7) get the relationship field from the child and get the correspoinding new parent using map and update the relatiohsip field.
8) now insert the child copy map.getValues()
Map<Id, Sobject> parentCopyList = getCreatableFieldsSOQL('parent__c', 'where id in...', 'parent_copy__c');
insert parentCopyList.getValues();
Map<Id, Sobject> childCopyList = getCreatableFieldsSOQL('child__c', 'where id in...', 'child_copy__c');
for (Sobject child : childCopyList ) {
Sobject newParent = parentCopyList.get (child.get('parent__c'));
child.put('parent__c', newParent.get('id'))
}
insert childCopyList.getValues();
also note that id field might also get copied in the getCreatableFieldsSOQL method. you need to add if to avoid that
if (field != 'id') {
sObj.put (field, source.get(field));
}
All Answers
on that object will able to edit the record.
http://sfdc.arrowpointe.com/2011/03/28/cloning-records-in-apex/
Note that you have to work/change the above code to copy the child object. when you copy the child you have to update the parent relationship field to new parent record.
Hi, many thanks for the code and effort. I can get it to work but am confused how I can iterate through the child records and relate the copies to the new parent object. Can you put me on the right track?
Kind regards
1) Instead of returning a list, return a map with key as source Id and value as new object
instead of this line targetList.add(sObj ); targetMap.put (source.get('Id'), sObj)
2) Remove the insert from the above code
3) call the sample code with parent objects which will return a map.
4) insert the parent copy using insert map.getValues()
5) call the sample code with child object which will return a map
6) iterate the return map.getValues()
7) get the relationship field from the child and get the correspoinding new parent using map and update the relatiohsip field.
8) now insert the child copy map.getValues()
Map<Id, Sobject> parentCopyList = getCreatableFieldsSOQL('parent__c', 'where id in...', 'parent_copy__c');
insert parentCopyList.getValues();
Map<Id, Sobject> childCopyList = getCreatableFieldsSOQL('child__c', 'where id in...', 'child_copy__c');
for (Sobject child : childCopyList ) {
Sobject newParent = parentCopyList.get (child.get('parent__c'));
child.put('parent__c', newParent.get('id'))
}
insert childCopyList.getValues();
also note that id field might also get copied in the getCreatableFieldsSOQL method. you need to add if to avoid that
if (field != 'id') {
sObj.put (field, source.get(field));
}