1

I have a custom detail page button to run OnClick Javascript on custom object Form_Requests_abv__c. On this button click it should do two operations. 1. Create a new Sample_Order_Form_abv__c record with data cloned from Form_Requests_abv__c object record currently opened.

  1. Open a Visualforce page with Sample_Order_form_abv__c record just created.

Error I am facing enter image description here

Javascript Code

{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 
//identify parent record
var FormR= new sforce.SObject("Form_Requests_abv__c");
FormR.id = "{!Form_Requests_abv__c.Id }";
FormR.name = "{!Form_Requests_abv__c.Name}";

// insert Sample Form transactions
var SampleFormTrans= new sforce.SObject("Sample_Order_Form_abv__c");

SampleFormTrans.Account_abv__c = '{!Form_Requests_abv__c.Account_abv__c}';
SampleFormTrans.Address_abv__c= '{!Form_Requests_abv__c.Address_abv__c}';
SampleFormTrans.Form_Request_abv__c= '{!Form_Requests_abv__c.Id}';
SampleFormTrans.OwnerID= '{!Form_Requests_abv__c.OwnerId}';
SampleFormTrans.Quantity_abv__c= '{!Form_Requests_abv__c.Quantity_abv__c}';
//SampleFormTrans.Product_abv__c= '{!Form_Requests_abv__c.Product_ID_abv__c}';
SampleFormTrans.Form_template_abv__c='{!Form_Requests_abv__c.Form_Template_Name_abv__c}';

//get query
var query = "SELECT  Account_abv__c ,Address_abv__c,Form_Request_abv__c, OwnerID, Quantity_abv__c, Product_abv__c, Form_template_abv__c, Id FROM Sample_Order_Form_abv__c WHERE Form_Request_abv__c= '" + Form_Requests_abv__c.id + "'"; 
var records = sforce.connection.query(query); 
//alert(records);
var records1 = records.getArray('records');

if(records1[0]==null)
    {alert('New record to insert');
var result = sforce.connection.insert([SampleFormTrans]);}
if(result[0].getBoolean("success"))
{
   alert('Sample Form Transaction inserted successfully');
   window.location.reload();
}
else{
  alert('Error : '+result);
}

var formId = '{!Sample_Order_Form_abv__c.Id}'; 
var expired = "{!Sample_Order_Form_abv__c.Expired_abv__c}"; 
var templateId = '{!Sample_Order_Form_abv__c.Form_template_Id_abv__c}'; 
if(expired == '1'){ 
alert("Form expired. Print option not available."); 
}else{ window.open('/apex/FormTemplatePrinter?ids='+formId+'&TemplateId='+templateId,'_blank'); 
}
2
  • What problem are you facing? Commented Feb 23, 2016 at 7:23
  • @ShaileshPatil Hi, Please find the screenshot of error. Commented Feb 23, 2016 at 7:25

2 Answers 2

2

Looks like in your javascript you've missed {! } when referencing Form_Requests_abv__c.id

Try this one:

var query = "SELECT  Account_abv__c ,Address_abv__c,Form_Request_abv__c, 
OwnerID, Quantity_abv__c, Product_abv__c, Form_template_abv__c, Id FROM
Sample_Order_Form_abv__c WHERE Form_Request_abv__c= '" + {!Form_Requests_abv__c.Id} + "'"; 
4
  • Now it gives 'Unexpected token ILLEGAL'. From my code am I able to fetch the Form_Requests_abv__c object's ID from the current detail page URL? I am not getting it very clear. Commented Feb 23, 2016 at 7:38
  • Yes you are able to fetch Id. I`m not sure why is that now. Try to find the line which gives that error. Commented Feb 23, 2016 at 7:44
  • Please let me know where I am doing wrong? Commented Feb 23, 2016 at 7:51
  • I`m not sure what you are doing wrong now. You might want check this post stackoverflow.com/questions/12719859/… for more details on that error. Commented Feb 23, 2016 at 7:57
0

The reason was query is too long so it was getting line breaks. Below code bypasses the error. Javascript Code

{!REQUIRESCRIPT("/soap/ajax/33.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/33.0/apex.js")} 
//identify parent record
alert('New record to insert');
var FormR= new sforce.SObject("Form_Requests_abv__c");
FormR.id = '{!Form_Requests_abv__c.Id }';
FormR.name = '{!Form_Requests_abv__c.Name}';


// insert Sample Form transactions
var SampleFormTrans= new sforce.SObject("Sample_Order_Form_abv__c");

SampleFormTrans.Account_abv__c = '{!Form_Requests_abv__c.Account_abv__c}';
SampleFormTrans.Address_abv__c= '{!Form_Requests_abv__c.Address_abv__c}';
SampleFormTrans.Form_Request_abv__c= '{!Form_Requests_abv__c.Id}';
SampleFormTrans.OwnerID= '{!Form_Requests_abv__c.OwnerId}';
SampleFormTrans.Quantity_abv__c= '{!Form_Requests_abv__c.Quantity_abv__c}';
//SampleFormTrans.Product_abv__c= '{!Form_Requests_abv__c.Product_ID_abv__c}';
SampleFormTrans.Form_template_abv__c='{!Form_Requests_abv__c.Form_Template_Name_abv__c}';

//get query
var query = "SELECT  Account_abv__c ,Address_abv__c,Form_Request_abv__c, "+
"OwnerID, Quantity_abv__c, Product_abv__c, Form_template_abv__c, Id FROM "+
"Sample_Order_Form_abv__c WHERE Form_Request_abv__c=\'{!Form_Requests_abv__c.Id}\'"; 
var records = sforce.connection.query(query); 
//alert(records);
var records1 = records.getArray('records');

if(records1[0]==null)
    {alert('New record to insert');
var result = sforce.connection.create([SampleFormTrans]);}
if(result[0].getBoolean("success"))
{
   alert('Sample Form Transaction inserted successfully');
   window.location.reload();
}
else{
  alert('Error : '+result);
}

var formId = '{!Sample_Order_Form_abv__c.Id}'; 
var expired = '{!Sample_Order_Form_abv__c.Expired_abv__c}'; 
var templateId = '{!Sample_Order_Form_abv__c.Form_template_Id_abv__c}'; 
if(expired == '1'){ 
alert("Form expired. Print option not available."); 
}else{ window.open('/apex/FormTemplatePrinter?ids='+formId+'&TemplateId='+templateId,'_blank'); 
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .