You need to sign in to do that
Don't have an account?
Need Help: Custom button executing javascript not working
Hi Guys,
I need a little help. Opportunity Products is a related list on opportunities. I have created a custom button (executing javascript) on Opportunity products. The purpose of button is to update a checkbox (trafficked__c). If the display type of the button is DETAIL PAGE BUTTON , the button is working fine. But the requirement is to have button in the related list and when the user clicks the button it can check the checkbox on all the related lists records. So for this , I chenge the display type to LIST BUTTON but it is not updating the checkbbox on all the records. It is not even giving error. Below is the javascript. Any help will be highly appreciated.
{!REQUIRESCRIPT('/soap/ajax/28.0/connection.js')}
try{
var oli = new sforce.SObject('OpportunityLineItem');
oli.Id = '{!OpportunityLineItem.Id}'; //Setting the ID of the Current Opportunity Product Record
oli.Trafficked__c = true; //Setting the Trafficked__c status to true
var resultoli = sforce.connection.update([oli]); //Updating the Opportunity Line itmen Record
if(resultoli[0].success=='true'){
alert('The checkbox has been Updated Successfully.');
location.reload();
}
}
catch(e){
alert('An Error has Occured. Error: ' + e);
}
hi,
I have written for delete you can change the code for udapte
{!REQUIRESCRIPT("/soap/ajax/9.0/connection.js")}
var records = {!GETRECORDIDS($ObjectType.Draft_PO_Line__c)};
//records = records.concat(taskRecords);
if (records[0] == null) {
alert("Please select at least one record.") }
else {
var r=confirm("Are you sure?")
if (r==true)
{
var errors = [];
var result = sforce.connection.deleteIds(records);
if (result && result.length){
var numFailed = 0;
var numSucceeded = 0;
for (var i = 0; i < result.length; i++){
var res = result[i];
if (res && res.success == 'true'){
numSucceeded++;
} else {
var es = res.getArray("errors");
if (es.length > 0) {
errors.push(es[0].message);
}
numFailed++;
}
}
if (numFailed > 0){
alert("Failed: " + numFailed + "\nSucceeded: " + numSucceeded + " \n Due to: " + errors.join("\n"));
} else {
alert("Number of records deleted: " + numSucceeded);
}
}
window.location.reload();
}
}
All Answers
hi,
I have written for delete you can change the code for udapte
{!REQUIRESCRIPT("/soap/ajax/9.0/connection.js")}
var records = {!GETRECORDIDS($ObjectType.Draft_PO_Line__c)};
//records = records.concat(taskRecords);
if (records[0] == null) {
alert("Please select at least one record.") }
else {
var r=confirm("Are you sure?")
if (r==true)
{
var errors = [];
var result = sforce.connection.deleteIds(records);
if (result && result.length){
var numFailed = 0;
var numSucceeded = 0;
for (var i = 0; i < result.length; i++){
var res = result[i];
if (res && res.success == 'true'){
numSucceeded++;
} else {
var es = res.getArray("errors");
if (es.length > 0) {
errors.push(es[0].message);
}
numFailed++;
}
}
if (numFailed > 0){
alert("Failed: " + numFailed + "\nSucceeded: " + numSucceeded + " \n Due to: " + errors.join("\n"));
} else {
alert("Number of records deleted: " + numSucceeded);
}
}
window.location.reload();
}
}
Hi Mahim,
Thanks for the reply. I made same changes but it worked. Thanks for your help.:)
Hi Mahim ,
I need liitle more help if you can.
I am trying to create a custom button executing javascript. This button is same like DELETE button . it will also delete records only when certain checkbox is NOT checked off. If that checkbox is checked off , it should not delete records rather it should give an alert message. Below is the javascript. But Everytime , I click the button on record , I get this error: Please help me in fixing the code.
A problem with the OnClick JavaScript for this button or link was encountered:
Unexpected token '{'
Javascript:
{!REQUIRESCRIPT('/soap/ajax/28.0/connection.js')}
var oppty = new sforce.SObject('OpportunityLineItem');
oppty.Id = '{!OpportunityLineItem.Id}';
If ( oppty.Trafficked__c = 'False'){
var resultoppty = sforce.connection.delete([oppty]);
}
else
{
alert('cannot delete.please retry again');
location.reload();
}