+ Start a Discussion
John Neilan 2John Neilan 2 

Javascript - If / Contains Condition

I have a small piece of Javascript that I am using in an embedded VF page on Opportunities. I would like to add a condition to check to see if the RecordType of the Opp is a certain group of values. The code I have below does not work. Can someone help me with the proper syntax for my Record Type line?
 
SFInitial.openWizard = function(){
            if(
                "{!Opportunity.StageName}" === "{!$Label.Eng_OppStage4}" &&
                "{!Opportunity.RecordType}.indexOf('Eng')" > "-1" &&
                "{!Opportunity.Includes__c}" !== "Yes" &&
                {!Opportunity.Stage_To_Contract__c}
            ){
                window.open(
                    "/apex/VFFormInit?Id={!Opportunity.Id}",
                    "newWindow2"
                );
            }

            return this;

 
pconpcon
The problem is that indexOf is method and you need to be calling that on your string.  And it returns an integer.
 
SFInitial.openWizard = function() {
    if(
        "{!Opportunity.StageName}" === "{!$Label.Eng_OppStage4}" &&
        "{!Opportunity.RecordType}".indexOf('Eng') > -1 &&
        "{!Opportunity.Includes__c}" !== "Yes" &&
        {!Opportunity.Stage_To_Contract__c}
    ) {
        window.open(
            "/apex/VFFormInit?Id={!Opportunity.Id}",
            "newWindow2"
        );
    }

    return this;
}

If you have a controller on your page, I would suggest moving this logic into the controller.  It will be faster, more reliable than the javascript and more testable.  There are also methods in Apex that make a lot fo this easier.