Simple AJAX in Salesforce

Scenario:

The relationship between Member and Interests is Master Detail. One member can have multiple interests.

Check Valid Interests button will say whether any interest under the Member is active using AJAX.

Custom Detail page Button:

Button Javascript code:

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

var soql = "SELECT InfallibleTech__Active__c FROM  InfallibleTech__Interest__c WHERE InfallibleTech__Member__c = '{!InfallibleTech__Member__c.Id}'";

result = sforce.connection.query(soql);
records = result.getArray("records");
var check = "false";

if(records.length > 0){
    for (var i=0; i < records.length; i++) {
        var record = records[i];
        if(record.InfallibleTech__Active__c == "false"){
            check = "true";
        } else{
            check = "false";
            break;
        }
    }
} else {
    check = "true"
}

if(check == "true"){
    alert("No active interests");
} else {
    alert("Active interests are present");
}

Output:

With Active interests:

With No Active interests:

Leave a Reply