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
sksfdc221sksfdc221 

Display error message in lightning component page if the set condition is true

I have a lightning component which is used to merge the custom lead object which is a duplicate. Here, I have a requirement that if the two selected leads are marked as true for Integrated_Lead__c field, then when clicked on merge, an error should be displayed on the page that "Leads cannot be merged".

Below is my Apex Class method:
 
@auraEnabled
public Static List<cContact> contactsearch(String userinp, String userinput) {
    system.debug(userinput + '=======' + userinp);
    errormsg = false;
    errormsg2 = false;
    contactList = new List<cContact>();
    String query;
    String leadName;
    if (!String.isEmpty(userinp)) {
        leadName = userinp + '%' + userinput + '%';
        system.debug('if====' + leadName);
    } else {
        leadName = '%' + userinput + '%';
        system.debug('else====' + leadName);
    }
    system.debug('leadName===>' + leadName);
    
        for (Lead_Custom__c c : [select Name, First_Name__c, Last_Name__c, Integrated_Lead__c, Id, Cell_Phone__c, Mailing_Street__c, Mailing_City__c, Mailing_State_Province__c from Lead_Custom__c where Name like :leadName]) {
            contactList.add(new cContact(c));
        }
    
    return contactList;
}
@auraEnabled
public Static List<cContact> getresults() {
    return contactList;
}

Lightning Component:
 
<aura:component controller="SearchDuplicateLeads_Lightning" implements="force:appHostable,flexipage:availableForAllPageTypes" access="global">
  
    <aura:handler name="init" value="{!this}" action="{!c.SearchDuplicateLeads}"/>
    <aura:attribute name="userinput" type="String" default=""/>
    <aura:attribute name="userinp" type="String" default=""/>
    <aura:attribute name="render" type="Boolean" default="false"/>
    <aura:attribute name="Spinner" type="boolean" default="false"/>
    
    <aura:attribute name="contactList" type="String[]" />
   
                                <aura:iteration items="{!v.contactList}" var="contacts">
                                    <tr class="slds-hint-parent">
                                        <td role="gridcell" class="mycol">
                                            <lightning:input type="checkbox" aura:id="selected" label=" " value="{!contacts.selected}" onchange="{!c.singleChecked}"/>
                                        </td>
                                        <td role="gridcell">
                                            <ui:outputText value="{!contacts.con.Name}" />
                                        </td>
                                        
                                    </tr>
                                </aura:iteration>
                                </tbody>
                            </table>
                        </div>
                        <lightning:button variant="brand" onclick="{!c.processrecSelected}" label="Next" class="cus-button"/>
                        <lightning:button variant="brand" onclick="{!c.Cancel}" label="Cancel" class="cus-button"/>
                    </aura:renderIf>
                </div>
            </div>
        </div>
    </div>
</aura:component>

Js:
 
processrecSelected: function(component, event, helper) {
    var action = component.get('c.processSelected');
    var strData = JSON.stringify(component.get('v.contactList'));
    console.log('strData===>'+strData);
    action.setParams({ 'contactstr' : strData});
    action.setCallback(this,function(response){
        if (response.getState() === "SUCCESS") {
            var res = response.getReturnValue();
            console.log('res===>'+res.length);
            if(res.length==2){
                redirecttoMerge(res[0].Id,res[1].Id);
            }else if(res.length>2){
                toastEvt('Please select at most two records to proceed');
            }else if(res.length==1){
                toastEvt('Please select at most two records to proceed');
            }else{
                toastEvt('Please select a record to proceed.');
            }
        }
    })
    $A.enqueueAction(action);
    function toastEvt(msg){
        var toastEvent = $A.get("e.force:showToast");
        toastEvent.setParams({
            "title": "Error!",
            "type":"Error",
            "message": msg
        });
        toastEvent.fire();
    }
},

Now, from the above contactList, the merging will be done when two Custom Leads are selected. As per my requirement, if the selected two leads are having Integrated_Lead__c = True, then an error message should be displayed on the screen that "Leads cannot be merged" on click on next button.
​​​​​​​
Can anyone please suggest the changes so that I can get the error message displayed on screen
jey welljey well
Visit to get best Tamil quotes (https://statussove.com/tamil-quotes/) collection
ShirishaShirisha (Salesforce Developers) 
Hi,

Greetings!

You can actually add the custom validation before merging the leads validation rule which can check for the checkboxes on both the leads and display the error message.

Please check the sample code for custom validation rule in the lightning component:

https://www.biswajeetsamal.com/blog/handle-trigger-and-validation-rule-error-in-lightning-component/

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
sksfdc221sksfdc221
@Shirisha, I have tried the suggested solution and the error message is not displaying on the screen. I can track the error message in the debug log but cannot able to see that on the UI part in lightning component.
I have created validation rule as follows

User-added image

below is my updated .js code:
 
if (response.getState() === "ERROR") {
                var errors = action.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        alert(errors[0].message);
                    }
                }
I can trace the error in the debug log but not on the screen. 
Can you please suggest any changes based on the above input.

Thanks!