+ Start a Discussion
Jay reddyJay reddy 

matching an opportunity record type to an account record type

When creating an Opportunity I'm trying to setup an opportunity record type/page layout that will automatically be applied based on the Account record type chosen. My users already choose from a drop down with creating an account, so I don't want them to have to do it again when creating an opportunity. it should just be assigned based off the account record type choosen dynamically.

Tried Workflows, PB ... didn't work! Can't use URL hack because of lightning. 
Raj VakatiRaj Vakati
Use process builder .. On opportyinity create or edit 

Condition is Opportunity.Record Type name not equals to Account record type name 

Action : Update the Opportunity record type Id with value based on conditions 
Jay reddyJay reddy
@Raj V ...... Dude!!! Thanks for your reply but please read my requirement again. I stated that I want it to happen dynamically, while creating Opportunity record [when you click "NEW" it should dynamically choose related opportunity record type], not after creating opportunity record.
Raj VakatiRaj Vakati
My mistake Jay .. 
QQ 
Are you creating Opportunity from the Account Related list ? or from the Opportunity tab ? 
Jay reddyJay reddy
@ Raj V ... From the Account Related list
Jay reddyJay reddy
When I click on the below "New" button ... it should read the Account record type and choose the Opportunity record type dynamically.

For instance, if the account is created with record type A... then if I try to create Opportunity from that Account record, the record should be set to  Opportunity record type "A1". let me know if it's clear or not.
User-added image
Jay reddyJay reddy
When I click on the below "New" button ... it should read the Account record type and choose the Opportunity record type dynamically.

For instance, if the account is created with record type A... then if I try to create Opportunity from that Account record, the record should be set to  Opportunity record type "A1" automatically without showing the record selection pop up box. let me know if it's clear or not.
User-added image
Raj VakatiRaj Vakati
You need to overrride the New Button of the Opportunity with Lightning overrides 
Jay reddyJay reddy
I've been trying to override with VF page and Class.... I almost achieved it but "Save and New" and "Cancel" buttons are not acting properly.

It works good when I click "NEW" button but after I click "Save and New" ... it prompts the record type selection pop up box, which I want it act just like "NEW" button. And when I click "Cancel" ... the page gets blank. it doesn't go back to Account record.
<apex:page standardController="Opportunity"
        extensions="OpportunitySpecificationNewController"
        action="{!url }"
        />
 
public with sharing class OpportunitySpecificationNewController extends NewChildControllerBase {
    public OpportunitySpecificationNewController(ApexPages.StandardController ignored) {
        super(Account.SObjectType, Opportunity.SObjectType);
    }
}
 
public abstract class NewChildControllerBase {
    private SObjectType parentSobType;
    private SObjectType childSobType;
    public NewChildControllerBase(SObjectType parentSobType, SObjectType childSobType) {
        this.parentSobType = parentSobType;
        this.childSobType = childSobType;
    }
    public PageReference url() {
        PageReference p = new PageReference('/' + childSobType.getDescribe().getKeyPrefix() + '/e');
        Map<String, String> m = p.getParameters();
        m.putAll(ApexPages.currentPage().getParameters());
        m.put('RecordType', getChildRecordTypeId(getParentSObjectId(m)));
        m.put('nooverride', '1');
      
        
        return p;
    }
    
   
   
    private Id getChildRecordTypeId(Id parentId) {
        SObject parent = Database.query('select id, RecordType.DeveloperName from ' + String.valueOf(parentSobType)
                + ' where Id = :parentId');
        String parentDeveloperName = (String) parent.getSobject('RecordType').get('DeveloperName');
        return [select Id from RecordType where SObjectType = :String.valueOf(childSobType)
                and DeveloperName = :parentDeveloperName].Id;
    }
    private Id getParentSObjectId(Map<String, String> m) {
        for (String key : m.keySet()) {
            if (key.endsWith('_lkid')) {
                return m.get(key);
            } 
        }
        return null;
    }
}