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
AntonyWarcAntonyWarc 

Help with custom button javascript

Hi all,

I have built this cutom button javascript which works when a new record is created, but if number of offices = number of allowed offices, I get this error "cannot read property [0] of undefined after my bespoke error message has displayed and you click OK.

Basically I want to click on the button, if the number of offices is already equal to the number of allowed ofices, display the message, then refresh the page. As I said if my built in validation rule = false, then the record is created and I'm taken to the page defined as expected.

Any help appreciated.  

Antony
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
var alow = new sforce.SObject("Allowed_Offices__c");
alow.contract__c="{!Contract.Id}"
if('{!Contract.Allowed_office_count__c}' == '{!Contract.Number_of_offices__c}')
{alert('No more offices can be added to this contract');}
else{
var result = sforce.connection.create([alow]);};
if(result[0].getBoolean("success")){window.location = "/" + result[0].id;}
else{window.location.reload()
}

 
Best Answer chosen by AntonyWarc
Nayana KNayana K
Problem was with if else block .
Try this :
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
var alow = new sforce.SObject("Allowed_Offices__c");
alow.contract__c="{!Contract.Id}"
if('{!Contract.Allowed_office_count__c}' == '{!Contract.Number_of_offices__c}')
{
alert('No more offices can be added to this contract');
}
else
{
var result = sforce.connection.create([alow]);
if(result[0].getBoolean("success"))
{
window.location = "/" + result[0].id;
}
else
{
window.location.reload();
}
}

 

All Answers

Nayana KNayana K
Problem was with if else block .
Try this :
 
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
var alow = new sforce.SObject("Allowed_Offices__c");
alow.contract__c="{!Contract.Id}"
if('{!Contract.Allowed_office_count__c}' == '{!Contract.Number_of_offices__c}')
{
alert('No more offices can be added to this contract');
}
else
{
var result = sforce.connection.create([alow]);
if(result[0].getBoolean("success"))
{
window.location = "/" + result[0].id;
}
else
{
window.location.reload();
}
}

 
This was selected as the best answer
AntonyWarcAntonyWarc
Hi Nayana,

That works like a dream. I knew what the error was saying but couldn't work the logic out to fix it! Thank you so much.

​Ant