2

Ii want to display Javascript alert when changes made in one tab are not saved and user moves to next tab. I am using the following code but nothing is displayed when i checked condition for language. Without this condition alert is displayed when user moves from one tab to another.

<scripttype="text/javascript" src="/soap/ajax/27.0/connection.js"></script>
<script type="text/javascript" >
var sid = getCookie('sid');
var server = "https://" + window.location.host + "/services/Soap/u/27.0";
  //initiate the connection
sforce.connection.init(sid, server);

var currentUser = sforce.connection.getUserInfo();
var result = sforce.connection.query('select LanguageLocaleKey from User where      id=\''+currentUser.getUserId+'\'');
var tab1 = document.getElementById('PPage:T1');
var tab2 = document.getElementById('PPage:T2');
var tab3 = document.getElementById('PPage:T3');
var tab4 = document.getElementById('PPage:T4');
var tab5 = document.getElementById('corpgridphone');
//alert('tabs '+tab1 +tab2 +tab3 +tab4);
var save1 = document.getElementsByName("inlineEditSave");
var cancel1 = document.getElementsByName("inlineEditCancel");
var edit2 = document.getElementById('PPage:ActformId:Actpg:Actpgb:EditButton');
var edit3 = document.getElementById('PPage:IntformId:Intpg:Intpgb:EditButton1');
var edit4 = document.getElementById('PPage:CLIformId:CLIpg:CLIpgb:EditButton2');
//alert('Edit Buttons '+edit2 +edit3 +edit4);
tab1.onclick = function() {
    if(edit2.style.display == 'none'){
    if(result == 'fr')
            {
                alert('S'il vous pla&#236;t enregistrer les modifications dans l'onglet D&#233;tails');
            }
            else if(result == 'en_US')
            {
            alert('Please save the changes in Accounts tab');}
            return false;
    }   
    if(edit3.style.display == 'none'){
    if(result == 'fr')
            {
                alert('S'il vous pla&#236;t enregistrer les modifications dans l'onglet D&#233;tails');
            }
            else if(result == 'en_US')
            {
            alert('Please save the changes in Interests tab');}
            return false;
    }
    if(edit4.style.display == 'none'){
    if(result == 'fr')
            {
                alert('S'il vous pla&#236;t enregistrer les modifications dans l'onglet D&#233;tails');
            }
            else if(result == 'en_US')
            {
            alert('Please save the changes in Client Information tab');}
            return false;
    }

}
tab2.onclick = function() {
    if(save1[0].style.display == 'inline'){
    if(result == 'fr')
            {
                alert('S'il vous pla&#236;t enregistrer les modifications dans l'onglet D&#233;tails');
            }
            else if(result == 'en_US')
            {
            alert('Please save the changes in Details tab');}
            return false;
    }       
    if(edit3.style.display == 'none'){
    if(result == 'fr')
            {
                alert('S'il vous pla&#236;t enregistrer les modifications dans l'onglet D&#233;tails');
            }
            else if(result == 'en_US')
            {
            alert('Please save the changes in Interests tab');}
            return false;
    }       

    if(edit4.style.display == 'none'){
    if(result == 'fr')
            {
                alert('S'il vous pla&#236;t enregistrer les modifications dans l'onglet D&#233;tails');
            }
            else if(result == 'en_US')
            {
            alert('Please save the changes in Client Information tab');}
            return false;
    }

}
2

Assuming this is in the context of Visualforce, you would do well to simply have this exposed in the controller:

String userLanguage;

public String getLanguageLocaleKey() {
    if(userLanguage == null) {
        userLanguage = [SELECT LanguageLocaleKey FROM User WHERE Id = :UserInfo.getUserId()].LanguageLocaleKey;
    }
    return userLanguage;
}

This avoids the need to waste an API call just to the the user language when this information is freely available by querying it in your controller.

I would also recommend using a translation table instead of a convoluted if statement.

var messages = { 
    message1: { 
        fr: "S'il vous pla&#236;t enregistrer les modifications dans l'onglet D&#233;tails',
        en_US: "Please save the changes in Accounts tab"
    },
    message2: {
        ...
    },
    ...
}

You can then display the error like this:

if(edit2.style.display == 'none') { 
    alert(messages.message1[languageLocaleKey]); 
    return false; 
}

This centralizes your logic to just a small portion of code, and you can fix translations.

Also, note that you're not escaping your strings correctly in the example code; this should cause errors. Use double-quotes, or make sure you escape the strings correctly.

Finally, you could choose to avoid this entire mess by simply using Custom Labels, which are precisely designed to support this need without your page having to know what language the user is configured for (the system renders them automatically for you!).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.