+ Start a Discussion
sfdc dev 2264sfdc dev 2264 

Javascript custom button click help needed

Hi ,

I need help on the following requirement below as follows,

1) I have a custom button called "Convert to contract" on proposal object
2)The proposal object is linked to parent object "opportunity"

My requirement is

There is a picklist field on opportunity called "subtype__c" 

if the opportunity picklist value related to proposal was set to "Variation" then when i click on the button it should throw me an error message

if the opportunity picklist value related to proposal is set to oher values apart from "variation" then the normal flow which is happening now should happening

Help me what changes to be made in my javascript code

Thanks in Advance
MY JAVA SCRIPT BUTTON CODE:

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

for(var i = 0; i < document.getElementsByName("convert_to_contract").length; i++) {
document.getElementsByName("convert_to_contract")[i].className = "btnDisabled";
document.getElementsByName("convert_to_contract")[i].disabled = true;
}
try{
var contrQuery = "SELECT Id, Status__c FROM Contract__c where Proposal__c=\'"+'{!Proposal__c.Id}'+"\'";

var contrRecords = sforce.connection.query(contrQuery);

var contrRecords1 = contrRecords.getArray('records');
var allowContract = true;

for(var i=0; i<contrRecords1.length; i++){
if(contrRecords1[i].Status__c != 'Rejected – Legal' && contrRecords1[i].Status__c != 'Rejected - Customer'){
allowContract = false;
}
}

if(allowContract){
var query = "SELECT Id,Name,Account__c,Commercial_TC__c,Domestic_Annual_Share__c,International_Annual_Share__c,Legal_TC__c,Opportunity__c,Contact__c,Qantas_Annual_Expenditure__c,Status__c,Valid_From_Date__c,Valid_To_Date__c,Standard__c,Additional_Change_Details__c,Deal_Type__c,MCA_Routes_Annual_Share__c,Agent_Name__c,Agent_Fax__c,Agent_Email__c,Agent_Address__c,Frequent_Flyer_Status_Upgrade__c,Frequent_Flyer_Status_Upgrade_Offer__c,Qantas_Club_Discount__c,Qantas_Club_Discount_Offer__c,TMC_Code__c,Travel_Fund__c,Travel_Fund_Amount__c,Type__c,Proposal_Start_Date__c,Proposal_End_Date__c, (SELECT Id, FareStructure__c, Comment__c, Discount__c, Discount_Threshold_AM__c, Discount_Threshold_NAM__c FROM Fare_Structure_Lists__r),(SELECT Id,Contract_Agreement__c FROM Charter_Flight_Information__r),(SELECT Id, Name FROM Attachments ORDER BY CreatedDate DESC LIMIT 1),(SELECT Id,Amount__c,Paid_As__c,Payment_Criteria__c,Payment_Frequency__c,Type__c,Proposal__c FROM Contract_Payments__r) FROM Proposal__c where Active__c=true AND Status__c=\'Accepted by Customer\' AND Id=\'"+'{!Proposal__c.Id}'+"\' limit 1";

var records = sforce.connection.query(query);

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

// Contract Creation
if(records1.length>0){
var contract = new sforce.SObject("Contract__c");

contract.Name = records1[0].Name;
contract.Account__c = records1[0].Account__c;
contract.Commercial_TC__c = records1[0].Commercial_TC__c;
contract.Contact__c = records1[0].Contact__c;
contract.Domestic_Annual_Share__c = records1[0].Domestic_Annual_Share__c;
contract.International_Annual_Share__c = records1[0].International_Annual_Share__c;
contract.Legal_TC__c = records1[0].Legal_TC__c;
contract.Opportunity__c = records1[0].Opportunity__c;
contract.Qantas_Annual_Expenditure__c = records1[0].Qantas_Annual_Expenditure__c;
contract.Proposal__c = records1[0].Id;
contract.Type__c = records1[0].Type__c;
//contract.Valid_From_Date__c = records1[0].Valid_From_Date__c;
//contract.Valid_To_Date__c = records1[0].Valid_To_Date__c;
contract.Travel_Fund__c = records1[0].Travel_Fund__c;
contract.Travel_Fund_Amount__c = records1[0].Travel_Fund_Amount__c;
contract.Standard__c = records1[0].Standard__c;
contract.Additional_Change_Details__c = records1[0].Additional_Change_Details__c;
contract.Status__c = 'Signature Required by Customer';
contract.Deal_Type__c = records1[0].Deal_Type__c;
contract.MCA_Routes_Annual_Share__c = records1[0].MCA_Routes_Annual_Share__c;
contract.Agent_Name__c = records1[0].Agent_Name__c;
contract.Agent_Fax__c = records1[0].Agent_Fax__c;
contract.Agent_Email__c = records1[0].Agent_Email__c;
contract.Agent_Address__c = records1[0].Agent_Address__c;
contract.Qantas_Club_Discount__c = records1[0].Qantas_Club_Discount__c;
contract.Frequent_Flyer_Status_Upgrade__c = records1[0].Frequent_Flyer_Status_Upgrade__c;
contract.Frequent_Flyer_Status_Upgrade_Offer__c = records1[0].Frequent_Flyer_Status_Upgrade_Offer__c;
contract.Qantas_Club_Discount__c = records1[0].Qantas_Club_Discount__c;
contract.Qantas_Club_Discount_Offer__c = records1[0].Qantas_Club_Discount_Offer__c;
contract.TMC_Code__c = records1[0].TMC_Code__c;
contract.Travel_Fund__c = records1[0].Travel_Fund__c;
contract.Travel_Fund_Amount__c = records1[0].Travel_Fund_Amount__c;
contract.Contract_Start_Date__c = records1[0].Proposal_Start_Date__c;
contract.Contract_End_Date__c = records1[0].Proposal_End_Date__c;

var rtype;

if(records1[0].Type__c == 'Qantas Business Savings'){
rtype = 'QBS - Legal/Customer Pending';
}else if(records1[0].Type__c == 'Corporate Airfares'){
rtype = 'CA - Legal/Customer Pending';
}else if(records1[0].Type__c == 'Adhoc'){
rtype = 'Charters Contract - Legal/Customer Pending';
}else if(records1[0].Type__c == 'Scheduled'){
rtype = 'Charters Contract - Legal/Customer Pending';
}

var queryC = "SELECT Id, Name FROM RecordType where Name like '%"+rtype+"%' AND sObjectType='Contract__c' limit 1";
var rtypes = sforce.connection.query(queryC);

var rtypes1 = rtypes.getArray('records');
contract.RecordTypeId = rtypes1[0].Id;

var result = sforce.connection.create([contract]);

if(result[0].success == 'true'){

var contractDiscounts = records1[0].Fare_Structure_Lists__r;
if(contractDiscounts != null){
// Discount List Creation
var conDiscounts1 = contractDiscounts.getArray("records");
var discounts = [];
for(var i=0; i<conDiscounts1.length; i++){
var discount = new sforce.SObject("Contract_Discount_List__c");
discount.Approval_Comment_NAM__c = conDiscounts1[i].Approval_Comment_NAM__c;
discount.Approval_Comment_Pricing__c = conDiscounts1[i].Approval_Comment_Pricing__c;
discount.FareStructure__c = conDiscounts1[i].FareStructure__c;
discount.Comment__c = conDiscounts1[i].Comment__c;
discount.Contract__c = result[0].id;
discount.Discount__c = conDiscounts1[i].Discount__c;
discount.Discount_Threshold_AM__c = conDiscounts1[i].Discount_Threshold_AM__c;
discount.Discount_Threshold_NAM__c = conDiscounts1[i].Discount_Threshold_NAM__c;
discounts.push(discount);
}
var result1 = sforce.connection.create(discounts);
if(result1[0].success == 'false') {
alert('Exception: '+result1[0].errors.message);
}
}
var contractPayments = records1[0].Contract_Payments__r;
if(contractPayments != null){
// contractPayment List Creation
var conPay = contractPayments.getArray("records");
var cps = [];
for(var i=0; i<conPay.length; i++){
var cp = new sforce.SObject("Contract_Payments__c");
cp.Id= conPay [i].Id;
cp.Proposal__c = records1[0].Id;
cp.Contract_Agreement__c = result[0].id;
//cp.Name = conPay[i].Name;
//cp.Amount__c = conPay[i].Amount__c;
//cp.Contract_Agreement__c = conPay[i].Contract_Agreement__c;
//cp.Paid_As__c = conPay[i].Paid_As__c;
//cp.Contract_Agreement__c = result[0].id;
//cp.Payment_Criteria__c = conPay[i].Payment_Criteria__c;
//cp.Payment_Frequency__c = conPay[i].Payment_Frequency__c;
//cp.Proposal__c = records1[0].Id;
//cp.Type__c= conPay[i].Type__c;
cps.push(cp);
}
var result1 = sforce.connection.update(cps);
if(result1[0].success == 'false') {
alert('Exception: '+result1[0].errors.message);
}
}
var charters = records1[0].Charter_Flight_Information__r;
if(charters != null){
//Charters creation
var charters1 = charters.getArray("records");

var charterValues = [];

for(var i=0; i<charters1.length; i++){
var charterValue = new sforce.SObject("Charter_Flight_Information__c");
charterValue.Id = charters1[i].Id;
charterValue.Contract_Agreement__c = result[0].id;
charterValues.push(charterValue);
}
var result2 = sforce.connection.update(charterValues);
if(result2[0].success == 'false') {
alert('Exception: '+result2[0].errors.message);
}
}

if(records1[0].Attachments != null){
var attQuery = 'SELECT Id, Name, Body FROM Attachment where ParentId=\''+'{!Proposal__c.Id }'+'\' ORDER BY CreatedDate DESC LIMIT 1';
var attRecords = sforce.connection.query(attQuery);
var attRecords1 = attRecords.getArray('records');
var newAttachment = new sforce.SObject("Attachment");
newAttachment.Body = attRecords1[0].Body;
newAttachment.Name = attRecords1[0].Name;
newAttachment.ParentId = result[0].id;
var attchResult = sforce.connection.create([newAttachment]);
if(attchResult[0].success == 'false') {
alert('Exception: '+attchResult[0].errors.message);
}
}

window.location.href = '/'+result[0].id+'?retURL='+result[0].id;

}else{
alert('Exception: '+result[0].errors.message);
}
}else if(records1.length==0){
alert("{!$Label.Convert_Contract_Error}");
}
}else{
alert('Contract exist for this Proposal');
for(var i = 0; i < document.getElementsByName("convert_to_contract").length; i++) {
document.getElementsByName("convert_to_contract")[i].className = "btnDisabled";
document.getElementsByName("convert_to_contract")[i].disabled = true;
}
}
}
catch(e){
alert('An Error has Occured. Error:' +e);
}

 
Glyn Anderson 3Glyn Anderson 3
It's hard to tell what's going on without proper indentation, but here's my attempt:

To the query on line 25, add the field "Opportunity__r.Sub_Type__c".  This will query the sub-type of the opportunity looked up to by the proposal.

Insert new code between lines 32 & 33:
if ( records1[0].Opportunity__r.Sub_Type__c == 'Variation' ) {
    alert( 'Opportunity sub-type is "Variation"' );   // or whatever message you want
    return;
}

This should do the test after you have queried and verified the existence of a proposal records, but before you create a new contract record.

Let me know if that works.
sfdc dev 2264sfdc dev 2264
Hi , I tried the below code and included the fields in the query , but it throws exception on below statement on return, "A problem with the OnClick JavaScript for this button or link was encountered: return not in function" if i remove the return then it throws error on below else "A problem with the OnClick JavaScript for this button or link was encountered: expected expression, got keyword 'else' Please help me Thanks
sfdc dev 2264sfdc dev 2264
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}

for(var i = 0; i < document.getElementsByName("convert_to_contract").length; i++) {
document.getElementsByName("convert_to_contract")[i].className = "btnDisabled";
document.getElementsByName("convert_to_contract")[i].disabled = true;
}
try{
var contrQuery = "SELECT Id, Status__c FROM Contract__c where Proposal__c=\'"+'{!Proposal__c.Id}'+"\'";

var contrRecords = sforce.connection.query(contrQuery);

var contrRecords1 = contrRecords.getArray('records');
var allowContract = true;

for(var i=0; i<contrRecords1.length; i++){
if(contrRecords1[i].Status__c != 'Rejected – Legal' && contrRecords1[i].Status__c != 'Rejected - Customer'){
allowContract = false;
}
}

if(allowContract){
var query = "SELECT Id,Opportunity__r.Type,Opportunity__r.Sub_Type_Level_1__c,Opportunity__r.Sub_Type_Level_2__c,Name,Account__c,Commercial_TC__c,Domestic_Annual_Share__c,International_Annual_Share__c,Legal_TC__c,Opportunity__c,Contact__c,Qantas_Annual_Expenditure__c,Status__c,Valid_From_Date__c,Valid_To_Date__c,Standard__c,Additional_Change_Details__c,Deal_Type__c,MCA_Routes_Annual_Share__c,Agent_Name__c,Agent_Fax__c,Agent_Email__c,Agent_Address__c,Frequent_Flyer_Status_Upgrade__c,Frequent_Flyer_Status_Upgrade_Offer__c,Qantas_Club_Discount__c,Qantas_Club_Discount_Offer__c,TMC_Code__c,Travel_Fund__c,Travel_Fund_Amount__c,Type__c,Proposal_Start_Date__c,Proposal_End_Date__c, (SELECT Id, FareStructure__c, Comment__c, Discount__c, Discount_Threshold_AM__c, Discount_Threshold_NAM__c FROM Fare_Structure_Lists__r),(SELECT Id,Contract_Agreement__c FROM Charter_Flight_Information__r),(SELECT Id, Name FROM Attachments ORDER BY CreatedDate DESC LIMIT 1),(SELECT Id,Amount__c,Paid_As__c,Payment_Criteria__c,Payment_Frequency__c,Type__c,Proposal__c FROM Contract_Payments__r) FROM Proposal__c where Active__c=true AND Status__c=\'Accepted by Customer\' AND Id=\'"+'{!Proposal__c.Id}'+"\' limit 1";

var records = sforce.connection.query(query);

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

// Contract Creation
if(records1.length>0){
var contract = new sforce.SObject("Contract__c");
if (records1[0].Opportunity__r.Type == 'Business and Government' && records1[0].Opportunity__r.Sub_Type_Level_1__c == 'Currently In Contract' && records1[0].Opportunity__r.Sub_Type_Level_2__c == 'Variation') {
    alert( 'Opportunity sub-type is "Variation"' );   
    return;
}
else{

contract.Name = records1[0].Name;
contract.Account__c = records1[0].Account__c;
contract.Commercial_TC__c = records1[0].Commercial_TC__c;
contract.Contact__c = records1[0].Contact__c;
contract.Domestic_Annual_Share__c = records1[0].Domestic_Annual_Share__c;
contract.International_Annual_Share__c = records1[0].International_Annual_Share__c;
contract.Legal_TC__c = records1[0].Legal_TC__c;
contract.Opportunity__c = records1[0].Opportunity__c;
contract.Qantas_Annual_Expenditure__c = records1[0].Qantas_Annual_Expenditure__c;
contract.Proposal__c = records1[0].Id;
contract.Type__c = records1[0].Type__c;
//contract.Valid_From_Date__c = records1[0].Valid_From_Date__c;
//contract.Valid_To_Date__c = records1[0].Valid_To_Date__c;
contract.Travel_Fund__c = records1[0].Travel_Fund__c;
contract.Travel_Fund_Amount__c = records1[0].Travel_Fund_Amount__c;
contract.Standard__c = records1[0].Standard__c;
contract.Additional_Change_Details__c = records1[0].Additional_Change_Details__c;
contract.Status__c = 'Signature Required by Customer';
contract.Deal_Type__c = records1[0].Deal_Type__c;
contract.MCA_Routes_Annual_Share__c = records1[0].MCA_Routes_Annual_Share__c;
contract.Agent_Name__c = records1[0].Agent_Name__c;
contract.Agent_Fax__c = records1[0].Agent_Fax__c;
contract.Agent_Email__c = records1[0].Agent_Email__c;
contract.Agent_Address__c = records1[0].Agent_Address__c;
contract.Qantas_Club_Discount__c = records1[0].Qantas_Club_Discount__c;
contract.Frequent_Flyer_Status_Upgrade__c = records1[0].Frequent_Flyer_Status_Upgrade__c;
contract.Frequent_Flyer_Status_Upgrade_Offer__c = records1[0].Frequent_Flyer_Status_Upgrade_Offer__c;
contract.Qantas_Club_Discount__c = records1[0].Qantas_Club_Discount__c;
contract.Qantas_Club_Discount_Offer__c = records1[0].Qantas_Club_Discount_Offer__c;
contract.TMC_Code__c = records1[0].TMC_Code__c;
contract.Travel_Fund__c = records1[0].Travel_Fund__c;
contract.Travel_Fund_Amount__c = records1[0].Travel_Fund_Amount__c;
contract.Contract_Start_Date__c = records1[0].Proposal_Start_Date__c;
contract.Contract_End_Date__c = records1[0].Proposal_End_Date__c;

var rtype;

if(records1[0].Type__c == 'Qantas Business Savings'){
rtype = 'QBS - Legal/Customer Pending';
}else if(records1[0].Type__c == 'Corporate Airfares'){
rtype = 'CA - Legal/Customer Pending';
}else if(records1[0].Type__c == 'Adhoc'){
rtype = 'Charters Contract - Legal/Customer Pending';
}else if(records1[0].Type__c == 'Scheduled'){
rtype = 'Charters Contract - Legal/Customer Pending';
}

var queryC = "SELECT Id, Name FROM RecordType where Name like '%"+rtype+"%' AND sObjectType='Contract__c' limit 1";
var rtypes = sforce.connection.query(queryC);

var rtypes1 = rtypes.getArray('records');
contract.RecordTypeId = rtypes1[0].Id;

var result = sforce.connection.create([contract]);

if(result[0].success == 'true'){

var contractDiscounts = records1[0].Fare_Structure_Lists__r;
if(contractDiscounts != null){
// Discount List Creation
var conDiscounts1 = contractDiscounts.getArray("records");
var discounts = [];
for(var i=0; i<conDiscounts1.length; i++){
var discount = new sforce.SObject("Contract_Discount_List__c");
discount.Approval_Comment_NAM__c = conDiscounts1[i].Approval_Comment_NAM__c;
discount.Approval_Comment_Pricing__c = conDiscounts1[i].Approval_Comment_Pricing__c;
discount.FareStructure__c = conDiscounts1[i].FareStructure__c;
discount.Comment__c = conDiscounts1[i].Comment__c;
discount.Contract__c = result[0].id;
discount.Discount__c = conDiscounts1[i].Discount__c;
discount.Discount_Threshold_AM__c = conDiscounts1[i].Discount_Threshold_AM__c;
discount.Discount_Threshold_NAM__c = conDiscounts1[i].Discount_Threshold_NAM__c;
discounts.push(discount);
}
var result1 = sforce.connection.create(discounts);
if(result1[0].success == 'false') {
alert('Exception: '+result1[0].errors.message);
}
}
var contractPayments = records1[0].Contract_Payments__r;
if(contractPayments != null){
// contractPayment List Creation
var conPay = contractPayments.getArray("records");
var cps = [];
for(var i=0; i<conPay.length; i++){
var cp = new sforce.SObject("Contract_Payments__c");
cp.Id= conPay [i].Id;
cp.Proposal__c = records1[0].Id;
cp.Contract_Agreement__c = result[0].id;
//cp.Name = conPay[i].Name;
//cp.Amount__c = conPay[i].Amount__c;
//cp.Contract_Agreement__c = conPay[i].Contract_Agreement__c;
//cp.Paid_As__c = conPay[i].Paid_As__c;
//cp.Contract_Agreement__c = result[0].id;
//cp.Payment_Criteria__c = conPay[i].Payment_Criteria__c;
//cp.Payment_Frequency__c = conPay[i].Payment_Frequency__c;
//cp.Proposal__c = records1[0].Id;
//cp.Type__c= conPay[i].Type__c;
cps.push(cp);
}
var result1 = sforce.connection.update(cps);
if(result1[0].success == 'false') {
alert('Exception: '+result1[0].errors.message);
}
}
var charters = records1[0].Charter_Flight_Information__r;
if(charters != null){
//Charters creation
var charters1 = charters.getArray("records");

var charterValues = [];

for(var i=0; i<charters1.length; i++){
var charterValue = new sforce.SObject("Charter_Flight_Information__c");
charterValue.Id = charters1[i].Id;
charterValue.Contract_Agreement__c = result[0].id;
charterValues.push(charterValue);
}
var result2 = sforce.connection.update(charterValues);
if(result2[0].success == 'false') {
alert('Exception: '+result2[0].errors.message);
}
}

if(records1[0].Attachments != null){
var attQuery = 'SELECT Id, Name, Body FROM Attachment where ParentId=\''+'{!Proposal__c.Id }'+'\' ORDER BY CreatedDate DESC LIMIT 1';
var attRecords = sforce.connection.query(attQuery);
var attRecords1 = attRecords.getArray('records');
var newAttachment = new sforce.SObject("Attachment");
newAttachment.Body = attRecords1[0].Body;
newAttachment.Name = attRecords1[0].Name;
newAttachment.ParentId = result[0].id;
var attchResult = sforce.connection.create([newAttachment]);
if(attchResult[0].success == 'false') {
alert('Exception: '+attchResult[0].errors.message);
}
}

window.location.href = '/'+result[0].id+'?retURL='+result[0].id;

}else{
alert('Exception: '+result[0].errors.message);
}
}else if(records1.length==0){
alert("{!$Label.Convert_Contract_Error}");
}
}else{
alert('Contract exist for this Proposal');
for(var i = 0; i < document.getElementsByName("convert_to_contract").length; i++) {
document.getElementsByName("convert_to_contract")[i].className = "btnDisabled";
document.getElementsByName("convert_to_contract")[i].disabled = true;
}
}
}
}
catch(e){
alert('An Error has Occured. Error:' +e);
}