You need to sign in to do that
Don't have an account?
Brian Koblenz
copy object from Id in custom button
I am creating a custom button associated with a related list. The purpose of the button is to take a set of items in the related list associated with parent object P, copy them and associate the copies with parent object P1. I would like the copies to have all the data of the original object, but have a new parent. Furthermore, I would like this button to continue to work without modification if new fields are added to the object.
I (naively) assumed that the SObject constructor would take an Id for an object and create a copy. I also understand through a /id/e?clone=1 I could close a list of objects, but I assume they would be tied to object P which is undesirable. In terms of created dates etc I dont care if the field is copied or set anew.
I have the rough button code below. The area surrounded by **clone start** and **clone end** is where I am struggling.
On a more minor note, is there a way that my boolean check Propagated__c can be directly tested rather than comparing against the string "false" which must be coming from the query?
thanks
-brian
var idArray = {!GETRECORDIDS($ObjectType.HOMEtracker__Improvements_Adjustments__c)};
if (idArray.length == 0) {
alert('Please select at least one record');
}
else if (false) {
alert('Improvement->Service File->Buyer field must be filled in prior to propagation.');
}
else {
var updated_records = [];
var new_records = [];
var num_updated = 0;
var improvs = sforce.connection.retrieve(
'Id,Propagated__c,Closure_Date__c,Current_Depreciation__c',
'HOMEtracker__Improvements_Adjustments__c', idArray);
for(var i=0; i<idArray.length; i++) {
if (improvs[i].Propagated__c == 'false' &&
improvs[i].Closure_Date__c == null) {
num_updated++;
**clone start**
var i_copy = clone the object with id==idArray[i]
i_copy.Parent__c = P1; (this would be the reparenting step.)
new_records.push(i_copy);
**clone end**
var improv = new sforce.SObject(
"HOMEtracker__Improvements_Adjustments__c");
improv.id = idArray[i];
improv.Propagated__c = true;
updated_records.push(improv);
}
}
result = sforce.connection.update(updated_records);
result = sforce.connection.create(new_records);
alert('Number of copied records: '+num_updated);
window.location.reload();
}
I (naively) assumed that the SObject constructor would take an Id for an object and create a copy. I also understand through a /id/e?clone=1 I could close a list of objects, but I assume they would be tied to object P which is undesirable. In terms of created dates etc I dont care if the field is copied or set anew.
I have the rough button code below. The area surrounded by **clone start** and **clone end** is where I am struggling.
On a more minor note, is there a way that my boolean check Propagated__c can be directly tested rather than comparing against the string "false" which must be coming from the query?
thanks
-brian
var idArray = {!GETRECORDIDS($ObjectType.HOMEtracker__Improvements_Adjustments__c)};
if (idArray.length == 0) {
alert('Please select at least one record');
}
else if (false) {
alert('Improvement->Service File->Buyer field must be filled in prior to propagation.');
}
else {
var updated_records = [];
var new_records = [];
var num_updated = 0;
var improvs = sforce.connection.retrieve(
'Id,Propagated__c,Closure_Date__c,Current_Depreciation__c',
'HOMEtracker__Improvements_Adjustments__c', idArray);
for(var i=0; i<idArray.length; i++) {
if (improvs[i].Propagated__c == 'false' &&
improvs[i].Closure_Date__c == null) {
num_updated++;
**clone start**
var i_copy = clone the object with id==idArray[i]
i_copy.Parent__c = P1; (this would be the reparenting step.)
new_records.push(i_copy);
**clone end**
var improv = new sforce.SObject(
"HOMEtracker__Improvements_Adjustments__c");
improv.id = idArray[i];
improv.Propagated__c = true;
updated_records.push(improv);
}
}
result = sforce.connection.update(updated_records);
result = sforce.connection.create(new_records);
alert('Number of copied records: '+num_updated);
window.location.reload();
}
Not sure if I got it right but if you are looking for a clone button that clones the parent and its children you can try the approach bellow.
First of all, you have to create a class with a webservice method that will be invoked by a custom button in the parent's object layout that will clone the parent and its children:
After that, you will have to create a custom javascript button and place it at the parent's layout. Once clicked, the button will invoke the clone method and will clone the desired sObject.
Button's code:
This is one way to achieve what you are looking for. Keep in mind that you can change it as much as you want.
Hope to have helped. Regards.
Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
All Answers
Not sure if I got it right but if you are looking for a clone button that clones the parent and its children you can try the approach bellow.
First of all, you have to create a class with a webservice method that will be invoked by a custom button in the parent's object layout that will clone the parent and its children:
After that, you will have to create a custom javascript button and place it at the parent's layout. Once clicked, the button will invoke the clone method and will clone the desired sObject.
Button's code:
This is one way to achieve what you are looking for. Keep in mind that you can change it as much as you want.
Hope to have helped. Regards.
Don't forget to mark your thread as 'SOLVED' with the answer that best helps you.
I did figure out how to clone (manually) and get the reparenting by passing a magic id referring to the object's field id to the clone routine. So, the following string when entered in a browser and then clicking save actually creates the appropriate behavior. The _lkid is a hidden id name that allows the id to be set for a lookup field.
var clone_str = '/'+ ids_to_clone + '/e?clone=1' +
'&CF00Nd0000004Oten_lkid=a0fd000000EZODL' + // magic to set service_file__c
'&00Nd0000004Otiq=3'; // magic to set value__c to 3
alert('clone string: '+ clone_str);
window.location.href = clone_str;
Unfortunately, the code when execute in the custom button does not perform the clone. I really dont know how to debug that. Any help?
Also, I still have the problem of getting the value to assign to CF00Nd0000004Oten_lkid. That is a lookup field on found on P that I am having trouble referencing in my button. {!Object__c.Service_File__c} does not work when the service_file field is a lookup.
thanks
I tried to compile your apex class and get the following error:
[Error] Error: Compile Error: Invalid type: ParentSObject__c at line 13 column 14
I asked in another thread how this parentsobject__c works and now with the compile time error I am even more confused. Can you help?
thanks