function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Paul BrainardPaul Brainard 

Javascript button conversion to lightning

What is the best way to convert the following button in Classic to a Lightning component (or other option):

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

var newRecords = []; 

var IR = new sforce.SObject("Incident_Report__c"); 
IR.id ="{!Incident_Report__c.Id}"; 
IR.Generate_Incident_Report__c =  1; 
newRecords.push(IR); 

result = sforce.connection.update(newRecords);
alert("Incident Report:\nPlease refresh the page if you do not see the new Incident Report document created under Notes & Attachments");
window.location.reload();

Please, and thank you!

Paul
sfdcMonkey.comsfdcMonkey.com
Hi Paul,
 the best way is lightning component, create a quick action on your record detail page and use following lightning component :

apex class for lightning component :
public class IncidentReportCtrl{
 @AuraEnabled 
 
  public static void updateIncidentReport(string recId){
    // ### CHANGE youObjectName__c WITH YOUR MAIN OBJECT API NAME 
	  youObjectName__c obj ;

	  for(youObjectName__c temp : [select id,Incident_Report__c from youObjectName__c where id =: recId LIMIT 1]){
		  obj = temp;
	  }
	 
	  Incident_Report__c  IR = new Incident_Report__c();
         IR.id = obj.Incident_Report__c
         IR.Generate_Incident_Report__c =  1; 
	
	update 	IR; 
	
  } 
}
NOTE :CHANGE youObjectName__c WITH YOUR MAIN OBJECT API NAME IN APEX CLASS

lightning component :
<aura:component controller="IncidentReportCtrl" implements="force:lightningQuickAction">
   <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>     
    <lightning:spinner variant="brand" size="large"/>
</aura:component>
javaScript controller:
({
 doInit : function(component,event,helper){
      var action = component.get("c.updateIncidentReport");
        action.setParams({
            "recId": component.get("v.recordId")
        });
        
        action.setCallback(this, function(response) {
            if (response.getState() == "SUCCESS") {
			   alert("Incident Report:\nPlease refresh the page if you do not see the new Incident Report document created under Notes & Attachments");
               $A.get('e.force:refreshView').fire();
            }else{
                alert('Callback Failed...');
            }
        });
        $A.enqueueAction(action);
 },
 
})

for reference : http://sfdcmonkey.com/2017/04/16/add-lightning-component-lightning-action/

Kindly let us know if it helps you, & close this query by selecting best answer if you got your solution,so this will helps other in future
 
http://sfdcMonkey.com