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
Srinivas223Srinivas223 

onclick javascript button not working

Hello All,

I have a requirement where i have to show an alret message when there is no opportunity line item to the opportunity and if account billing country belongs to few restricted countries. I have my restricted countries in a custom setting. Here is the sample code. 
the code is working fine when I am checking for opportunity line items but I could not compare the account billing country to the list of countries in the custom setting.

I appreciate your help.
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")} 
  

var x='{!Opportunity.Mission_Types__c}';
    if (x == '') 
        alert('Enter at least one Mission Type');
     

var myQuery = "select Id, name,product2id from opportunityLineItem where OpportunityId= '{!Opportunity.Id}' limit 1";
result = sforce.connection.query(myQuery); 
var records = result.getArray("records"); 

if(records =='')
alert('There should be at least one product '   ); // this is working fine

var oppQuery = "select AccountId from opportunity where Id= '{!Opportunity.Id}' limit 1";
oppResult = sforce.connection.query(oppQuery); 
var opp = oppResult.getArray("opp"); 
var accId = opp[0].AccountId; // here is the problem. it says cannot read the property 
                                                                                                             // accountid



var account = "select billingCountry from account where Id=: accId  limit 1";
accResult = sforce.connection.query(account); 
var acc = accResult.getArray("acc"); 
var billingCountry = acc[0].billingCountry;


var customSetting ="select id, name,CountryAbbreviation__c from SMProposalRestrictedCountries__c";
data = sforce.connection.query(customSetting); 
var customSettingData = data.getArray("data");
var restrictedCountry;
for(var i=0;i<customSettingData.length;i++){
    IF(customSettingData[i].name == billingCountry )
    restrictedCountry = true;
}
if(restrictedCountry == true)
alert('Restricted Billing country on Account');

 if (x  !=  '' && records  != '') { 
window.location.href='/apex/OpptySendEmailToSpecialMissions?id={!Opportunity.Id}';

}

 
Abdul KhatriAbdul Khatri
Please try this
 
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/32.0/apex.js")} 
  

var x='{!Opportunity.Mission_Types__c}';
    if (x == '') 
        alert('Enter at least one Mission Type');
     

var myQuery = "select Id, name,product2id from opportunityLineItem where OpportunityId= '{!Opportunity.Id}' limit 1";
result = sforce.connection.query(myQuery); 
var records = result.getArray("records"); 

if(records =='')
alert('There should be at least one product '   ); // this is working fine

var oppQuery = "select AccountId from opportunity where Id= '{!Opportunity.Id}' limit 1";
oppResult = sforce.connection.query(oppQuery); 
var opp = oppResult.getArray("records"); 
var accId = opp[0].AccountId; // here is the problem. it says cannot read the property 
                                                                                                             // accountid



var account = "select billingCountry from account where Id=: accId  limit 1";
accResult = sforce.connection.query(account); 
var acc = accResult.getArray("acc"); 
var billingCountry = acc[0].billingCountry;


var customSetting ="select id, name,CountryAbbreviation__c from SMProposalRestrictedCountries__c";
data = sforce.connection.query(customSetting); 
var customSettingData = data.getArray("data");
var restrictedCountry;
for(var i=0;i<customSettingData.length;i++){
    IF(customSettingData[i].name == billingCountry )
    restrictedCountry = true;
}
if(restrictedCountry == true)
alert('Restricted Billing country on Account');

 if (x  !=  '' && records  != '') { 
window.location.href='/apex/OpptySendEmailToSpecialMissions?id={!Opportunity.Id}';

}

 
Abdul KhatriAbdul Khatri
User-added image
Abdul KhatriAbdul Khatri
That helped?
Abdul KhatriAbdul Khatri
Hello
Abdul KhatriAbdul Khatri
Man was my last solution helpful?
Abdul KhatriAbdul Khatri
Are you really following up on this solution?
Abdul KhatriAbdul Khatri
Hello