0

Im trying to create a group in a chattool using aura component in salesforce. when i run the code for creating a group in developer console it runs fine. But i want to to execute when user press a button in the aura component on salesforce following is the code i tried.

ChatWorkOperation.cls

public with sharing class ChatWorkOperation {
    @AuraEnabled
    public static String CreateGroup(){
        string result;
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setMethod('POST');
        request.setHeader('X-ChatWorkToken','ba96xxxxxxxxxx0xxxxxxa6f');
        request.setBody('members_admin_ids=5xx836&name=Test2Group');
        request.setEndpoint('https://api.chatwork.com/v2/rooms');
        HttpResponse response = http.send(request);
        Integer status = response.getStatusCode ();
        system.debug(status);
        system.debug(response);
        result = 'Success';
        return result;

    }
}

ChatWork.cmp

<aura:component controller="ChatWorkOperation" implements="flexipage:availableForAllPageTypes,forceCommunity:availableForAllPageTypes" access="global">
<lightning:button label="Group_Create"
                onclick="{!c. creategroupbutton}"/>
</aura:component>

ChatWorkController.js

({
    creategroupbutton : function(component, event, helper) {
        var action = component.get("c.CreateGroup");
        action.set("v.disabled",true)
        $A.enqueueAction(action);
    }
})

Can anyone guide me where Im going wrong?

4
  • If the API version of the apex class is set to an older version, try upgrading to latest version or changing to any other version – Raul Mar 29 at 7:51
  • Just checked its 51 – Tejas Mar 29 at 7:55
  • What are you trying to do with action.set? – Sachin Hooda Mar 29 at 10:41
  • trying to disable the button once pressed. – Tejas Mar 29 at 11:16
1

I can see that you are not setting the callback for the Apex Action Call. And Also, you are setting the disabled attribute to the wrong element.

Use the below code for the JavaScript Controller

({
creategroupbutton : function(component, event, helper) {
    var action = component.get("c.CreateGroup");
    component.set('v.disabled', true);
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            alert("From server: " + response.getReturnValue());
        }
        else if (state === "INCOMPLETE") {
            component.set('v.disabled', false);
        }else if (state === "ERROR") {
            // enable the button if there is any error.
            component.set('v.disabled', false);
            var errors = response.getError();
            if (errors) {
                if (errors[0] && errors[0].message) {
                    console.log("Error message: " + errors[0].message);
                }
            } else {
                console.log("Unknown error");
            }
        }
    });
    $A.enqueueAction(action);
}
})

To disable the button once clicked please use the below code

<aura:component controller="ChatWorkOperation"  impl

ements="flexipage:availableForAllPageTypes,forceCommunity:availableForAllPageTypes" access="global">
<aura:attribute name="disabled" type="Boolean" default="false"/>
<lightning:button label="Group_Create"
    disabled="{!v.disabled}"
    onclick="{!c.CreateGroup}"/>
</aura:component>

Let us know if this works.

5
2

To set an attribute on a component, use component.set, not action.set:

component.set("v.disabled", true);

Note that you haven't set the callback handler, so you have no way to tell if the call failed or not.

1
  • Thank you so much @sfdcfox – Tejas Mar 30 at 7:40

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.