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
Salma ElmoutaouaffikSalma Elmoutaouaffik 

custom object with two record types

I have a custom object with two record types.
my question is how to save the data input in the same vf into the record types ?
for exemple the object is Myobject__c and it has two records objet1 & object2
ManojjenaManojjena
Hi Salma ,

If you have two record type ,Why you want to send data from different record type ,how you want to defind when to save first record type and when to send second record type .Is there any pick list on basis of that you need to save data or nay other identifier .
please confirm .
Salma ElmoutaouaffikSalma Elmoutaouaffik
this my vf 

 
<apex:page standardController="MyObject__c" extensions="MyCustomController">
<apex:form >
<apex:pageBlock title="view">
     Name:{!MyObject__c.Name__c}<br/>
     
  <apex:pageblockSection title="edit" columns="1">
  <apex:inputField value="{!MyObject__c.Name__c}"/>
  
  
  </apex:pageblockSection>
  <apex:commandButton value="save this" action="{!Quicksave}"/>


</apex:pageBlock>
</apex:form>
</apex:page>

Iwant when i click save the value of the inputfield to be saved in one of the record types , how can I refer to the record type??
 
ManojjenaManojjena
Hi Salma,

In your MyCustomController class inside Quicksave method in the MyObject__c record assign like below .
 instanceof rec. RecordTypeId=MyObject__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('YourRecordTypeName').getRecordTypeId();
Let me know any issue .

Thnaks 
Manoj
Salma ElmoutaouaffikSalma Elmoutaouaffik
Thanks Manoj for answering, but it still doesn't work for me; 
i do not have a quicksave method here is my apexcode:
public with sharing class MyCustomController {

    public MyCustomController(ApexPages.StandardController controller) {

    }

}

 
ManojjenaManojjena
Ok ,What is your record type name ?





 
Salma ElmoutaouaffikSalma Elmoutaouaffik
i have two record types their names are for exemple XXX & YYY 
ManojjenaManojjena
Hi Salma,

Try with below code it will save record in XXX record type .
 
<apex:page standardController="MyObject__c" extensions="MyCustomController">
	<apex:form >
		<apex:pageBlock title="view">
			<apex:pageblockSection title="edit" columns="1">
				<apex:inputField value="{!myObj.Name__c}"/>
			</apex:pageblockSection>
			<apex:commandButton value="save this" action="{!doSave}"/>
	   </apex:pageBlock>
	</apex:form>
</apex:page>
public with sharing class MyCustomController {
   public MyObject__c myObj{get;set;}

    public MyCustomController(ApexPages.StandardController controller) {
        myObj=(MyObject__c)controller.getRecord();
    }
	public void doSave(){
	  myObj.RecordTypeId=MyObject__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('XXX').getRecordTypeId();
	  try{
	  Insert de;
	  }catch(dmlException de ){
	    System.debug(de);
	  }
	}
}

Thanks
Manoj
Salma ElmoutaouaffikSalma Elmoutaouaffik
thanks the code is working , but where can i view what is saved ??
ManojjenaManojjena
HI salma,

Try with below code ,Or create a tab for that object and check .
 
<apex:page standardController="MyObject__c" extensions="MyCustomController">
	<apex:form >
		<apex:pageBlock title="view">
			<apex:pageblockSection title="edit" columns="1">
				<apex:inputField value="{!myObj.Name__c}"/>
			</apex:pageblockSection>
			<apex:commandButton value="save this" action="{!doSave}"/>
	   </apex:pageBlock>
	</apex:form>
</apex:page>
public with sharing class MyCustomController {
   public MyObject__c myObj{get;set;}

    public MyCustomController(ApexPages.StandardController controller) {
        myObj=(MyObject__c)controller.getRecord();
    }
	public PageReference doSave(){
	  myObj.RecordTypeId=MyObject__c.sObjectType.getDescribe().getRecordTypeInfosByName().get('XXX').getRecordTypeId();
	  Insert myObj;
	   PageReference pageRef = new ApexPages.StandardController(myObj).view();
        pageRef.setRedirect(true);
        return acctPage;
	  
	}
}

If it solve your problem please select as best answer to help others .
Salma ElmoutaouaffikSalma Elmoutaouaffik
thanks Manoj