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
Brian KoblenzBrian Koblenz 

custom button implementation not behaving as expected when "calling" url

I have been struggling for a few days trying to do something I would have expected to be reasonably straightforward.  I have created a custom button, visual force page, and apex class to support my goal.

I have a custom button on a related list and my overall goal is to clone every item in the related list and then reparent the clones to another master. My approach is to find all the elements in the related list by querying all the objects of the desired type who have the master id of the parent page that invoked the button.

My first query statement does seem to return the appropriate elements.  (btw, while the code below has hardcoded the parent_id, I wanted to extract the rightmost part of the url variable to get the parent_id, but I could not seem to use the right(), find() and len() functions feeding them the url variable.  If anyone can tell me how to do that it would also be appreciated.)

Here is the code:
var url = window.parent.location.href;
alert('url: ' + url);
var parent_id = 'a0fd000000EZGwt';

var query_str = "Select Id from HOMEtracker__Improvements_Adjustments__c where HOMEtracker__Service_File__r.Id = " + "'" + parent_id + "'";
alert('query: ' + query_str);

var allimp = sforce.connection.query(query_str);
alert('len: ' + allimp.records.length);
alert('recs: ' + allimp);

window.open('/apex/clonePlus?id=a0Wd0000008I89G&clone=1','_self');
window.open('/apex/clonePlus?id=a0Wd0000008IFyH&clone=1','_self');
window.parent.location.href = url;

var allimp2 = sforce.connection.query(query_str);
alert('newlen: ' + allimp2.records.length);
alert('recs2: ' + allimp2);

The purpose of the query is to get the set of ids that I need to clone and then I was going to loop over those ideas doing:
window.open(/apex/clonePlus.....) with the appropriate id.  My code above just hardcodes the attempt to perform two clones.

My next problem is that after the completion of my script I only have one clone and it is the one from the second window.open() call.  The clone code only clones one id at a time and a possible workaround would be to pass all of the ids, but I am really trying to understand why what I am doing here does not work.

Even though I know that one clone got created, (when the page is refreshed and I am done with my button), the second query statement which I expect to now find one additional item because of the clone still returns the same result as the original query.  Since I havent gotten around to any reparenting I was counting on the second query to give me back all of the entries (including the clones) so that I could then perform updates on the original entries and the clones.

Any help with my (at least) three different issues?

thanks