0

I'm creating a form for a community using a lightning component. I'm trying to give the case object it's field values before I send it to the apex controller. This isn't working for me.

form.cmp

<aura:component controller="CommunityCaseFormController" access="global" implements="forceCommunity:availableForAllPageTypes">

<aura:attribute name="case" type="Object" default="{}"/>    

<ui:inputText aura:id="subject" label="Subject" />
<ui:inputText aura:id="description" label="Problem Description" />

<div onclick="{!c.handleCreateCase}" class="new-ticket">
    <button type="button"><a href="#">Create Case</a></button>
</div>   
</aura:component>

controller.js

({
    handleCreateCase: function (component, event, helper) {
        var c = component.get("v.case"); 
        console.log(c);

        c['Subject'] = component.find("subject").get("v.value"); 
        c['Description'] = component.find("description").get("v.value"); 
        console.log(c);

        component.set('v.case', c);
    }, 
})

enter image description here

enter image description here

The controller code appears to stop as soon as I start to assign attributes and values to the case object.

  • why dont you use the lightning:recordEditForm? – glls Feb 12 at 0:53
  • @glls It didn't seem necessary. – Tyler Zika Feb 12 at 0:56
  • youll avoid having to use an apex controller, and most of your client side controller logic (in most cases) – glls Feb 12 at 0:57
  • in any case, if you absolutely need it this way, try declaring an empty map/object before fetching your case, then add your case map/object to your new variable then update it, and then set the new case attribute to your updated object – glls Feb 12 at 0:59
  • @glls all I want to do is create a record. I don't need to update. – Tyler Zika Feb 12 at 1:00
0

Your issue seems to be while accessing the case attribute from the component in controller. If you want to just set the values being captured in the form and send it to the controller as in your current implementation, you will need to take the below approach.

handleCreateCase : function(component, event, helper) {

     var c;

     // create the object and then set the attributes right here on the object
     c = {'sobjectType' : 'Case', 
          'Subject' : component.find("subject").get("v.value"), 
          'Description' : component.find("description").get("v.value")}; 

    console.log(c);

    // now set the JSON created above to the attribute defined on your component
    component.set("v.case", c);

    // rest of the code
}

And as @glls has recommended in the comment, do take a look at lightning:recordEditForm if you need to create records without the need of writing custom controllers.

  • ahhh, so you can't use a generic object type... this works too <aura:attribute name="case" type="Case" default="{}"/> – Tyler Zika Feb 12 at 1:03
  • Yes, no change on the attribute declaration as on your component. Whatever you create in the JS will override it. – Jayant Das Feb 12 at 1:05

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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