Trying to clear ApexPages.Messages Container - salesforce

I apologize for the long winded question.
Context:
We have a mapping that basically maps a field from one object to a field on another object. Object_A to Object_B. In the trigger of Object A we look to see if there is a mapping to object B. If so, we load object b record and copy the value from the specific field in A to the field in B. After we've made all the copies we update B.
We have a try catch around the update to B to catch any exceptions. The issue we are facing is that an exception is thrown from a Salesforce validation, not a custom validation rule and creates messages automatically. For example someone entered 123.45, and the field in object b that it's attempting to transfer to is formatted as a 2.0 number. That will generate a DML exception for invalid range. Which is fine. The problem is that Salesforce is also adding an error to the apexPages.Messages container. So if we have an on the page it will display the Salesforce error.
There's 2 problems with this.
1. The message is not really user friendly.
2. The message contains the field label of the field on object B not the field on object A. The user in this context will have no idea what that field is and with potentially 100 fields on the page that they have filled out, they might not be able to track it down.
I would much rather SF didn't add the message automatically, allowing me to catch the exception and format a message. But I don't see anyway to either stop SF from creating the message or clearing out the messages container so that we can add our own.
Is there a way to either stop Salesforce from automatically adding the message or to clear the apexPage.Messages without refreshing the page?
edited to show a quick example code:
list<Contact> contacts = [Select id,Email from Contact where id='someSFId'];
contacts[0].email = 'badEmail';
try{
update contacts;
}
catch(Exception ex)
{
System.debug(Apexpages.hasMessage());
}
You'll see the results of the debug will be true even though I've not added a message.

Hope this helps any.
List<ApexPages.Message> msgs = ApexPages.getMessages();
for(ApexPages.Message message : msgs ){
// get the object A field label from maaping
// construct custom error message/ replace field label string
}
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_apexpages.htm

Related

Integration Segment with Salesforce, adding an action

All I want to do is,
Add Salesforce as destination
From the Track Object, get a field
Update to Salesforce
I created a custom action
But it does not work
everytime it inserts a lead
My action is not executed
What am I doing incorrect?
Below image I am testing to create a case to see if that works.
It throws and error for not including Salesforce:true.
Not sure where to include that
I think the issue is that you are using the Create operation for the action. Instead of creating a new case, you should use an Update operation. As the form says:
If the CRUD Operation is Update an ID field mapping is required.
I can't see that you have included an ID field in your field mappings there either. You will need that ID, so that you can update your customer.

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.

How to Update RecordTypeId field in Lightning record form in salesforce?

Hi #all I have lightning record edit form which are showing specific value one of them is a recordtype. I want to update the recordtype from the same as other fields are updating but when I click on recordtype field it shows me the below error:-
[LWC component's #wire target property or method threw an error during value provisioning. Original error:
[Field: RecordTypeId is not a valid lookup field.]]
Record Type change is a critical operation that messes everything up. Potentially you're looking at new page layout (incl required/readonly fields), new picklist dependencies... There's reason why you select it first before any layout is displayed. And why record type change is a special button, not a field visible on normal edit action.
If you know what you're doing, are confident the requirements won't change / you'll know how to represent the changed layouts, picklists etc...
Use getObjectInfo to pull (among others) the Map of record types.
Use this data to build lightning-combobox that onchange updates a helper variable (you'd ideally have that variable bound to <lightning-record-edit-form record-id={recordId} record-type-id={recordTypeId}
Have a custom handleSubmit in the record-edit-form in which you'd intercept the save, prepopulate the field and then submit.

INVALID_FIELD_FOR_INSERT_UPDATE in Salesforce via API

I am trying to make a batch update to Salesforce as part of a data masking project and am getting the error INVALID_FIELD_FOR_INSERT_UPDATE when I try updating fields of a particular custom object via API.
Our custom object has a few standard fields and a few custom fields. The custom fields are what I'm trying to update but I keep getting denied.
Each field that I'm trying to update is either Long Text Area(32768) or Text(255). There are no lookups, controlling fields, validation rules, nor are there field dependencies.
I am able to make modifications to other objects (Account for example) via the masking process. I am also able to make modifications to the values through the web UI on the values for this custom object.
I've tried as members of the API group as well as System Administrator.
Can anyone please point me in the right direction? I don't have access to the source code of the masking tool, but I do have elevated rights in SF.
Thanks in advance for your advice.
The exact error:
INVALID_FIELD_FOR_INSERT_UPDATE Error message: Unable to create/update fields: Name. Please check the security settings of this field and verify that it is read/write for your profile or permission set.
The funny thing is that I'm not trying to update the field "Name". Name seems to be an auto-number field on the object, but I'm not sure why this field would change value on an update to a different column.
If some one facing the same issue of not able to update the Name column because it has data type as Name.
There are 2 solutions:
If you want to push the name of human then try pushing FirstName and LastName instead. "Name" will get populated automatically.
If you want to push the name of a product then try changing the type of Name field to Text. If that doesn't work then you can push the name to either FirstName or LastName, whichever is mandatory(Just A work around).

Display Alert box through the trigger - 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

Resources