• michael Dawson 7
  • NEWBIE
  • 10 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
I have the below code but I would like to add @InvocableMethod to it so I can set the start and end date from a flow but need help as I'm new to Apex.

public with sharing class GenerateBusinessDays {
    public static void GenerateBusinessDays(Date startDate, Date endDate)  {
        Id businessHoursId = [SELECT Id FROM BusinessHours WHERE IsDefault = true LIMIT 1].Id;
        if(startDate == null || endDate == null){
            return;
        }
        delete [select id from business_day__c where date__c >= :startDate and date__c <= :endDate];
        // Adding 12 hours to ensure the conversion of UTC to EST won't shift the date to the previous day
        DateTime startDateTime = DateTime.newInstanceGMT( startDate.Year(), startDate.Month(), startDate.Day(), 12, 0, 0);
        DateTime endDateTime = DateTime.newInstanceGMT( endDate.Year(), endDate.Month(), endDate.Day(), 12, 0, 0);
        List<Business_Day__c> businessDays = new List<Business_Day__c>();
        while(startDateTime <= endDateTime) {
            if(BusinessHours.isWithin(businessHoursId, startDateTime)){
                Business_Day__c businessDay = new Business_Day__c();
                businessDay.Date__c = startDateTime.Date();
                businessDay.Name = startDateTime.format('dd mm, yyyy');
                businessDays.add(businessDay);
            }
             startDateTime = startDateTime.addDays(1);
        }
        insert businessDays;
    }
}
I have added a trigger and Class that works to stop a PDF being upload to an object on Salersforce but if someone uploads a new version it will allow them to add a PDF can you help me stop this.

Trigger:

rigger trig_ContentDocumentLink_AfterInsert on ContentDocumentLink (after insert) {
   if(Trigger.isAfter && Trigger.isInsert){
      System.debug('Trigger started after insert.');
      TW_ContentDocumentLink_Handler.onAfterInsert(Trigger.New);  
    }
}

Classes

public class TW_ContentDocumentLink_Handler {
     public static void onAfterInsert(list<ContentDocumentLink> lstCntLinks) {
        String strObjPrefix; 
        Set<Id> setCntDocIds = new set<Id>();
        set<Id> setAgmtIds = new set<Id>();
        map<Id, Report_Deliverable__c> mapAgmt;
        try{
            for(ContentDocumentLink clIterator : lstCntLinks) {
                strObjPrefix = String.valueOf(clIterator.LinkedEntityId).substring(0, 3); // Return first letters of record id like 001 for Account 
                if(strObjPrefix == Report_Deliverable__c.sObjectType.getDescribe().getKeyPrefix()) {
                    setCntDocIds.add(clIterator.ContentDocumentId);// Content Document Id
                    setAgmtIds.add(clIterator.LinkedEntityId);// Agreement Id - Parent record Id
                }
            }
            
            if(setCntDocIds.size() > 0 && setAgmtIds.size() > 0 ) {           
                 mapAgmt = new map<Id, Report_Deliverable__c>([SELECT Id FROM Report_Deliverable__c WHERE Id IN :setAgmtIds]);
                //Fetching parent record details and restricting only if Record type is Account 
                if(mapAgmt.size() > 0){
                    map<Id, ContentDocument> mapContentDocuments = new map<Id, ContentDocument>([SELECT Id, Title, FileExtension FROM ContentDocument WHERE Id IN :setCntDocIds]);
                    list<ContentDocument> lstCntDocsToUpdate = new list<ContentDocument>();        
                    for(ContentDocumentLink cdlIterator : lstCntLinks) {
                        ContentDocument objCntDoc = mapContentDocuments.get(cdlIterator.ContentDocumentId);
                        // Allow all files except : pdf 
                        if(objCntDoc.FileExtension == 'pdf'){
                            cdlIterator.addError('You can not upload pdf files.');//Showing error   
                            // This line will abort file upload option. 
                            // Error message will be displayed only when 'ContentDocumentLink' object has some RecordType created (any other than Master)
                            // else Generic message will be dispayed - 'Can not add 1 file to Account'.   
                        }                        
                    }  
                }
            }
        }
        Catch (Exception ex){
            system.debug('Exception in TW_ContentDocumentLink_Handler class :' + ex.getMessage());
        }
    }
}


*********VF Page***************

<apex:page controller="uploadFile">
<apex:messages />
<apex:form enctype="multipart/form-data" >
<apex:pageBlock title="Contribute Content" >
<apex:commandbutton action="{!upload}" value="Publish" status="status"/>
<apex:pageBlockSection columns="1">
<apex:pageBlockSectionItem >
<apex:outputLabel > Title </apex:outputLabel>
<apex:inputTextarea value="{!title}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
title
<apex:inputFile value="{!file}" />
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

*********Apex Class************
public class uploadFile {
public blob file { get; set; }
public String title{get;set;}
public ContentVersion cv { get; set; }


public PageReference upload() {
ContentVersion cv = new ContentVersion();
cv.versionData = file;
cv.title = title;
cv.pathOnClient ='/test img.jpeg';
cv.FirstPublishLocationId = '01dfr000000000gA'; //Change id
try
{
insert cv;
system.debug('*********************Result'+cv.pathonClient);
}
catch (DMLException e)
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading Document in Library'));
return null;
}
finally
{
cv= new ContentVersion();
}

ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Document uploaded successfully to library'));
return null;
}
}

My question Here is i could only upload jpeg files , and can see preview in library.

What if want to upload different formats of files...like pdf, csv, doc, txt jpeg, png etc....

Please help me wit this.. I would really appreciate for Quick response

Thanks