1

Using a Before trigger/ Workflow has not helped me fill in or provide a default value to a standard field(NAME) while saving a new record.

I was asked to build a Visual force page and then override the 'NEW' button to add a default value to the token field, type Name, which will then allow to save a record.

Visual force page code used to override 'NEW':

<apex:page standardController="Certification__c"
action="{!URLFOR($Action.Certification__c.New, NULL, ['Name'='DoNotEdit'], true)}"/>

My 'NEW' button before overriding: before

My 'NEW' button after overriding. after

My Contact look-up field value tends to disappear after i override the new button page. Token is the standard field, type Name. My 'Cancel' button is also not working after the override.

How can i bring everything from the default page, but only add a default value to the token(Name) field.

UPDATE - explained further

 Field Label = Token, Field Type = Name, Data Type = Text(80), Indexed, required (cannot be empty)

Name field is required in order to save the record, hence a before trigger won't work here. Name type fields don't take default values and is always a required/mandatory field. So the field Token is a 'text' field which goes like contact.Name, account.Name etc..(goes by .Name) This type of field requires a value upon saving. you cannot also provide a default value. I tried using a workflow, or a before trigger. But these work only after the record is saved but not just before it is saved. I want my user to not input this token every time they create a new record, but pull the value from it's parent object field.

SOLVED: As Brian suggests below, I created a new button. You can use this link URL Hacks My final button URL looks as follows (note always have the retURL or return url) :

/a09/e?CF00NE0000004Nt36={!Contact.Name}&CF00NE0000004Nt36_lkid={!Contact.Id}&Name={!Contact.C_Token__c}&retURL=/{!Contact.Id}
  • 1
    I believe you need to explore all the URLFOR parameters and options. You should be able to set the Contact ID as a parameter in the URLFOR, as well as a return URL when you press the cancel button. – Brian Miller Apr 3 at 6:06
  • Is it possible to clone the existing standard page, and then do the edit's in it – Niveth Kumar Apr 3 at 6:25
  • 1
    I think you can if you use the Web Link option and do some URL hacks there (see here for a sample or just google "button url hacks salesforce"). I'm still not clear on the question - what's the main objective? A trigger might still be useful depending on the end result you're looking for – Brian Miller Apr 3 at 6:37
  • BTW, if you're moving to Lightning, or using Chatter Actions is an option, here is a great post for prepopulating values – Brian Miller Apr 3 at 6:40
  • 1
    I have explained further, please check my update. hope it helps – Niveth Kumar Apr 3 at 6:48
1

If you're trying to prepopulate fields on the UI when the user tries to insert a new record:

  1. In Classic - try using the Web Link option in the new button override option and do some URL hacks as explained here
  2. In Lightning (or Classic using Chatter Actions) - you can use Actions to prepopulate fields and that can be your main entry into inserting a new record. There's a great post on that here

If you're just trying to set the SObject.Name field, this can be achieved through a before trigger.

Here's a sample if you wanted to set the Opportunity Name to "{Account Name} - {Amount}" anytime the Opportunity was created or updated (i.e. it'll always overwrite the user's attempt to change the name):

trigger MyOppTrigger on Opportunity(before insert, before update) {
    for (Opportunity opp : Trigger.new)
        opp.Name = opp.Account.Name + ' - ' + opp.Amount;
}

Note that if you're trying to pull some other value from the Account lookup field, you'll need to do a separate query to grab that other field, or use a formula field to pull that lookup field's value to the opportunity. Then the opp.Name code would look like this:

opp.Name = opp.Cust_Field_on_Account__c + ' - ' + opp.Amount;

At this point, might as well just add the whole logic into the formula field and just do opp.Name = opp.Opp_Auto_Name_Formula_Field__c

  • .Name field is required in order to save the record, hence a before trigger won't work here. Name type fields don't take default values and is always a required/mandatory field. I have mentioned this issue in my previous Update, please have a look – Niveth Kumar Apr 3 at 8:01
  • Error: Invalid Data. Review all error messages below to correct your data. Token Error: You must enter a value – Niveth Kumar Apr 3 at 8:02
  • A before trigger actually runs before the record hits the database - hence setting the Name to a real string value should work. As for Invalid Data error - that error sounds like you're not assigning a value to Name - meaning whatever you're assigning to it might be empty or null. – Brian Miller Apr 3 at 8:37
  • I'm trying to assign a value to this field through the trigger. But when clicking NEW and then SAVE, this required field is left blank. Meaning I want it to retrieve a value from another place and set it to this field before saving the record. Meaning this field is left blank. I can't assign a default value nor set it as 'Not-required' – Niveth Kumar Apr 3 at 8:43
  • 1
    Looks good, thanks a lot your ideas worked. – Niveth Kumar Apr 3 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.