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
JoshGuiJoshGui 

Declare Record Type and a Field Value when guest user adds record

This is probably a simple problem, but can't figure it out myself.  (amateur).

 

I have  a Site with pages, and a custom controller so that a (public) guest user can fill out a form and it then redirects them to a new page with output fields so they can see what they entered.  The problem I am having is that this custom object has "record types" and all submissions are going into the default record type, and I'm not sure how to change that.  I have a different Page for each (record type) submission form, and would like to be able to "set" the record type based on the page they are on.

 

Along the same lines, "during" adding a new record, I'd like to set a custom field "Status_c" to a value - that I can change later in my administrator ui, that the guest user would not be able to see or input or change.  I believe these are related tasks?

 

If anybody could help, or at least point in the right direction, I'd very much appreciate it!!!!

 

 

Here's part of a page and controller:

 

 

<apex:page controller="PIFBonusController" tabStyle="Account" sidebar="false" showHeader="false">
<meta name="robots" content="noindex">
<apex:form >
<apex:pageBlock title="Bonus PIF">
<p/>First Name:<br/><apex:inputField required="true" value="{!newpif.First_Name__c}"/>
<p/>Last Name:<br/><apex:inputField required="true" value="{!newpif.Last_Name__c}"/>

 

<apex:page controller="PIFBonusController" tabStyle="Account" sidebar="false" showHeader="false">

   <apex:form >

   <apex:pageBlock title="Bonus PIF">

       <p/>First Name:<br/><apex:inputField required="true" value="{!newpif.First_Name__c}"/>

       <p/>Last Name:<br/><apex:inputField required="true" value="{!newpif.Last_Name__c}"/>

 

 

   <apex:commandButton value="Submit PIF" action="{!Save}"/>

 

   </apex:pageBlock>

   </apex:form> 

</apex:page>

 

 

 

public with sharing class PIFBonusController {
 
  // the contact record you are adding values to
  public PIF__c newpif {
    get {
      if (newpif == null)
        newpif = new PIF__c();
      return newpif;
    }
    set;
  }
 
  public PIFBonusController() {
    // blank constructor
  }
 
  // save button is clicked
  public PageReference save() {
 
    try {
      insert newpif; // inserts the new record into the database
    } catch (DMLException e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new PIF.'));
      return null;
    }
 
    // if successfully inserted new pif, then displays the thank you page.
    return Page.PIF_Bonus_Thankyou;
  }
 
}

Best Answer chosen by Admin (Salesforce Developers) 
Richie DRichie D

Hi,

 

You could do something like:-

 

newPif = new PIF__c();

List<RecordType> RTs = [select id, name from RecordType where sobjectType='PIF__c'];
    Id recordTypeID = null;
    
    for(RecordType rt : RTs){
        if(rt.name == '[INSERT RECORD TYPE HERE'){
            recordTypeID = rt.Id;
            break;
        }
    }
if(recordTypeID != null)
newpif.recordTypeId = recordTypeID;

 You should be able to set the recordTypeID to which ever one you want.

 

Rich.

 

All Answers

Richie DRichie D

Hi,

 

You could do something like:-

 

newPif = new PIF__c();

List<RecordType> RTs = [select id, name from RecordType where sobjectType='PIF__c'];
    Id recordTypeID = null;
    
    for(RecordType rt : RTs){
        if(rt.name == '[INSERT RECORD TYPE HERE'){
            recordTypeID = rt.Id;
            break;
        }
    }
if(recordTypeID != null)
newpif.recordTypeId = recordTypeID;

 You should be able to set the recordTypeID to which ever one you want.

 

Rich.

 

This was selected as the best answer
JoshGuiJoshGui

Just forgot to say thanks!