I am facing an issue with cases, recently they were a lot of spam cases that have been created from web to case. and this is not the first time it happened earlier also. So we have created validation rule on case to avoid spamming based on the subject, body, sender, etc but it's just a temporary solution because every time we receive a different kind of data, as soon as we get to know about spamming we are checking the values and adding those values in the validation rules to avoid further case creation.

But now we have come up with the below solution.

Creating an IP address field on case object, on the creation of case in salesforce we are saving the IP address of the system which we received the data from. so our motto is whenever more cases are created from the same IP address (10+ cases within 24 hours from the same IP address) then we are going to block that IP address temporarily. So I think by implementing this we can prevent spam cases creation in salesforce.

Question is:

How to get the IP address of the user's system in apex when they try to create a case?

And please do let me know if you have a better solution.


IP address information is not available in Apex (at least not natively).

A better approach here would likely be to reject these on your site (i.e. block it before it gets sent to Salesforce. Take advantage of the reCAPTCHA option.), but the details of how to approach that are likely off-topic for this site.

From https://help.salesforce.com/articleView?id=faq_cases.htm&type=5

How can I prevent spam from becoming cases?

You can limit spam in the following ways.

  • reCAPTCHA is your first line of defense. In Salesforce, enable reCAPTCHA. Then make sure that the code on your website includes the reCAPTCHA verification.
  • Implement an anti-spam alternative.
  • For severe attacks, ask Salesforce support to create a blacklist rule to reject requests from specific IP addresses.
  • Download spam filter apps from AppExchange.
  • 1
    reCaptcha alone will probably solve the OP's issue Jan 29 '20 at 16:16

Create a custom visualforce page to log cases, and then on submission use the following to get the users IP address:

String ipAddress = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');

you can then use this to populate the ip address field on the case.


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.