0

thank you in advance for your assistance. We have a Javascript Button "Accept Case" which checks the owner. If the owner is not the existing owner, then you can click on this button and taken the ownership. If not you will get an error message. Also the button checks, if the case was updated within the last 5 minutes. If yes you cannot change and taken the ownership of this case:

{!REQUIRESCRIPT("/soap/ajax/26.0/connection.js")}

var CaseNum = '{!Case.CaseNumber}'
var UserId = '{!User.Id}';
var ExistingOwner = '{!Case.OwnerId}'
var ExistingOwnerName = '{!Case.OwnerFullName}'

if ( UserId == ExistingOwner) {
alert ('You already own the case');

}
else {
var caseObj = new sforce.SObject("Case");
caseObj.Id = '{!Case.Id}';
caseObj.OwnerId= '{!$User.Id}';
var dtNow = new Date();
var nowInJSON = dtNow.toJSON();
caseObj.Last_Edit_Date_Time__c = nowInJSON;
var result = sforce.connection.update([caseObj]);
if (result[0].success=='false') {
alert(result[0].errors.message);
location.reload(true);
} else {
alert('You have taken ownership from ' + ExistingOwnerName);
location.reload(true);
;
}
}

Now we want to use this in lighnting - can anyone help us? I am looking for detailed step-by-step instructions to convert this to Lightning Component.

2

The general idea is that you'd want to write a component as a Quick Action. This trailhead actually covers everything you need to know, but the general idea is that you'd have a component that starts off like this:

<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>

From there, you just need to write your recordLoaded controller function:

({ 
  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 */ }
      }));
  }
}) 

Note: this doesn't include the extra logic for checking "last five minutes" or setting the Last_Edit_Date_Time__c field, etc, but is rather meant as a simple demonstration to get you started. I really recommend reading the trailhead, as it'll make a lot more sense that way.

  • Hi, thank you for your response. This helps me a lot and I have used also the trailhead. Do I also need an apex class to find the case records? Normally I thought not but now, I will get an error message on the quick action, that the variable "caseRecord" is null. – Anne Winter-Schwarz Jul 25 '18 at 13:21
  • @AnneWinter-Schwarz You need to wait for recordUpdated to fire, and have it bound to an attribute so you can read it. – sfdcfox Jul 25 '18 at 14:28

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.