181. Lets consider your custom Object named “Training__c” has field “Trainer__c”. You have set some default value in that field. Will that default value apply to new record created by apex code ?ORHow to make sure that record created from apex code should respect default value of fields ?ORDefault value in field from Apex code.Ans :

After API 20, it should automatically populate However there is known issue for same here, click here if it impacts you.Workaround :
If Default value of field is not getting populated by Apex then we have to use “Dynamic Apex”. Create instance of object from sObjectType like shown below:

182. What is best practice to refer dynamic custom messages in Visualforce with multi-language support ? Ans :
Using Custom Label or OutputField or InputField tag, Platform itself will take care of internationalization. However in some cases, Message needs to be dynamic at the same time it should also support muti-language. In Custom Label, we cannot save dynamic String.

Let’s assume we want to show message something like “DEVELOPERNAME is not authorized to access this page”.
Here, Developername should be dynamically changed in visualforce which supports multilanguage. For each developername, it is not feasible to create custom labels. So below workaround can be used :

Step 1 : Create a Custom Label with text “{0} is not authorized to access this page“. In every language, dynamic value should represented by {0}.

There may be scenario in Salesforce that you need to send a Visualforce page rendered as PDF as a part of Email Attachment. This will be very easy if you want to perform this using Controller or Extension class, we just have to call getContentAsPDF() method of PageReference class and use returned blob data as a attachment in Email.

Update – 21 Oct 2015 (Winter 16)

After Winter 16 release, this solution will not work as getContent() method is treated as callout and if we try to call it from Async Apex or Rest API in this case, it will not return pdf content.

This solution will only work for you if you have not enabled critical update “PageReference getContent() and getContentAsPDF() Methods Treated as Callouts” in your Salesforce organization.

If we are talking about achieving same in Trigger then it would be problem. Trigger does not support getContent() method of PageReference class. If you are thinking to use getContent() in future call then again we are not lucky, because @future methods does not support it. Also Apex job doesn’t support this method.

Now, I hope you understood that in which situation we are 🙂

So, In this article, I am going to explain how to resolve this issue. Not exactly resolve but workaround for above problem.

Example and tutorial on creating the trigger for attachment in salesforce which will not allow to upload the file in opportunity if file contains some predefined text

Lots of time i have seen the question on salesforce developer forum regarding the trigger on Attachment. As this is not a straightway, still we can have trigger on Attachment.

We cannot create trigger on Attachment from the standard salesforce UI however we will have to use the eclipse.

Before insert Trigger on Attachment using eclipse in Salesforce

While creating the trigger, we have to keep some important points. We should be very careful while writing the trigger on Attachment, as it will be used by any standard or custom Object and therefore the criteria to run trigger for “particular object only” must be handled carefully. Continue reading “Creating Trigger on Attachment in Salesforce”

This article explain the Automatic submission of Approval process using Apex and trigger. It include Automatic submission, approval as well as rejection of record completely using Apex and trigger.

Although this is very common approach and lots of articles are around on this topic, still I want to delineate the topic in other way. This topic covers complete scenarios for the approval process based on the Apex class.

Agenda of this article:

Automatically submit the record for approval on the basis of field value.

Automatically select the next Approver.

Approve / Reject the record on the basis of field.

Assumptions:

Opportunity Object is used.

Approval Process is already set on the Opportunity.

Field “Next_Approver” will decide that who is going to approve the record.

There are three steps in the approval process.

There is no test class written and no check for mandatory fields needed for the trigger, as I have considered positive scenarios only.

This is the last tutorial in series and we will see that how to create a Trigger and Test Cases in salesforce.

A trigger is an Apex script that executes before or after specific data manipulation language (DML) events occur, such as before object records are inserted into the database, or after records have been deleted.

Triggers are stored as metadata in Salesforce.com. A list of all triggers in your organization is located at Setup | Develop | Apex Triggers. In addition to this list, triggers are associated and stored with specific objects.