+ Start a Discussion
Cloud EliteCloud Elite 

build lightning component to accept cases

Hello All, 

I tried to build a lightning component to have a button that when clicked, the case is accepted to the user who cliced it, mainly a case that was assigned to a queue. 

here is the code i have so far but need your help in what is missing, when i try to save the controller, i get an error. can you please review and correct ? 
 
component 
==========

<aura:component implements="force:hasRecordId,force:lightningQuickActionWithoutHeader">
  <aura:attribute name="record" type="Case" default="{ 'sobjectType': 'Case' }" />
  <aura:attribute name="complete" type="Boolean" default="false" />
  <force:recordData recordId="{!v.recordId}"
                    fields="['CaseNumber','OwnerId','OwnerFullName']"
                    targetFields="{!v.record}"
                    aura:id="recordData"
                    recordUpdated="{!c.recordLoaded}" />

</aura:component>
====


controller 
======= 

({ 
  recordLoaded: function(component, event, helper) {
    var caseRecord = component.get("v.record"),
      recordData = component.find("recordData");
    caseRecord.OwnerId = component.get("$SObjectType.CurrentUser.Id");
    if(!component.get("v.complete")) { // Avoid infinite loop
      component.set("v.complete", true);
      component.set("v.record", caseRecord);
      recordData.saveRecord($A.getCallback(function(result) {
        if(result.state === "SUCCESS" || result.state === "DRAFT") {
          $A.get("e.force:closeQuickAction").fire();
          $A.get("e.force:refreshView").fire();
        } else { /* show an error here */ }
      }));
  }
})

 
Alain CabonAlain Cabon
Hi,

You can get more details when there is an error with the console.log by testing all the states.
handleSaveRecord: function(component, event, helper) {
        component.find("recordHandler").saveRecord($A.getCallback(function(saveResult) {
            // NOTE: If you want a specific behavior(an action or UI behavior) when this action is successful 
            // then handle that in a callback (generic logic when record is changed should be handled in recordUpdated event handler)
            if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
                // handle component related logic in event handler
            } else if (saveResult.state === "INCOMPLETE") {
                console.log("User is offline, device doesn't support drafts.");
            } else if (saveResult.state === "ERROR") {
                console.log('Problem saving record, error: ' + JSON.stringify(saveResult.error));
            } else {
                console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
            }
        }));
    },

By giving the exact error:   console.log('Problem saving record, error: ' + JSON.stringify(saveResult.error)); that may help in understanding the problem.

You are just using result instead of saveResult (but that is not important).

 
Cloud EliteCloud Elite
The error i get is when i save the controller with the above code i provided for the controller. see screen shot for the error User-added image
Alain CabonAlain Cabon
If it is the complete code for the controller, a parenthesis is missing at the end.
 
({ 
    recordLoaded: function(component, event, helper) {
        var caseRecord = component.get("v.record"),
            recordData = component.find("recordData");
        caseRecord.OwnerId = component.get("$SObjectType.CurrentUser.Id");
        if(!component.get("v.complete")) { // Avoid infinite loop
            component.set("v.complete", true);
            component.set("v.record", caseRecord);
            recordData.saveRecord($A.getCallback(function(result) {
                if(result.state === "SUCCESS" || result.state === "DRAFT") {
                    $A.get("e.force:closeQuickAction").fire();
                    $A.get("e.force:refreshView").fire();
                } else { /* show an error here */ }
            }));
        }
    }
})

 
Alain CabonAlain Cabon
The javascript parser for the controller is misleading and the error should say "missing parenthesis" simply but there is a long complicated popup instead (?).
Cloud EliteCloud Elite
thanks for getting back real quick. that is funny that i small thing like this cause that.  i saved the component but i am still not able to use it to take ownership of the case. what am i missing ? I am trying to use this as a button that when clicked it will change the owner to the user clicked the button. 
Alain CabonAlain Cabon
That doesn't work like that since the version 43. 

Get the Current User’s IDhttps://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.get_current_user
... but that is not Aura anymore but the replacing framework of Aura called Lightning Web Components
and it needs Salesforce DXhttps://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.install_setup_develop

You can try; https://success.salesforce.com/ideaView?id=0873A000000E7lFQAS
Undocumented workaround: JS Controller: $A.get('$SObjectType.CurrentUser.Id');

A complete solution by reading the user with an Apex class:
http://sfdcmonkey.com/2018/01/08/display-current-user-information/
They also talked about the undocumented workaround but that doesn't work anymore since the version 43.

For a so basic need, the Apex controller could be the only remaining technique (outside the LWC and the complete rewriting of your code + DX).