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
John BraunJohn Braun 

OR condition in Onclick Javascript button

Hey Everyone,

 

I have an onclick javascript button that I'd like to require different fields to be populated before progressing to another screen.

 

I would like the button to have an or condition that considers record types. If the record typd is "Commercial", then require this set of questions. If the record type is "PDS", then require this set of questions. I'm not sure how to edit my existing code to have this condition. I also have it "bypassing" the field population requirements if the field, "existing commercial pricing quote" is set to "Yes".

 

Would anyone mind helping me understand how I would structure this code to require these fields depending on the record type? Thanks so much for any help!!!!!!

 

 

 

if ( '{!Opportunity.Existing_Commercial_Pricing_Quote__c}' == 'Yes')

{window.location = "/0Q0/e?&oppid={!Opportunity.Id}&ent=Quote&Name='Placeholder' &00NA0000004pyaC=1&ent=Quote &RecordType={!Opportunity.Quote_Record_Type__c} &retURL=/{!Opportunity.Id}"; }

 

else if( '{!Opportunity.Active__c}' == '')
{alert('In order to request a Quote, ensure that "Active" is populated on the Opportunity record.'); }

 

else if ('{!Opportunity.Rx_OTC__c}' == '')
{alert('In order to request a Quote, ensure that "Product Type" is populated on the Opportunity record.'); }

 

else if ('{!Opportunity.Project_Scope_for_Patheon__c}' == '') {alert('In order to request a Quote, you must ensure that "Project Scope for Patheon" is populated on the Opportunity record.'); }

 

else if ('{!Opportunity.Reason_to_Outsource__c}' == '')
{alert('In order to request a Quote, you must ensure that "Reason to Outsource" is populated on the Opportunity record.'); }


else if ('{!Opportunity.Key_Criteria_for_Sucessful_Bid__c}' == '') {alert('In order to request a Quote, you must ensure that "Key Criteria for Successful Bid" is populated on the Opportunity record.'); }

 

else if ('{!Opportunity.Indication__c}' == '') {alert('In order to request a Quote, you must ensure that "Indication" is populated on the Opportunity record.'); }

 

else if ('{!Opportunity.Current_PDS_Project__c}' == '') {alert('In order to request a Quote, you must ensure that "Current PDS Project" is populated on the Opportunity record.'); }

 

else if ('{!Opportunity.Pending_Launch_Date__c}' == '') {alert('In order to request a Quote, you must ensure that "Commercial Launch Date" is populated on the Opportunity record.'); }

 

else if ('{!Opportunity.Forecast_Volumes_and_Market_Potential__c}' == '') {alert('In order to request a Quote, you must ensure that "Forecast Volumes and Market Potential" is populated on the Opportunity record.'); }

 

else if ('{!Opportunity.Decision_timeline__c}' == '') {alert('In order to request a Quote, you must ensure that "Decision Timeline" is populated on the Opportunity record.'); }

 

else if ('{!Opportunity.Regulatory_Approval_Strategy__c}' == '') {alert('In order to request a Quote, you must ensure that "Regulatory Approval Strategy" is populated on the Opportunity record.'); }

 

else { window.location = "/0Q0/e?&oppid={!Opportunity.Id}&ent=Quote&Name='Placeholder' &00NA0000004pyaC=1&ent=Quote &RecordType={!Opportunity.Quote_Record_Type__c} &retURL=/{!Opportunity.Id}"; }

sandeep@Salesforcesandeep@Salesforce

One simple solution might be checking record type in Java script using connect.js and using soql. then control style visibility none or block for particular set of question based on record type.

GlynAGlynA

John,

 

Below is a solution(?) that attempts to do many things.

 

1) It includes the sforce.connection API so that you can query the record type IDs

2) It queries the record type IDs

3) If splits the field checks into two groups, one group for one record type; another group for the other record type.  You will have to sort out which field should actually be checked in each group.

4) It accumulates an alert string via the "checkField" function and display a single alert at the end with all the error messages combined. As a user, I would prefer this to being told about one error at a time.

 

A caveat:  This code is completely untested.  There may be syntax or other errors in the code.  If it doesn't work for you (but you'd like to try to make it work), I will be happy to help you figure out any problems.

 

The code:

{!requireScript("/soap/ajax/29.0/connection.js")}

var rtid_Commercial = sforce.connection.query( "SELECT Id FROM RecordType WHERE sObjectType = 'Opportunity' AND DeveloperName = 'Commercial'" ).getArray("records")[0].Id;
var rtid_PDS        = sforce.connection.query( "SELECT Id FROM RecordType WHERE sObjectType = 'Opportunity' AND DeveloperName = 'PDS'" ).getArray("records")[0].Id;

var alertString = '';
var alertCount = 0;

function checkField( field, label )
{
    if ( field == '' ) alertString += '' + (++alertCount) + ') "' + label '" is populated on the Opportunity record\n';
}

if ( '{!Opportunity.Existing_Commercial_Pricing_Quote__c}' != 'Yes')
{
    if ( '{!Opportunity.RecordTypeId}' == rtid_Commercial )
    {
        checkField( '{!Opportunity.Active__c}',                                 "Active"                                );
        checkField( '{!Opportunity.Rx_OTC__c}',                                 "Product Type"                          );
        checkField( '{!Opportunity.Project_Scope_for_Patheon__c}',              "Project Scope for Patheon"             );
        checkField( '{!Opportunity.Reason_to_Outsource__c}',                    "Reason to Outsource"                   );
        checkField( '{!Opportunity.Key_Criteria_for_Sucessful_Bid__c}',         "Key Criteria for Successful Bid"       );
    }
    if ( '{!Opportunity.RecordTypeId}' == rtid_PDS )
    {
        checkField( '{!Opportunity.Indication__c}',                             "Indication"                            );
        checkField( '{!Opportunity.Current_PDS_Project__c}',                    "Current PDS Project"                   );
        checkField( '{!Opportunity.Pending_Launch_Date__c}',                    "Commercial Launch Date"                );
        checkField( '{!Opportunity.Forecast_Volumes_and_Market_Potential__c}',  "Forecast Volumes and Market Potential" );
        checkField( '{!Opportunity.Decision_timeline__c}',                      "Decision Timeline"                     );
        checkField( '{!Opportunity.Regulatory_Approval_Strategy__c}',           "Regulatory Approval Strategy"          );
    }
}

if ( alertCount != 0 )
{
    alert( 'In order to request a Quote, you must ensure that:\n' + alertString );
}
else
{
    window.location = "/0Q0/e?&oppid={!Opportunity.Id}&ent=Quote&Name='Placeholder' &00NA0000004pyaC=1&ent=Quote &RecordType={!Opportunity.Quote_Record_Type__c} &retURL=/{!Opportunity.Id}";
}

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator

John BraunJohn Braun

Hi Glyn,

 

Thank you so much for this suggestion, when applying it, I get the error:

 

"Unexpected String"

 

Do you have any advice on where to go to fix it?