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
Praveen MeharPraveen Mehar 

Not showing the result.Please help me

public class N1apex {
   @AuraEnabled
    public static ID mySearch(String phon){
        Contact acc=[select ID,Phone,AccountId from Contact where Phone=:phon];
        return acc.AccountId;
    }
    @AuraEnabled
    public static string names(String firstname,String lastname,ID accountId){
        String status;
        try{
        contact c=new contact();
        c.FirstName=firstname;
        c.LastName=lastname;
        c.AccountId=accountId;
        insert c;
        status='Record inserted';
    }
        catch(Exception e){
            status='Record Failed';
        }
        return status;
}
}
----------------------------------------------------------------------------------------------------------------
<aura:event type="COMPONENT" description="Event template" >
    <aura:attribute name="accountId" type="ID"/>
</aura:event>

----------------------------------------------------------------------------------------------------------------
<aura:component controller="N1apex">
    <aura:registerEvent name="myEvent" type="c:N1Event" />
    
    <div>
        <lightning:input label="phone" aura:id="pname"/>
        <lightning:button label="submit" onclick="{!c.show}"/>
    </div>
</aura:component>

({
	show : function(component, event, helper) {
    	var phone1=component.find("pname").get("v.value");        
        
        var action=component.get("c.mySearch");
        action.setParams({"phon":phone1});
        action.setCallback(this,function(response){
            console.log("Method invoked");
            var state=response.getState();
            if(state==='SUCCESS'){
                console.log("Operation Successfull");
                var accid=response.getReturnValue();
				var evt=component.getEvent("myEvent");
                evt.setParams({"accountId":accid});
                evt.fire();
            }
                else{
                    console.log("Operation Failed");
                }                
            }
        );
        $A.enqueueAction(action);
	}
})

----------------------------------------------------------------------------------------------------------------
<aura:component controller="N1apex">
    <aura:handler name="myEvent" event="c:N1Event" action="{!c.invoke}"/>
    <lightning:input label="LastName" aura:id="lname"/>
    <lightning:input label="FirstName" aura:id="fname"/>
    <c:N1Event_Child />
</aura:component>

({
	invoke : function(component, event, helper) {
        var lname=component.find("lname").get("v.value");
        var fname=component.find("fname").get("v.value");
        var acctid=component.getParams("accountId");
        var action=component.get("c.names");
        action.setParams({"lastname":lname,"firstname":fname,"accountid":acctid});
        action.setCallback(this,function(response){
            var state=response.getState();
            if(state==='SUCCESS'){
                alert(response.getReturnValue());
            }
            else{
                alert('Failed');
            }
        });
        $A.enqueueAction(action);
        
	}
})

--------------------------------------------------------------------------------------------------------------

<aura:application extends="force:slds">
    <c:N1Event_Parent />
</aura:application>

 
ShirishaShirisha (Salesforce Developers) 
Hi Praveen,

Greetings!

You can debug the lightning components using the lightning inspector easily.The below document and video will guide you on the same.

https://www.youtube.com/watch?v=YgL0UNVoqQ8

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/debug_intro.htm

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
MoonpieMoonpie
Praveen,

What result are you expecting what result are you getting?
Praveen MeharPraveen Mehar

Hi Moonpie,
the expected result is if I create contact with phone field value passed as already existing in the contact, then for the same account the new contact is linked.

MoonpieMoonpie
Hi Praveen,

And what result are you getting?
MoonpieMoonpie
Would it happen to be mixing up your first and last names on the newly created Contact record?