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 

Error message

Dear community,

 

I have a class called by a button into my opportunity who will create an order in our website.

It's working pretty well, but i had to add a check function who will check if the product are in stock.(this function isn't a problem)

 

If one of the opportunity line item is not in stock (by the checking process) i just want to display a message to say "The products below arent in stock : id 1234 id 2569 ....." and don't allow the class to keep working( as a break point).


What is the best way to do it? i didn't understand the add.error method.

 

 

Sincerely,

Antony

Best Answer chosen by Admin (Salesforce Developers) 
Noam.dganiNoam.dgani

When to validate, is a business flow question that should be answered internally in your company.

 

don you wnat to prevent your user from setting stage to 'Closed Won' becuase that's what the businnes demands or just

as a helper for your push order process?

 

i would perform the validation when the 'Push Order' event occurs, because from the time the opportunity is set to 'Closed Won' untill the user decides to 'Push Order' your stock can change.

 

So, basically what you have to do is have your action method perform the validation on the opporutnity data, before it calls out to the external service.

additionally - add <Apex:pageMessages/> to your VF page as such

apex:page standardController="Opportunity" extensions="PushOrder" showHeader="false" action="{!CreateMyUrl}" >
<Apex:pageMessages/>
{!Opportunity.id}
</apex:page>

 

if the validation passes - great. continue to perform the callout.

if not - in your controller do the following:

ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'YOUR ERROR MESSAGE'));

and return null (i'm assuming your page returns action method returns a page reference back to the opp or something).

 

If you do decide to perform the validation when the users changes the stage and not when the push order occurs,

let me know - that can be done with a before update trigger on the opp.

i didnt go into it becuase i dont think that's your use case.

 

hope this helps and that its clear.

if not please dont hesitate to ask away.

 

if this does help - kindly mark it as resolved

All Answers

Noam.dganiNoam.dgani

Hi

 

Does your button run a vf page? JavaScript? Updates the opportunity?

 

AntonyasLenAntonyasLen

My bad ,

It's a button running a visual force page who will run my apex code :

 

<apex:page standardController="Opportunity" extensions="PushOrder" showHeader="false" action="{!CreateMyUrl}" >
{!Opportunity.id}
</apex:page>

Informations:

 

My class is working well it's sending a request to my e-comerce website to create an order.

But the logistic service want to disallow the "Push Order" from salesforce if the quantity of one the opportunity line item is too high compare to the available stock (i aslo have an other request who can get the current stock and it's working well too), also, if the opportunity isn't  "Close Won".

 

The best way should be to:

 

1- Check the stock of my item when i'm changing the stage of my opportunity to "Close Won" - Unauthorized my user to do it if my StockRequest is telling my that one of my Opportunity Line Item isn't in stock.

Just unauthorize the user to click on "Push Order" if the stage isn't "Close Won".

 

2- Check all when the users will attempt to  "Push Order" and block them to do it if the opportunity stage isn't "Close Won" or/and some object aren't in stock.

 

 

Don't you think so?

Anyway my main question are:
How to make a validation rules for a button and show up the error message (or send email ).

Can we launch apex class (to test) when the stage is changing?

 

Thanks,

 

Antony

Noam.dganiNoam.dgani

When to validate, is a business flow question that should be answered internally in your company.

 

don you wnat to prevent your user from setting stage to 'Closed Won' becuase that's what the businnes demands or just

as a helper for your push order process?

 

i would perform the validation when the 'Push Order' event occurs, because from the time the opportunity is set to 'Closed Won' untill the user decides to 'Push Order' your stock can change.

 

So, basically what you have to do is have your action method perform the validation on the opporutnity data, before it calls out to the external service.

additionally - add <Apex:pageMessages/> to your VF page as such

apex:page standardController="Opportunity" extensions="PushOrder" showHeader="false" action="{!CreateMyUrl}" >
<Apex:pageMessages/>
{!Opportunity.id}
</apex:page>

 

if the validation passes - great. continue to perform the callout.

if not - in your controller do the following:

ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'YOUR ERROR MESSAGE'));

and return null (i'm assuming your page returns action method returns a page reference back to the opp or something).

 

If you do decide to perform the validation when the users changes the stage and not when the push order occurs,

let me know - that can be done with a before update trigger on the opp.

i didnt go into it becuase i dont think that's your use case.

 

hope this helps and that its clear.

if not please dont hesitate to ask away.

 

if this does help - kindly mark it as resolved

This was selected as the best answer
AntonyasLenAntonyasLen

I'm totally agree with you!

I think that i will choose to perform the validation when the 'Push Order' event occurs, anyway if an other salesman is working at the same on an opportunity with the same product it !

 

As you guessed at the end of my CreateMyUrl() function i'm returning a PageReference.

Here an extract of my current PushOrder class (without the stock test) :

 

public with sharing class PushOrder {

	public Opportunity op;

	public PushOrder(ApexPages.StandardController controller)
	{
		this.op = (Opportunity)controller.getRecord();
		
	}
	
	
	 public PageReference CreateMyUrl(){
            ******START*********

Creation of the url - doing  request - reading the answer - pushing the order

            ******END*********
           return new PageReference('javascript&colon;window.close()');    
        }
}

I created a function called getProductStock().
I understood the idea but i don't what is the the best way to use this test ... i mean like :

 

List<OpportunityLineItem> oli = getProductStock(op);
if ( oli = null)
{
	**insert here my current push process**
}
Else{
	for(OpportunityLineItem opps : oli){
		ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'The quantity asked for the product' + oli.ProductID +' is not in stock'));
	}
}

 

What do you think about that?

Or

List<OpportunityLineItem> oli = getProductStock(op);
if ( getProductStock(op = true)
{
	**insert here my current push process**
}
Else{
		ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'Some product are not in stock please check it'));

}

 Thanks for your advice

 

 

AntonyasLenAntonyasLen

I forgot to tell you that my checking function is also a callout i hope that it will not be a problem isn't it?

Noam.dganiNoam.dgani

Yep. thats looks like the correct flow.

 

please note: return null (the page reference to the current page) at the end of your else statement.

 

glad i could help

 

 

Noam.dganiNoam.dgani

Regarding the method that performs the check, having it perform a callout is not a problem in the context of a VF page.

 

if you would have chosen to perform the check in an opportunity trigger, that would be problematic, becuase

you cannot perform synchronous callout in a trigger (transactions cant wait for response from external services) - therefore you couldnt have added and error to the opportunity record.

AntonyasLenAntonyasLen

I tried to set it ...

For the moment i'm waiting for my webmaster i creating a fake product.

i set a boolean variable as true to check the error page i got this:

 

.

 

How can i easily hide the Opportunity id? (because is not that secure).

I will mark as resolved you second i think it's most usefull answer and try later with a real product.

 

My last question would be how can we do the same kind of test when the stage is changing ? (because i need to make a test using apex code =/)

Thanks a lot

Noam.dganiNoam.dgani

Just remove {!opportunity.id} from your vf page.

 

regarding the test in apex -

 

you will need a before update trigger on opportunity that will check if the stage changed and if it is now closed won.

if so - call a @future method that will perform the callout to your web service and update the opportunity accordingly.

 

the downside is that you can't perform a callout in the middle of a transaction (like the opportunity update), so you won't 

have immediate feedback from your webservice to show to the user that updated the record.

AntonyasLenAntonyasLen

i was thinkin that i need to let my  {!opportunity.id} inside my vf page to pass the ID to my class