Display Alert box through the trigger - salesforce - salesforce

Is it possible to display an alert message (not error message) through a trigger? I have written a trigger that checks for duplicate accounts in the system. The trigger at the moment gives an error message to the user telling that there is a duplicate account. But, if the user changes the value of a field "Is record near to duplicate?" to YES, the trigger allows the user to save the record.
But, I want to display the error message in an alert pop up box like "Account with this Name exists,are you sure you want to continue" and then user clicks Yes and the record gets created. Any thoughts on how I can do this. My code is below:
for(Account account: System.Trigger.New)
{
if(accountMap.containsKey(account.Physical_Street__c)==accountMap2.containsKey(account.Phone))
if(accountMap.containsKey(account.Physical_Street__c)==accountMap3.containsKey(account.Name))
if(account.Is_record_near_to_duplicate__c.equals('No'))
{
account.addError('Account with this Name,Street and Phone Number already exists. If you still wish to create the agency change the value of field "Is Record Near To Duplicate" to YES');
}
}

If you really need an alert box then you could create a custom javascript button that uses the ajax toolkit to replace the standard save button.
However, as Baxter said you are getting pretty far from standard salesforce look and feel. I would recommend instead to add an error on the checkbox field instead of the object so it is clear to the user what they need to select.
if(account.Is_record_near_to_duplicate__c.equals('No'))
{
account.Is_record_near_to_duplicate__c.addError('Agency with this Name, Physical Street and Phone Number already exists. If you still wish to create the agency change the value of field "Is Record Near To Duplicate" to YES');
}

Unless you write a custom visualforce page that processes the error message and then displays it as a popup there's no way to do this.
If you wanted the alert to modify the data you may look at using the ajax api to set the Is_record_near_to_duplicate__c field to 'Yes' but either way you're getting pretty far from the standard functionality and interface with this.

It is not straight forward to implement a pop-up alert for a trigger error, and pop-ups went out-of-fashion a long time ago. If you are going through the hassle, you might as well implement a custom soultion on a VF page.
Throw a custom exception(for dupe accounts) from the trigger, and catch it in your controller. When you catch this exception, you can dynamically displayed section on the page which asks the user to confirm his/her action. When the user confirms the action, the page will do the rest.
Hope this makes sense!
Anup

Related

React Final Form: has a value existed?

How is it possible to check has a value already existed using React Final Form? I'm interested in the general approach. Also, be thankful for some links, etc, because could find nothing relevant.
Now details:
I have a form: a text input "User name" and a button "Submit".
I type a value (let say, "John") and press "Submit".
The value is saved into a database and becomes to display on my web page.
The input field is cleared and I am able to enter the new value.
I'm entering "John" again.
I should get an error message "Such name has already existed" once I moved the focus out of the field, or click on the "Submit" button.
You'd generally accomplish this by doing a preflight request to the server via fetch.
Send the field value(s) over whenever you do validation (e.g. on change or on blur) and have the server report back any issues (e.g. "Name already taken").
Implement some checking logic In the POST request. Before you write the logic to add the user to the database, check If a user with that username already exists, and If It does, return an error. It also depends on the database you are using how they handle searching for documents.
If its MongoDB you need to use the .find() method and find by username. If you find a matching username, return an error saying to enter a unique username.

Salesforce "Record is not createable INSUFFICIENT_ACCESS" error

Hi,
I'm trying to create a lightning component to add records to the campaign object and I keepo getting an error message that I can't seem to find sufficient information on.
I'm using the lightning:recordEditForm component and because it automatically generates the picklist or input field based on the field data type and doesn't need a controller.
Here is the code I have written.
When I try to add the component to the appropriate page I get the error message shown in red.
Has anyone else solved a similar problem?
I'm inclined to believe it's a permissions issue but I'm not sure. If there's any more information I can give let me know.
I would really appreciate some assistance.
Thank you
Make Sure that your Profile has all necessary Object Permissions for Campaign Object.
For Campaigns: check if you have enabled "Marketing User" checkbox in the relevant User's profile.
This is one of annoying Salesforce Gotchas.
Check whether the user has access to create the record.
Try checking it in Profiles --> Campaign --> make sure Read, Create, Edit are checked (True)
Also make sure you have enabled Marketing User (Setup --> Users)

Concurrent update on record in Salesforce

In system I am developing, user can access particular page which is used to make DML operation on custom object. I want to fulfill following business case :
User A opens a Page made changes on page but did not click Save.
At the same time User B opens a page made changes and click Save. User B data got saved in record.
Now user A came and click Save. But as data is outdated I want to prompt user that "you are over righting the latest update. Do you want to continue."
Is there any faster out-of-box way available to achieve it ?
Assuming this is in a VisualForce page, you could check if the value has changed inside your Save method.
Query for the LastModifiedDate of all records you may change, and save that in your controller. This should be done in your controller's constructor and/or anywhere you re-load the record(s).
In the save method, first query the record(s) again to check if the LastModifiedDate has been changed. If it has been changed, prompt the user.
Assuming you are trying to insert a record into an object.
You can write a validation rule on that object. Check if that record is already inserted or not and show an error message.
Regards,
Naveen
autorabit

How do I make it so that a user must either enter text or select a check box on Salesforce?

I am using salesforce for a group project here at SJSU, the thing is this is our first time using it, and we are having a little bit of trouble programming some things on Salesforce.
What we are trying to do:
We have a section on one of our forms where users will give us authorization to use Data they submit, the usage of this data will be under the terms set by those who submit the data. If the user does not want to set limitations on their authorization for us to use the data then they can select a checkbox called labeled "none" which basically means they are setting no limitations on what we can use their data for.
What we want to do is, if users select the checkbox then we want the users to not be able to enter any text into the textbox. If users enter text into the box while the checkbox is checked we want an error message to appear which will let the user know that no data can be entered into the box if the checkbox is checked. However if no checkbox is checked then we want users to be able to enter the data. How do we go about doing this
AND (None_c = True then Limitations_c has to be empty, elseif None_c = False then Limitations_c cannot be empty.)
If you want to leverage the out of the box UI (Page Layout), then you will likely want to use Validation Rules.
https://help.salesforce.com/HTViewHelpDoc?id=fields_defining_field_validation_rules.htm&language=en_US
The gist of it is that you want to define Error criteria, so when this criteria evaluates to true, and error is thrown. In that case, you should be able to construct something similar to the following:
Checkbox1__c && NOT(ISBLANK(Text2__c))
Validation rules would be the easiest way to execute this, though checking the box would not dynamically prevent users from entering text. With a validation rule, it just wouldn't let a users save.
The nice thing about using validation rules is that you can construct them such that checking None__c will not vomit on the user unless they actually modify Limitations__c. ISCHANGED() is great for that.
If I could suggest an alternative, the way I would implement this is to treat an empty Limitations__c as None__c = True. It simplifies things for users, and you can add a formula-driven checkbox if a checkbox element is truly required.

cakephp avoid logged in users to access other user's account

I'm developing a web with CakePHP 1.3.7. Everything is going fine and I love it, but I just came accross a problem (probably because of my lack of knowledge) that I can't figure out how to fix.
This is my problem:
In the website, there's the typical 'manage my account' area, where users can only access when they're logged in. In this area, there's a link that calls to an action in the same 'users' controller called 'edit_items'. So when a user goes to edit_items, the action receives as a parameter the user's id (something like domain.com/users/edit_items/45, where 45 is the user's id), and shows the information in a form. The problem comes if I go directly to the address bar of the browser and change that 45 for any other user's Id, then the information of that other user is also shown, even if that user is not logged in. This is obviously a big security issue.
I've been trying to avoid passing the user's id as a parameter and getting it from the Auth component (which I'm using) with $this->Auth->User('id'). For whatever reason, I can read the logged user's info into the form fine, but when I try to save the changes on the form, I get an error as if the save action had failed, and I have no clue why.
Is there any other way to avoid my problem? Or to figure out why the save is returning an error?
Thanks!
EDIT
SO the problem comes from the validation, here's the deal: when the user fills out the form to create a new item, there are certain fields, some of them with validation rules applied. However, when the user goes back to edit the item, not all the fields are editable, only some. Is it possible that, since some fields that required validation when creating the item are not available when editing, that causes the error? How can avoid that? Can I change the validation rules only for the edit action?
Example of what's happening: when creating an item,one of the fields is item_name, which has some validation applied to it. When editing the item, its name can not be changed, so it's not shown in the edit form. I believe this what may be causing the error, maybe because the item_name is missing?
You are turned on the right direction - passing user_id on the url is a bad idea when the users need to edit their own details.
You can use following: when saving your form before the actual save you can pass the user_id to the posted data. Something like this:
if (!empty($this->data)) {
$this->data['User']['id'] = $this->Auth->user('id');
... //Some extra stuff
if ($this->User->save($this->data)) {
... //success
} else {
... //error
}
}
This way the logged user will override it's own record always. Check if you have some validation rules in your model which give you this error.

Resources