Another Salesforce blog!!!

Salesforce, Apex

Salesforce: onClick JavaScript


Sample 1:

{!REQUIRESCRIPT("/soap/ajax/28.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/28.0/apex.js")}
var name = "{!Contact.FirstName}";
var conid = "{!Contact.Id}";
//window.confirm('are you sure you want to execute   ' + conid );
 
//SELECT ContactId FROM User
//where contactId = '003E000000crCfb'
 
//var userReg = sforce.connection.query("SELECT ContactId from User where contactId //='{!Contact.Id}' limit 1");
//alert(userReg );
 
var recordExists = false;
var result = sforce.connection.query("SELECT Name,ContactId from User where contactId ='{!Contact.Id}' limit 1");
records = result.getArray("records");
 
for (var i=0; i< records.length; i++) {
  var record = records[i];
  //alert(record.Name + " -- " + record.ContactId );
  recordExists = true;
  alert('This user is already registered.  ' + record.Name);
}
 
if(!recordExists)
{
      if(confirm('You are about to generate a new Activation Email for this user. Would you like to proceed?')) {
      var result = sforce.apex.execute('RequestActivationCodeController','requestActivationCodeFromJS',{ contId: '{!Contact.Id}' });
    if(result == 'Success') {
       window.top.location.reload();
       alert('Send Activation Email Success!');
    }
    else if (result == 'There is no email exists')
    {
       alert('There is no email exists');
       recordExists = false;
    }
}
}

Sample 2: using Labels

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

var recordId = "{!Event__c.Id}";
if("{!Event__c.OwnerId}" != "{!$User.Id}" && "{!Event__c.Status__c}" =="New"){
    alert("Event must send for approval or approved  before attendees send for Subumission.");
}else{
    var resCode = sforce.apex.execute('GlobalUtilCls', 'validateEventRequiredFields', {eventId:recordId});

    if( resCode == '{!$Label.Result_Code_All_OK}'){
        var cqChecked = false;
        if("{!Event__c.Is_Control_Questions_Check_Performed__c}"==true){
            cqChecked = true;
        }else{
            var cqList = sforce.apex.execute('ControlQuestionHelper','getControlQuestionIdListWS',{objectId:recordId});
            if(cqList.length == 0){
                cqChecked = true; 
            }
        }
        if(cqChecked==false){
            alert('Error- '+ '{!$Label.Event_ControlQuestion_RequiredMsg}');    
        }
        else{
            var checkDocsResCode = sforce.apex.execute('RequiredDocumentHelper', 'validateRequiredDocumentsWS', {objectId:recordId});
            if(checkDocsResCode == '{!$Label.Result_Code_All_OK}'){
                window.location.href ='/apex/EventSubmitForm?Id='+recordId;
            }else{
                alert('Error- '+checkDocsResCode);
            }
        }
    }else{
        //display error message
        alert('Error- '+resCode);
    }
}

Sample 3: reLoad the same page

{!REQUIRESCRIPT("/soap/ajax/28.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/28.0/apex.js")}
 
//var conid = "{!Contact.Id}";
 
if(confirm('You are about to generate a Username Retrieval Email for this user. Would you like to proceed?'))
{
  //alert('{!Contact.Email}');
  var result = sforce.apex.execute('RequestUsernameController','requestUsernameFromJS',{ emailAddress: '{!Contact.Email}'});
 
    if(result == 'Success') {
       window.top.location.reload();
       alert('Send Username Retrieval Email Success!');
    }
    else if (result == 'There is no email exists')
    {
       alert('There is no email exists');      
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*