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
Shweta GargShweta Garg 

window.open not working in javascript on custom button

Hi all,
I want  to open a pdf on clicking custom button in another tab. I used Window.open method in onclick javascript.It was working fine but now I also need to increment  a field also on clicking button . Here is the code sample.
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
var ID = "{!Quote.Id}"; 
var quoteobj = new sforce.SObject("Quote"); 
quoteobj.Id = '{!Quote.Id}'; 
quoteobj.ZT_PDF_Version__c = {!Quote.ZT_PDF_Version__c } + 1; 
var res = sforce.connection.update([quoteobj]); 
//window.location.reload(); 


window.location = '/apex/Generate_PDF?id='+ID;

But it is opening on the same tab. But customer wants to open a pdf in Another tab.
Window.open in not working when used with code (incrementing field).

Thanks for the help in advance.

Thanks,
Shweta
LBKLBK
This following code should work. I do have a similar Javascript based button in my Lead Page Layout.
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
var ID = "{!Quote.Id}"; 
var quoteobj = new sforce.SObject("Quote"); 
quoteobj.Id = '{!Quote.Id}'; 
quoteobj.ZT_PDF_Version__c = {!Quote.ZT_PDF_Version__c } + 1; 
var res = sforce.connection.update([quoteobj]); 
//window.location.reload(); 


window.open("/apex/Generate_PDF?id="+ID);

 
Ashish KeshariAshish Keshari
I agree with LBK - the 'window.open' instead of 'window.location' should resolve your issue. thanks
LBKLBK
Please make not of the difference in syntax between window.location and window.open.
 
window.location = '/apex/Generate_PDF?id='+ID;

window.open("/apex/Generate_PDF?id="+ID);

 
Shweta GargShweta Garg
Thanks for you reply. I had used  Window.open . but it not working.
I had commented this part of code.
/*var quoteobj = new sforce.SObject("Quote"); 
quoteobj.Id = '{!Quote.Id}'; 
quoteobj.ZT_PDF_Version__c = {!Quote.ZT_PDF_Version__c } + 1; 
var res = sforce.connection.update([quoteobj]);*/
window.open("/apex/Generate_PDF?id="+ID);

Then only window.open is working. I am not getting why is it so.
LBKLBK
It could mean that one of those commented lines of code is not executing. Because Javascript works on interpretation not compilation mode, if a line of code fails, further lines are not executed.

Can you put alert statements after every lines of this commented code and see where it breaks?

May be line 3 of the above code need to be replaced with this.
 
quoteobj.ZT_PDF_Version__c = Number('{!Quote.ZT_PDF_Version__c }') + 1
Let me know how it goes.
 
Shweta GargShweta Garg
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
var ID = "{!Quote.Id}"; 
var newRecords = []; 
var quoteobj = new sforce.SObject("Quote"); 
quoteobj.Id = '{!Quote.Id}'; 
quoteobj.ZT_PDF_Version__c = {!Quote.ZT_PDF_Version__c } + 1; 
newRecords.push(quoteobj ); 
var res = sforce.connection.update(newRecords ); 
window.open("/apex/Generate_PDF?id="+ID);
This is the code sample and it is working perfecr for me.
Thanku guys for your help.
LBKLBK
Good to know. Please mark this question as solved.