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
sumpritsumprit 

Create Trigger to display ALERT message when the SAVE button is clicked.

Hi there,

I already have a Scontrol which displays an alert message on the Opp page when the 3 criterias are met. I would like to implement the same logic in a Trigger so that it displays the alert message only when the SAVE button is clicked. Here is the code for Scontrol:-
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
<html>
<head>

<script type="text/javascript" language="javascript" src="/js/functions.js"></script>
<script type="text/javascript" src="/soap/ajax/10.0/connection.js"></script>
<script type="text/javascript">
   
function updatethechkbox()
{
var cas= new sforce.SObject("Opportunity");
result = sforce.connection.create([cas]);
cas.Id = "{!Opportunity.Id}";
cas.ForAlertMsg__c = "true";
result = sforce.connection.update([cas]);


}

    function throwalert()
    {

     // Begin by creating 3 variables for the criteria to be met.
    // 1st RecordType should be KBB Generic for the Opportunity
   
    var recordtype = "{!Opportunity.RecordType}";
    
    // 2nd will be the TYPE of opportunity that is created, which is the standard field.
   
    var thetype = "{!Opportunity.Type}";
   
    // 3rd will be the Opportunity Status which is also a standard field.
   
    var isstatus = "{!Opportunity.StageName}";
  
   // Another variable is defined which will hold the ALERT message as this is the better approach - inside the function.

    var msgg = "You can now create the Sales Order for this Opportunity"

   // Another variable for the hidden checkbox field on the Oppor page which will be set to TRUE first time alert is displayed
   
     var hide = "{!Opportunity.ForAlertMsg__c}";
     //var isChanged = false; // This will be set to TRUE when the ALERT is displayed the first time.

   
    // Now the IF statement for 3 criteria to be met, along with the following:-
    //  1. Check if the chkbox is set to TRUE or not.
    // 2. If checkbox is NOT TRUE then display the message and set it to TRUE.
    // 3. If checkbox is TRUE then dont display the message.


     if ((recordtype == "KBB Generic") && (thetype == "KBB KPO") && (isstatus == "Closed Won") && ({!Opportunity.ForAlertMsg__c} == false))

     {
        updatethechkbox();
        alert(msgg);
       
       }
       else
       {          
                     window.parent.location.replace = "{URLFOR($Action.Opportunity.View, Opportunity.Id,null,true)};"
        }
     } 
    </script>
</head>
<body onload="throwalert()";>
</body>
</html>
////////////////////////////////////////////////////////////////////////////////////////

I am little confused as to how will I start writing a Trigger which does the same thing?

I understand it will be of the Before Insert and Before update.

Can you give me few starting points on this one please?

thanks
Always ThinkinAlways Thinkin
Hi Sumprit,
Apex does not offer any control that would open an alert message similar to JavaScript. To quote the guide "Apex cannot be used to...Render elements in the user interface other than error messages."

Luke C
<script type="text/javascript"></script>
AmandaHowell.ax368AmandaHowell.ax368
I really like the idea of this s-control that creates a message alert.  I am trying to adjust the coding to create one that will pop up if a custom field called "Split Commission Sale" is checked.  I thought I had edited it properly but when I added the html s-control to the opportunity page and then went to an opportunity and tried it and the page layout showed this huge section with //////////// and the rest was blank and I didn't get any pop up or alert.  Can you please tell me what I'm doing wrong?  (my edited coding is below)
Thanks,
Amanda

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
<html>
<head>

<script type="text/javascript" language="javascript" src="/js/functions.js"></script>
<script type="text/javascript" src="/soap/ajax/10.0/connection.js"></script>
<script type="text/javascript">
 
function updatethechkbox()
{
var cas= new sforce.SObject("Opportunity");
result = sforce.connection.create([cas]);
cas.Id = "{!Opportunity.Id}";
cas.SplitCommMessageShown__c = "true";
result = sforce.connection.update([cas]);


}

    function throwalert()
    {

     // Begin by creating 1 variable for the criteria to be met.
   
 
    // 1st will be the Opportunity Split Commission Sale which is a custom field.
 
    var isstatus = "{!Opportunity.Split_Commission_Sale__c}";
 
   // Another variable is defined which will hold the ALERT message as this is the better approach - inside the function.

    var msgg = "Please manually update each Rep amount field when there is a split commission."

   // Another variable for the hidden checkbox field on the Oppor page which will be set to TRUE first time alert is displayed
 
     var hide = "{!Opportunity.SplitCommMessageShown__c}";
     //var isChanged = false; // This will be set to TRUE when the ALERT is displayed the first time.

 
    // Now the IF statement for 1 criteria to be met, along with the following:-
    //  1. Check if the chkbox is set to TRUE or not.
    // 2. If checkbox is NOT TRUE then display the message and set it to TRUE.
    // 3. If checkbox is TRUE then dont display the message.


     if ((isstatus == "True") && ({!Opportunity.SplitCommMessageShown__c} == false))

     {
        updatethechkbox();
        alert(msgg);
      
       }
       else
       {         
                     window.parent.location.replace = "{URLFOR($Action.Opportunity.View, Opportunity.Id,null,true)};"
        }
     }
    </script>
</head>
<body onload="throwalert()";>
</body>
</html>
////////////////////////////////////////////////////////////////////////////////////////
Always ThinkinAlways Thinkin
Hi Amanda,
Are the slashes showing up on the page coming from slashes you have anywhere in your s-control? The inline JavaScript pretty much shows whatever characters are not specifically hidden by virtue of brackets.

I also suspect that the statement:
Code:
     if ((isstatus == "True") && ({!Opportunity.SplitCommMessageShown__c} == false))

 should exclude the quote marks around True since that field corresponds to a boolean not a string.

If you are working in Firefox, I can't recommend installing Firebug strongly enough - it's the handiest way to see where your JavaScript is hanging up...and it will probably point you to the bugs more quickly and accurately than I will!

Luke



jbardetjbardet

I am having a similar problem: just a blank spot appearing on the opp layout instead of the desired alert. Can anyone help? And if it makes a difference, we use Professional Edition. Thanks!

 

<html>
<head>

<script type="text/javascript" language="javascript" src="/js/functions.js"></script>
<script type="text/javascript" src="/soap/ajax/10.0/connection.js"></script>
<script type="text/javascript">

function throwalert()
{

// Begin by creating 3 variables for the criteria to be met.
var oppname = "{!Opportunity.Name}";
var isstatus = "{!Opportunity.StageName}";
var quoteest = "{!Opportunity.Quote_Estimate__c}"


// Now create a function which will decide if the criteria is met or not and throw alert message.

//var oppname = "Test"
//var quoteest >= 50000
//var isstatus = "Proposal/Price Quote"

var msgg = "The quote estimate for this opportunity is equal to or greater than $50,000. Remember to submit this opportunity for approval. "

if ((oppname == "Test") && (quoteest >= 50000) && (isstatus == "Proposal/Price Quote"))
{
alert(msgg);
}
else
{
window.parent.location.replace = "{URLFOR($Action.Opportunity.View, Opportunity.Id,null,true)};"
}
}
</script>
</head>
<body onload="throwalert()";>
</body>
</html>