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
belabela 

unable to insert record based on record type

Hi All,
I am trying to insert record based on the record type.but while trying the the logic is not going into the if condition.and is the write way to take hardcode id for inserting record ?please help me below is the code.

public with sharing class insertinrecordtypecontroller {

public String name { get; set; }
public String value { get; set; }

public PageReference Insertvalue() {
    position__C po=new position__c();
        system.debug('this is insert value 111111111');

    if(po.RecordTypeId =='01228000000Aif1AAC'){
    po.name=name;
    po.value__c=value;
    system.debug('this is insert value');
    insert po;
    }
    return null;
  }
}


visual force:
=----------------

<apex:page controller="insertinrecordtypecontroller">
<apex:form >
 <apex:pageBlock >
 <apex:pageBlockSection >
 <apex:inputText label="name" value="{!name}" />

<apex:inputText label="value" value="{!value}" />
</apex:pageBlockSection>

 </apex:pageBlock> 
 <apex:commandButton value="save" action="{!Insertvalue}"/>

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


Thanks,
B.Mahanandeesh.
Best Answer chosen by bela
Ajay Ghuge 6Ajay Ghuge 6
Hi,

Just remove the If . It is not required. As you are not setting value to po.recordtypeid so it is blank also you are comparing blank with string having value.

Also add the debugs before if to check values of string.

Hope this helps.

Regards,
Ajay

All Answers

Ajay Ghuge 6Ajay Ghuge 6
Hi ,

Hardcoding recordtypeid is not at all the way to do this. Also, your code will assign the record type id to the po object.

For getting recordtypeid use following way :
string strRecordTypeId = Schema.SObjectType.position__C.RecordTypeInfosByName.get('RecordTypeName').RecordTypeId

RecordTypeName is the recordtype of which you want Id.

Assign it to the :
po.recordtypeId = strRecordTypeId ;

Regards,
Ajay
belabela
Hi Ajay,

thanks for the response,I have used  in the same way creating utility class.but also i am unable to insert record based on record type .
I wrote if condition and its not entering to the if condition.if we remove if condition  and writing in the below way its working.what is the problem with if condition.please help me.

2.And also please check my utility class I have one doubt
.Map<String, schema.recordtypeinfo> recordTypeInfo = position__c.sObjectType.getDescribe().getRecordTypeInfosByName();

I declared postion object here,so i can get only position object recordtypeinfo.but the functionality of utility class is not that.it has to take all objects in utility class and based on the requirement we use class name and call this method and write out required object like below
 public string recordtypeid=recordtyperetreive.getObjectRecordTypeName(Position__c.SObjectType,'ITPosition');

but if i want to take contact object recordtype,again i have to change contact in place of postion in utility class
.I think i did wrong please suggest me how to achive this .





po.recordtypeid == recordtypeid;
    po.name=name;
    po.value__c=value;
    insert po;
    
    return null;
  }

//utility class
-------------------
public class recordtyperetreive{
public static String getObjectRecordTypeName(SObjectType sObjectType, string recordTypeName)

{

//Generate a map of tokens for all the Record Types for the desired object

Map<String, schema.recordtypeinfo> recordTypeInfo = position__c.sObjectType.getDescribe().getRecordTypeInfosByName();

if(!recordTypeInfo.containsKey(recordTypeName))

            throw new RecordTypeException('Record type "'+ recordTypeName +'" does not exist.');

//Retrieve the record type id by name
return recordTypeInfo.get(recordTypeName).getRecordTypeId();
}
    public class RecordTypeException extends Exception{}
}


Controller:
---------------
public with sharing class insertinrecordtypecontroller {

public String name { get; set; }
public String value { get; set; }
public list<Position__c> p{set;get;}
public string recordtypeid=recordtyperetreive.getObjectRecordTypeName(Position__c.SObjectType,'ITPosition');


public PageReference Insertvalue() {
    position__C po=new position__c();
        system.debug('this is insert value 111111111');

    if(po.recordtypeid == recordtypeid){
    po.name=name;
    po.value__c=value;
    insert po;
    }
    return null;
  }


}-

thanks
B.Mahanandeesh.

 
Ajay Ghuge 6Ajay Ghuge 6
Hi,

Just remove the If . It is not required. As you are not setting value to po.recordtypeid so it is blank also you are comparing blank with string having value.

Also add the debugs before if to check values of string.

Hope this helps.

Regards,
Ajay
This was selected as the best answer