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
sudha76sudha76 

Javascript OnClick buton is not working as coded.

I am trying to create a OnClick JS Code, where if an Account displays the Profile records, in the related list called - Account_Technical_Profile__c, then the user should not be allowed to create another record. Only one Technical Profile per Account is allowed. They can edit the existing Technical Profile, but they cannot add one more , if it already exists.

 

Here is my code:-

 

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}

var Account = new sforce.SObject("Account");
var Account_Technical_Profile__c = new sforce.SObject("Account_Technical_Profile__c");
Account.id = "{!Account.Id}";

var qr = sforce.connection.query("Select Account__c From Account_Technical_Profile__c where Account_Technical_Profile__c='{!Account.Id}'");

var records = qr.getArray('records');
var alreadyadded = false;

if (records.length> 0)
{
for (var i=0;i<records.length;i++) {
alert ("The Account Profile for this Account already exists. Please update the existing Account Technical Profile");
alreadyadded = true;
}
else {
window.location = 'https://cs3.salesforce.com/a0f/e?CF00NQ0000000xdOp={!Account.Name}&&retURL={!Account.Id}'';
}
}

 

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

 

I am getting the "Syntax Error". What am I missing here? The logic seems to be correct, right?

 

 

 

MoUsmanMoUsman

Hi sudha76,

 

Please escape single quote from the query and try again as given below.

 

var qr = sforce.connection.query("Select Account__c From Account_Technical_Profile__c where Account_Technical_Profile__c=/'{!Account.Id}/'");

 

Somthing like this I did not run this code at my end so try it to scape quote, I believe it will work.

 

--

Thanks

Usman

sudha76sudha76

Here is the fnal piece of code that worked:-

 

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")}

var queryStr = "Select Account__c From Account_Technical_Profile__c where Account__c='{!Account.Id}'";

var result = sforce.connection.query(queryStr);

var records = result.getArray('records');

if(records != null && records.length > 0){
alert ("The Account Profile for this Account already exists. Please update the existing Account Technical Profile");
}
else{
//The below piece of code is pointing to the Account Technical Profile custom object. It displays the Account Name based
//Acct ID . The code allows the Technical Profile specific to the Account Name even though the Account has Dupes.

window.location.href = "/a0n/e?CF00N800000051qPz={!Account.Name}&CF00N800000051qPz_lkid={!Account.Id}&retURL=%2F{!Account.Id}";
}

 

thanks.