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
@GM@GM 

Error occurred trying to load the template for sending preview email: DML currently not allowed. 

Error occurred trying to load the template for sending preview email: DML currently not allowed. 


I'm getting above error even after adding allowDML="true" to component.

Could any one help on this?


VF template :


<messaging:emailTemplate subject="Alert: WON Opportunity -" recipientType="User" relatedToType="Opportunity">

      <messaging:htmlEmailBody >

      <c:opportunityAttachedFiles componentValue="{!relatedTo.Id}"/>

      </messaging:htmlEmailBody>

</messaging:emailTemplate>


VF Component :


<apex:component controller="OpportunityAttachments" allowDML="true" access="global">

    <apex:attribute name="componentValue" description="Attribute on the component."
                  type="Id" required="required" assignTo="{!opptyId}"/>
    <apex:dataTable value="{!OpptyFilesLink}" var="att">
        <apex:column >
            <apex:facet name="header">File Name</apex:facet>
            {!att.Name}
        </apex:column>
        <apex:column >
            <apex:facet name="header">File Link</apex:facet>
            {!att.DistributionPublicUrl}
        </apex:column>
    </apex:dataTable>
</apex:component>


Controller :


public class OpportunityAttachments {
    public Id opptyId;
    
    public void setopptyId (Id oppId) {
        opptyId = oppId;
    }
    
    public String getopptyId() {
        return opptyId;
    }
    
    public List<ContentDistribution> getOpptyFilesLink(){
        List<ContentDistribution> lstTobeInserted = new List<ContentDistribution>();
        List<ContentDistribution> lstToReturn = new List<ContentDistribution>();
        Set<Id> setContentDocumentId = new Set<Id>();
        List<ContentDocumentLink> lstContentDocumentLink = new List<ContentDocumentLink>();
        if(String.ValueOf(opptyId) != null && String.ValueOf(opptyId) != ''){
            lstContentDocumentLink = [SELECT ContentDocumentId,Id,IsDeleted,LinkedEntityId,ShareType,SystemModstamp,Visibility FROM ContentDocumentLink where LinkedEntityId =: opptyId];
        }
        if(!lstContentDocumentLink.isEmpty()){
            for(ContentDocumentLink obj : lstContentDocumentLink){
                setContentDocumentId.add(obj.ContentDocumentId);
            }
            
            for(ContentDocument obj :[SELECT ArchivedById,ArchivedDate,ContentModifiedDate,ContentSize,CreatedById,CreatedDate,Description,FileExtension,FileType,Id,IsArchived,IsDeleted,LastModifiedById,LastModifiedDate,LastReferencedDate,LastViewedDate,LatestPublishedVersionId,OwnerId,ParentId,PublishStatus,SharingOption,SystemModstamp,Title FROM ContentDocument where Id=: setContentDocumentId]){
                ContentDistribution cd = new ContentDistribution();
                //cd.ContentDocumentId = obj.ContentDocumentId;
                cd.name = obj.Title;
                cd.ContentVersionId = obj.LatestPublishedVersionId;
                cd.PreferencesAllowOriginalDownload = false;
                cd.PreferencesAllowPDFDownload = false;
                cd.PreferencesAllowViewInBrowser = true;
                lstTobeInserted.add(cd);
            }
            Database.upsert(lstTobeInserted,ContentDistribution.Name.getDescribe().getSObjectField(),false);
        }
        set<Id> getLatest = new set<Id>();
        for(ContentDistribution obj : [Select ContentDocumentId,Name,ContentVersionId,DistributionPublicUrl from ContentDistribution order by CreatedDate DESC]){     
            if(!getLatest.contains(obj.ContentDocumentId)){
                getLatest.add(obj.ContentDocumentId);
                lstToReturn.add(obj);
            }
        }
        return lstToReturn;
    }
}


Thanks,

GM
Best Answer chosen by @GM
@GM@GM
Hi,

The DML is not allowed even after adding allowDML="true" to component hence i have removed the DML operations in the component.

Regards,
GM

All Answers

NagendraNagendra (Salesforce Developers) 
Hi,

Please check with following thread from stack exchange community which says DML just might not be allowed in this context. Please let us know if this helps.

Regards,
Nagendra
@GM@GM
Hi,

The DML is not allowed even after adding allowDML="true" to component hence i have removed the DML operations in the component.

Regards,
GM
This was selected as the best answer