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
dillip nayak 3dillip nayak 3 

How to Get Content Type Using Name Attchment object.Is there any method to get content type

How to Get Content Type if content type is blank in attchment object.Is there any method that help to get content type with using Name Field of attchment object.


Thanks
 
EnreecoEnreeco
Hi Dillip.
if you are using Visualforce you can use something like this:
<apex:inputFile contentType="{!contentTypeVar}" value="{!bodyVar}" fileName="{!nameVar}" />

After the postback you have the 3 variables set and ready to be used.

(https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_inputFile.htm)


--
May The Force.com be with you!
@enreeco
dillip nayak 3dillip nayak 3
HI Enreeco,

Thanks for your message .I need it on trigger

trigger AttchmentContentType on Attachment (After Insert) {
if(trigger.isinsert && trigger.isafter)
{
list<Attachment> attchmentList=new list<Attachment>();
for(Attachment attachment:[select id,name,ContentType from Attachment where id=:trigger.newMap.keySet()])
{
if(string.isBlank(attachment.ContentType) || string.isEmpty(attachment.ContentType)) {
 if(attachment.Name.endsWith('.txt'))
attachment.ContentType='text/plain';
else if(attachment.Name.endsWith('.csv'))
attachment.ContentType='application/vnd.ms-excel';
else if(attachment.Name.endsWith('.doc'))
attachment.ContentType='application/msword';
attchmentList.add(attachment);
} }
if (attchmentList.size()>0) update attchmentList;
} }

i Dont wan to use endswith because some time user rename the file.

i need it dynamically is there any method.please share me the code.if you have

Look forward to hear from you.

Thanks