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
Karen HansonKaren Hanson 

Custom button to convert recordtype to anoth with visualforce

Hi
I do have a requirement in which two record type  A and b on opportunity I have created it !
“A”  record type  should have a button convert to “B” record type  which when clicked should display an alert ‘Click yes to Proceed’ if yes, then show a page where amount can be entered. while saving , ensure that it is greater than 10k usd and then redirect to B record type  page
 
Best Answer chosen by Karen Hanson
Abhishek BansalAbhishek Bansal
Hi Karen,

First of all you have to create a custom button with following properties :

1. Detail Page Button
2. Behaviour - OnclickJavascript

Write this code on Button Javascript :

if(window.confirm('click yes to proceed')){
    var win = window.open('/apex/convertToB?id={!Opportunity.Id}', '_blank');
    win.focus();
}

Add this button to Opportunity page layout for Record Type "A" so that this button is only visible to records of Record Type A

Create a controller class (Name - cont )
Code for class :


public class cont {

    public PageReference changeRT() {
        if(opp.Amount < 10000){
            Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR, 'Amount should be greater than 10k'));
        }
        else{
            opp.RecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('B').getRecordTypeId();
            update opp;
        }
        
        return new PageReference('/'+opp.id);
    }

    public Opportunity opp {get;set;}
    public cont(){
        opp = [Select RecordTypeId,Amount from Opportunity where id = :ApexPages.currentPage().getParameters().get('id')];
        opp.Amount = null;
    }
    
}

Now create a New VF page (Name - convertToB)
Code for VF page :


<apex:page controller="cont">
  <apex:form id="frm">
      <apex:pageMessages></apex:pageMessages>
      <apex:inputField value="{!opp.Amount}"/>
      <apex:commandButton value="ChangeRecordType" action="{!changeRT}" reRender="frm"/>
  </apex:form>
</apex:page>

Let me know if you need more help.

Thanks,
Abhishek Bansal