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

Javascript Buttons Broken

Hello!

We have made the jump to lightning. Our Javascript buttons are now unavailable and the button conversion app says we must manually convert them (I have no idea where to begin with this).

Attached is the button code that we would like to convert to work in lightning.

Essentially the button switched record type from one to another without hitting the edit screen.

I look forward to your expert responses.

Kind regards,

Administrator 5Administrator 5
JS Button Attached now
Durga PaavanDurga Paavan
Hi Administrator S,

Please find below steps:
1. Create Lightning component which implements 'force:hasRecordId' and define record id attribute. 
2. You can fetch the case record from Apex method in Init Method of lightning component using recordId attribute.
3. If it is closed throw an error otherwise call an another apex method which simply updates the recordId.
4. Once the response status is success then reload the page.

Hope above steps helps.

Cheers,
Durgapaavan
Khan AnasKhan Anas (Salesforce Developers) 
Hi Administrator,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Apex:
public class UpdateRecordTypeC {
    
    @AuraEnabled
    public static void updateRT(String rid) {
        Case a = [SELECT Id,RecordTypeId FROM Case WHERE Id = :rid];
        a.RecordTypeId = '0127F000000ykIaQAI'; //Please query the record type instead of hardcoding it
        update a;
    }
}

Component:
<aura:component controller="UpdateRecordTypeC"
                implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
	
    <lightning:button label="Update Record Type" onclick="{!c.doUpdateRecordType}"/>
</aura:component>

Controller:
({
    doUpdateRecordType : function(component, event, helper) {
        var r = component.get('v.recordId');
        console.log('r -> ' + r);
        var action = component.get('c.updateRT');
        action.setParams({rid : r});
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                // Do something
            }
        });
        $A.enqueueAction(action);
    }
})

Add this component to record page. Please refer to the below link which might help you further.

https://trailhead.salesforce.com/en/content/learn/projects/quickstart-lightning-components/quickstart-lightning-components3

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas