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
SFDC New learnerSFDC New learner 

how to get the unique id after the record is saved

Hi All,

I am trying to create a new contact. Once the record is saved in Salesforce, an email need to be sent along with the unique id.

I have created a formula field for the Unique ID and associated record ID to this field.

Now, when I try to send an email with Unique ID, it shows null value.

<apex:page controller="SubmitNewContactController" docType="html-5.0">
   
    <apex:form>
        <apex:slds/>
    <apex:pageMessages></apex:pageMessages>
        <div class="slds-scope slds-box">
         <apex:outputPanel >
            <div class="slds-text-align_center slds-text-heading_medium"><p>Submit New Contact </p></div>
             <div class="slds-form-element">
                    <label class="slds-form-element__label"><abbr class="slds-required"></abbr> FirstName</label>
                <div class=" slds-scope slds-form-element__control">
                    <apex:input value="{!FirstName}" styleClass="slds-input" id="FirstName" />
                </div>
                 <label class="slds-form-element__label"><abbr class="slds-required"></abbr> LastName</label>
                 <div class=" slds-scope slds-form-element__control">
                    <apex:input value="{!LastName}" styleClass="slds-input" id="LastName" />
                </div>
                 <label class="slds-form-element__label"><abbr class="slds-required"></abbr> Phone</label>
                 <div class=" slds-scope slds-form-element__control">
                    <apex:input value="{!Phone}" styleClass="slds-input" id="Phone" />
                </div>
                 <label class="slds-form-element__label"><abbr class="slds-required"></abbr> Email</label>
                 <div class=" slds-scope slds-form-element__control">
                    <apex:input value="{!Email}" styleClass="slds-input" id="Email" />
                </div>
                 <label class="slds-form-element__label"><abbr class="slds-required"></abbr> Zipcode</label>
                 <div class=" slds-scope slds-form-element__control">
                    <apex:input value="{!Zipcode}" styleClass="slds-input" id="Zipcode" />
                </div>
                 <div class=" slds-scope slds-form-element__control">
                    <apex:commandButton action="{!SubmitContact}" value="Submit"/> 
                </div>
            </div>
         </apex:outputPanel>   
        </div>
        
    
    </apex:form>
</apex:page>
public class SubmitNewContactController {
    public Contact c{get;set;}
    public String FirstName{get;set;}
    public String LastName{get;set;}
    public String phone{get;set;}
    public String Email{get;set;}
    public String Zipcode{get;set;}
    
    public SubmitNewContactController(){
        c = new Contact();
    }
    
    public PageReference SubmitContact(){
        c.FirstName = FirstName;
        c.LastName = LastName;
        c.phone = phone;
        c.Email = Email;
        c.MailingPostalCode = Zipcode;
        insert c;
        
        PageReference pr = new PageReference('/apex/ThankYouPage');
        Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
        String[] sendingTo = new String[]{c.Email};
        semail.setToAddresses(sendingTo);
        semail.setSubject('Registration is completed');
        semail.setPlainTextBody('Hi'+ c.name+'Thank you for registering.This is your UniqueID'+ c.Unique_ID__c);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});
        pr.setRedirect(true);
        return pr;
        
        
        
    }
    

}

I am able to send the email but cannot capture the name and uniqueId in the email. It shows null in the email.
Can anyone please help me to fix the code?

Thanks,
Sirisha
 
Best Answer chosen by SFDC New learner
Anant KamatAnant Kamat
Your formula will have the value once the record is created in Salesforce. However without querying the fields explicitly, you will not be able to pass them to the email message. Fire the query below and get the values.

Contact c1 = [Select id,Unique_ID__c, Name from Contact where id = :c.id];

The use the c1.Name and c1.Unique_ID__c in your email message.

Let me know if it is working for you.

All Answers

Anant KamatAnant Kamat
Your formula will have the value once the record is created in Salesforce. However without querying the fields explicitly, you will not be able to pass them to the email message. Fire the query below and get the values.

Contact c1 = [Select id,Unique_ID__c, Name from Contact where id = :c.id];

The use the c1.Name and c1.Unique_ID__c in your email message.

Let me know if it is working for you.
This was selected as the best answer
Madhukar_HeptarcMadhukar_Heptarc
Hi Siri,

Can you try replacing below controller code
public class SubmitNewContactController {
    public Contact c{get;set;}
    public String FirstName{get;set;}
    public String LastName{get;set;}
    public String phone{get;set;}
    public String Email{get;set;}
    public String Zipcode{get;set;}
    
    public SubmitNewContactController(){
        c = new Contact();
    }
    
    public PageReference SubmitContact(){
        c.FirstName = FirstName;
        c.LastName = LastName;
        c.phone = phone;
        c.Email = Email;
        c.MailingPostalCode = Zipcode;
        insert c;
 
        Contact ConDetails = [Select id, Name,Unique_ID__c,Email,Phone from Contact where id = :c.id];
        system.debug('Contact Details'+ConDetails);
        
        PageReference pr = new PageReference('/apex/ThankYouPage');
        Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
        String[] sendingTo = new String[]{c.Email};
        semail.setToAddresses(sendingTo);
        semail.setSubject('Registration is completed');
        semail.setPlainTextBody('Hi'+ ConDetails.name+'Thank you for registering.This is your UniqueID'+ ConDetails.Unique_ID__c);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});
        pr.setRedirect(true);
        return pr;
        
        
        
    }
    

}
Please let me know if it is useful. 

Thanks,
Madhukar 
 
Deepali KulshresthaDeepali Kulshrestha
Hi,

You have to send the setTargetObjectId and What Id Along with the Mail String as you want to show the detail of a contact record so you have to pass the contact Id in setTargetObjectId.
Please try the below code as it may help you.

        Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
        String[] sendingTo = new String[]{c.Email};
        semail.setToAddresses(sendingTo);
        semail.setSubject('Registration is completed');
        semail.setTargetObjectId(C.Id);
        semail.setWhatId(C.Account.Id);
        semail.setPlainTextBody('Hi'+ c.name+'Thank you for registering.This is your UniqueID'+ c.Unique_ID__c);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {semail});
       
        

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha

 
SFDC New learnerSFDC New learner
Hi Anant/Madhukar,

Thanks for the quick response. Your solution worked. 

Can you please help to create a formula which should generate random number or alphanumeric instead of Record ID. The number should be 5 digits.

Thanks,
Sirisha
Anant KamatAnant Kamat
Hi Sirisha,
Try using the below one in your formula.

LEFT(TEXT( SQRT( (VALUE( (LEFT(RIGHT(TEXT( CreatedDate ),6),2))& TEXT(DAY(DATEVALUE(CreatedDate)))& TEXT(MONTH(DATEVALUE(CreatedDate)))& TEXT(YEAR(DATEVALUE(CreatedDate)))& (LEFT(RIGHT(text(CreatedDate),6),2))) )*10 ) ) ,3 ) 

Let me know if it is working.
SFDC New learnerSFDC New learner
Hi Anant,

Sorry for the late reply. Your solution did work, but I have created the Autonumber datatype field for now.

I want to display the error message near the field if the values are empty or null. Trying to do as below

<apex:page controller="SubmitNewContactController" sidebar="false" showHeader="false" docType="html-5.0">
    
    <apex:form >
        <apex:slds />
    <apex:Messages id="messages"></apex:Messages>
        <div class="slds-scope slds-box">
         <apex:outputPanel >
            <div class="slds-text-align_center slds-text-heading_medium"><p><b>Submit New Contact</b> </p></div>
             <div id="formBlock">
                 
             
             <div class="slds-form-element">
                    <label class="slds-form-element__label"><abbr class="slds-required">*</abbr> FirstName</label>
                <div class=" slds-scope slds-form-element__control">
                    <apex:input value="{!FirstName}" styleClass="slds-input" id="FirstName"/>
                    <apex:actionSupport reRender="error" rendered="{!IF(ISNULL(FirstName),true,False)}" />
                </div>
                 <apex:outputPanel  id="error" rendered="{ISNULL(messages)}">
                 <div class="slds-form-element slds-has-error" >
                  <div class="slds-form-element__help" id="error-message-unique-id">This field is required</div>
                 </div>
                     </apex:outputPanel>
                 <label class="slds-form-element__label"><abbr class="slds-required">*</abbr> LastName</label>
                 <div class=" slds-scope slds-form-element__control">
                    <apex:input value="{!LastName}" styleClass="slds-input" id="LastName" />
                </div>
                 <label class="slds-form-element__label"><abbr class="slds-required">*</abbr> Phone</label>
                 <div class=" slds-scope slds-form-element__control">
                    <apex:input value="{!Phone}" styleClass="slds-input" id="Phone"  />
                </div>
                 <label class="slds-form-element__label"><abbr class="slds-required">*</abbr> Email</label>
                 <div class=" slds-scope slds-form-element__control">
                    <apex:input value="{!Email}" styleClass="slds-input" id="Email"  />
                </div>
                 <label class="slds-form-element__label"><abbr class="slds-required"></abbr> Zipcode</label>
                 <div class=" slds-scope slds-form-element__control">
                    <apex:input value="{!Zipcode}" styleClass="slds-input" id="Zipcode"  />
                </div>
                 <div class=" slds-scope slds-form-element__control">
                    <apex:commandButton action="{!SubmitContact}" value="Submit"/> 
                </div>
            </div>
                 </div>
         </apex:outputPanel>   
        </div>
            
        
    
    </apex:form>
</apex:page>

Can anyone please help me on this validation. It is allowing me to save the record instead of showing the error message.

Thanks,
Sirisha