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
AntonyasLenAntonyasLen 

Using javascript to make a "validation rules"

Hi,

 

i created some Triggers/Classe and i want to launch them by clicking on a button so far it's working by using a VF page who is calling my classes. The issue s that i want to know how to forbib my classes to be launched in some case ( for the example a boolean who is false).

 

Someone told me about using javascript but i'm pretty bad and it's kind a mess for me ...

 

Does anyone can help me??( if you can give me a strong exemple it would be awesome

 

Thanks

TrinayTrinay
Hi Antony,

 

   What i am understanding from your question  was, you need to call the class/trigger by button click. By doing this refer the following

 

1. We can't call the Trigger by button click because the triggers are used when we want to perform an operation on a dml event. 

 

2. We can call the class by button click.But it have some conditions:

   

           Firstly, your class should be marked as "Global"

           Secondly, the logic goes to a static method of this class which is marked as "WebService".

 

    If an Apex class has the above characteristics, the method marked as web service can be called via javascript when the button is clicked on.

 

Refer the following class and script for your clear understanding:

Example Class:

----------------------

global class OutboundEmails

{
       WebService staticvoid SendEmailNotification(string id)

       {
           //create a mail object to send a single email.
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

           //set the email properties
           mail.setToAddresses(newstring[] {'myemail@domain.com'});
           mail.setSenderDisplayName('SF.com Email Agent');
           mail.setSubject('A new reminder');
           mail.setHtmlBody('an object with ID='+ id + ' is just clicked on.');
          

           //send the email
           Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail } );
     }
}

 

Creating a button:

------------------------

  create a detail page button let's say on Account object and name it "Send Me ID".
  This button's behavior will be "Execute Javascript". Then add the following code to the body of button.

 

{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
sforce.apex.execute("OutboundEmails","SendEmailNotification", {id:"{!Account.Id}"});
window.alert("Account Id is sent." );

 

Description of the script:

------------------------------------

OutboundEmails - Your global class Name.

SendEmailNotification - method name

Account.Id - your object id.

 

After you completing the above click the created button, then you got the mail. I hope this will helpful to you.

 Reference:

 http://salesforcesource.blogspot.in/2009/06/triggering-apex-method-with-custom.html

AntonyasLenAntonyasLen

Hi,

 

Thanks for your answer i got it how to use it ( i already saw the blog post ).
But my problem is that i want to call a class as you explained , but in one case i want to block the call and show up a message.

Background:

 

I want to allow the user to call the class by this button : " only one time and only  if the opportunity is "Close won" "

 

Thanks =]

PrakashbPrakashb

You can use Javascript to achieve your functionality.

 

On your javascript function you can do the validation and show an alert if it satisfies or else you can call your class directly or using actionfunction.

 

Please specify your requiremtn more so that I can help with some sample example.

 

I have given a small sample the can be used.

 

<script type="text/javascript">

function validate(){

if(your condition)

alert(Your Message);

else

classcall();

}

</script>

 

<apex:actionfunction name="classcall" action="{!yourmethod}"/>

AntonyasLenAntonyasLen

My requierements are the requierements below:

I want to "Push my Order" the meaning is that when my Opportunity is "Closed Won" my salesman will be available to push his opportunity to create an order in our e-commercer website,also, when the salesman will push his opportunity an hidden checkbox will be set to True to lock it.

So my salesman can't push his order if the opportunity isn't "closed won" or/and he already did. ( checkbox name = "Hidden__c"


My class will just sent an http request with the product information.

public with sharing class PushOrder {

	public Opportunity op;

	public PushOrder(ApexPages.StandardController controller)
	{
		this.op = (Opportunity)controller.getRecord();
		
	}
	
	 //@Future(callout=true)
	  public PageReference CreateMyUrl(){

           ...............
          }
}

 **NOTE**

So far my class is working because i tried to call my class by using a VF page called with a button.
But as i said i need to block my user to do it .

 

I tried to floow you example:

<script type="text/javascript">

function validate(){

if({!Opportunity.StageName} != 'Closed Won')

alert( "It's not the time to push");

else{

sforce.apex.execute("PushOrder","CreateMyUrl", {};
}

</script>

 But i got an error message when i tried to execute it

 

I hope that i provided enought informations.

Let me know if i forgot something.

 

Thansk a lot to spend time to explain to me !

 

sincerely,

 

Antony

AntonyasLenAntonyasLen

maybe i need to change the type of my class =[

 

edit:

 

I tried toset my class as static void or void ...but it still don't work =/