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 in lightning component

I have a custom lightning component which is used to merge two duplicate leads (Lead_Custom__c).

In the component, when two leads are selected, on click of next button, process moves to next page.

 I have a requirement where if the selected 2 leads' custom field called Integrated_lead__c is true (Intergated_Lead__c = True), an error message should be displayed on click of next button

Apex Method:
@auraEnabled
    public Static List<Lead_Custom__c> processSelected(String contactstr) {
        system.debug('contactstr=======>' + contactstr);
        integer iCount;
        iCount = 0;
        String leadName;
        if (!String.isEmpty(userinp)) {
            leadName = userinp + '%' + userinput + '%';
            system.debug('if====' + leadName);
        } else {
            leadName = '%' + userinput + '%';
            system.debug('else====' + leadName);
        }
        List<Lead_Custom__c> selectedContacts = new List<Lead_Custom__c>();
        List<cContact> contactList = [select Name, First_Name__c, Integrated_Lead__c,  Last_Name__c, Id where Name like :leadName order by last_name__c, first_name__c limit 100];
        system.debug('contactList=======>' + contactList);
        if (contactList != null) {
            for (cContact cCon : contactList) {
                if (cCon.selected == true) {
                    iCount = iCount + 1;
                    selectedContacts.add(cCon.con);
                    if (iCount == 1) {
                        idParams = '?id1=' + cCon.con.id;
                    } else {
                        idParams += '&id2=' + cCon.con.id;
                    }
                }
            }
            selectedContactsstep2 = selectedContacts;
        }
        return selectedcontacts;
    }

Component controller.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();
        }
        function redirecttoMerge(conId1, conId2){
           
             
            var evt = $A.get("e.force:navigateToComponent");
            evt.setParams({
                componentDef : "c:Merge_Component_two",
                componentAttributes: { id1 : conId1,id2 : conId2 }
                
            });
            
            evt.fire();
        }
    }

From the above controller.js code, processrecSelected fires on click of next button. Can anyone please suggest the changes in the above code so that I can display error message when the 2 leads related integrated_lead__c is true.


 
vishal-negandhivishal-negandhi

Hi there, 

Based on what I understood, below code must help
 

// change the redirecttoMerge function as below

function redirecttoMerge(con1, con2){
    // check if both are integreated leads
	if(con1.integrated_lead__c && con2.integrated_lead__c){
		toastEvt('Cannot merge when both selected contacts are integreated leads');
	}
	else{
		// fire an event 
		var evt = $A.get("e.force:navigateToComponent");
		evt.setParams({
			componentDef : "c:Merge_Component_two",
			componentAttributes: { id1 : conId1,id2 : conId2 }
			
		});
		evt.fire();
	}
}

// and the part where you call this function, change as below
if(res.length == 2)
     redirecttoMerge(res[0], res[1]);

Alternatively, you could also filter these records in the soql iteslf but if the exact business requirement is to show an error message, the above code must help.

 

Best,

Vishal