1

I'm trying below example which is giving ERROR as response. Any idea on how to get the below code execution successfully:

Component:
<aura:component controller="ConCreation">
    <aura:attribute name="conInfo" type="Contact" default="{'sObject':'conatct','LastName':''}"/>
      <ui:inputText aura:id="LastName" label="Last Name"
                    value="{!v.conInfo.LastName}"
                    required="true"/>

      <ui:inputText aura:id="FirstName" label="First Name"
                    value="{!v.conInfo.FirstName}"
                    required="true"/>    
    <ui:button press="{!c.saveCon}" label="click"/>    
</aura:component>

Aura controller:

({
    saveCon : function(component, event) {
        var con = component.get("v.conInfo");

        var action1 = component.get("c.saveContact");
        action1.setParams({
        "c": con
       });
        action1.setCallback(this, function(response) {
            var state = response.getState();
            console.log('state--'+state);            
            if (component.isValid() && state === "SUCCESS") {
                component.set("v.conInfo", response.getReturnValue());
            }
        });
        $A.enqueueAction(action1);       
    }
})

Aura apex Class:

global with sharing class ConCreation{
    @auraenabled
    public static string saveContact(contact c){
        if(c != null)
        {
            insert c;
            system.debug('c--'+c);
        }
        return c.id;
    }
}
  • what is the error message? – Tushar Sharma Jan 19 '17 at 10:05
  • What error message are you facing? – Pranay Jaiswal Jan 19 '17 at 10:30
  • I'm getting 'Error' in response of java script controller – Rv1 Jan 19 '17 at 10:47
5

You should replace sObject in your default with sobjectType as it is the only way the sobject will be recognised in the controller.

Also, you return the id of the contact in your apex classes and set it as a the v.ConInfo which changes the type of the aura attribute and prevent any use after the first one.

| improve this answer | |
0

Try this ;)

You have to pass function parameter value in String format then deserialize them;

In the component controller pass string instead of a JavaScript object:

action1.setParams({
    "c": JSON.stringify(con)
});

Then in Apex deserialize the string

@AuraEnabled
public static Id saveContact(String c) {
    Contact con = (Contact) Json.deserialize(c, Contact.class);
    System.debug(con);
    if(con != null){
        insert con;
        return con.Id;           
    }
    return null;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.