0

I have a component with 3 button with name of record Type. I need that when I click on a certain button, the Record Type of the account record will be update.My component

that's what i have:

ComponentButtons.cmp

<aura:component description="ComponentButtons"
                controller="AccountTransferCont" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction"
                access="global">
    <!--iconName="action:change_record_type"-->
    <lightning:overlayLibrary aura:id="overlayLib"/>
    <lightning:card>
        <div class="slds-grid slds-einstein-header slds-card__header">
            <header class="slds-media slds-media_center slds-has-flexi-truncate">
                <div class="slds-grid slds-grid_vertical-align-center slds-size_3-of-4 slds-medium-size_2-of-3">
                    <div class="slds-media__body">
                        <h2 class="slds-truncate" title="Einstein (10+)">
                            <a href="javascript:void(0);" class="slds-card__header-link" title="Einstein (10+)">
                                <span class="slds-text-heading_small">Account Transfer1</span>
                            </a>
                        </h2>
                    </div>
                </div>
                <div class="slds-einstein-header__figure slds-size_1-of-4 slds-medium-size_1-of-3"></div>
            </header>
        </div>
        <div class="c-container">

            <lightning:layout>
                <lightning:layoutItem padding="around-small">
                    <lightning:button label=" - >  Generic Sales" variant="brand"
                                      onclick="{!c.doGeneric}"/>
                </lightning:layoutItem>
            </lightning:layout>


            <lightning:layout>
                <lightning:layoutItem padding="around-small">
                    <lightning:button label=" - >  Foreign Sales"  variant="brand"
                                      onclick="{!c.doForeign}"/>
                </lightning:layoutItem>
            </lightning:layout>

            <lightning:layout>
                <lightning:layoutItem padding="around-small">
                    <lightning:button label=" - >  Military Sales"  variant="brand"
                                      onclick="{!c.doMilitary}"/>
                </lightning:layoutItem>
            </lightning:layout>

        </div>
    </lightning:card>
</aura:component>

ComponentButtonsController.js

({doGeneric: function (component, event, helper) {
        alert("Nowhere to convert!");
    },

    doForeign: function (component, event, helper) {

    },

    doMilitary: function (component, event, helper) {

    }
});

ApexController AccountTransferCont.apx

public without sharing class AccountTransferCont {
    @AuraEnabled
    public static void Military(String RecordID) {
        Account a = [SELECT Id,RecordTypeId FROM Account WHERE Id = :RecordID];
        RecordType rt = [SELECT Id FROM RecordType WHERE Name = 'Military' AND SobjectType = 'Account' LIMIT 1];
        a.RecordTypeId = rt.Id;
        update a;
    }
}
2
  • I am sure you are able to catch system debug statements...so after you click the button, just try refreshing url manually and see if you can able to get this record type...If that works, then this is something this can help simpluslabs.com/…
    – Pratap M
    May 27, 2019 at 0:56
  • 1
    Welcome SFSE community! What is a question actually? Do you face any problem, any error during code execution? May 27, 2019 at 5:41

1 Answer 1

2

Just pass record id and record type name into your controller to update the current record:

<!--component-->
..........
<!--you can use the same onclick event handler for each button changing only its value-->
<lightning:button label=" - >  Generic Sales" value="Generic Sales" variant="brand" onclick="{!c.doUpdateRecordType}"/>
..........

<!--controller-->
({
    doUpdateRecordType: function (component, event, helper) {
        // as long as your component implements force:hasRecordId
        const id = component.get('v.recordId);
        // you can keep your record type name as value of value attribute of 
        // lightning:button thus reducing repeteable code
        const recordTypeName = event.getSource().get('v.value');
        const action = component.get('c.updateRecordType');
        action.setParams({ id, recordTypeName });
        // If you want to get and process response
        // action.setCallback(this, (response) => {});
        $A.enqueueAction(action);
    },
});

<!--apexController-->
public without sharing class AccountTransferCont {
    @AuraEnabled
    public static void updateRecordType(String id, String recordTypeName) {
        Account a = [SELECT Id,RecordTypeId FROM Account WHERE Id = :id];
        RecordType rt = [SELECT Id FROM RecordType WHERE Name = :recordTypeName AND 
        SobjectType = 'Account' LIMIT 1];
        a.RecordTypeId = rt.Id;
        update a;
    }
}
7
  • I am not sure your answer really help...Because for every record type he has different method...
    – Pratap M
    May 27, 2019 at 0:53
  • 1
    So, what I'm telling is that it's possible to reduce repeatable code. If the logic of each button differs only in its value, which needs to be passed, then there is no point in writing separate methods for each button. May 27, 2019 at 5:06
  • 1
    Maybe I've missed something but the question was 'I need that when I click on a certain button, the Record Type of the account record will be update.'. I didn't see any issues with updating. May 27, 2019 at 5:31
  • 1
    it was perfect answer for me, thank you, Oleg, veeery much! ☺️ May 30, 2019 at 12:17
  • 1
    You're welcome and have a nice day))) May 30, 2019 at 12:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .