At this stage, I only achieve this ("a002800000i5q8UAAQ_Alex_2016-08-03_Technical_field2.png") by this code:
But I want to achieve this ("a002800000i5q8UAAQ_Alex_2016-08-03_Technical.png").objAttachmentt.Name = artist.Id+'_'+artist.Artist_Group_Name_English__c+'_'+String.valueOf(system.today())+'_Technical'+'_'+objAttachmentt.Name;
The whole code for reference:
Thank you.public class extattachfile {
Public attachment objAttachment{get;set;}
Public attachment objAttachment2{get; set;}
Public attachment objAttachment3{get; set;}
Public attachment objAttachmentt{get; set;}
Public attachment objAttachments{get; set;}
Public Artist__c artist{get; set;}
Public extattachfile(apexpages.standardcontroller stdCon) {
objAttachment = new Attachment();
objAttachment2 = new Attachment();
objAttachment3 = new Attachment();
objAttachmentt = new Attachment();
objAttachments = new Attachment();
artist= new Artist__c ();
}
public PageReference save() {
Boolean checkAttachment = false;
Boolean isValidUrl = true;
if(artist.Id == null){
if(artist.Website__c != null){
Pattern emailPattern = Pattern.compile('^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$');
Boolean isMatch = emailPattern.matcher(artist.Website__c).matches();
if(!isMatch){
objAttachment.Body = null;
objAttachment2.Body = null;
objAttachment3.Body = null;
objAttachmentt.Body = null;
objAttachments.Body = null;
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please provide a valid Website URL.'));
isValidUrl = false;
}
}
if(artist.Facebook__c != null){
Pattern emailPattern = Pattern.compile('^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$');
Boolean isMatch = emailPattern.matcher(artist.Facebook__c).matches();
if(!isMatch){
objAttachment.Body = null;
objAttachment2.Body = null;
objAttachment3.Body = null;
objAttachmentt.Body = null;
objAttachments.Body = null;
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please provide a valid Facebook URL.'));
isValidUrl = false;
}
}
if(artist.SoundCloud__c != null){
Pattern emailPattern = Pattern.compile('^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$');
Boolean isMatch = emailPattern.matcher(artist.SoundCloud__c).matches();
if(!isMatch){
objAttachment.Body = null;
objAttachment2.Body = null;
objAttachment3.Body = null;
objAttachmentt.Body = null;
objAttachments.Body = null;
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please provide a valid Soundcloud URL.'));
isValidUrl = false;
}
}
if(artist.YouTube__c != null){
Pattern emailPattern = Pattern.compile('^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$');
Boolean isMatch = emailPattern.matcher(artist.YouTube__c).matches();
if(!isMatch){
objAttachment.Body = null;
objAttachment2.Body = null;
objAttachment3.Body = null;
objAttachmentt.Body = null;
objAttachments.Body = null;
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please provide a valid Youtube URL.'));
isValidUrl = false;
}
}
if(isValidUrl){
insert artist;
}else{
return null;
}
}
List<Attachment> attachmentList = new List<Attachment>();
if(objAttachment.Body != null){
objAttachment.ParentId = artist.id;
objAttachment.Name = artist.Id+'_'+artist.Artist_Group_Name_English__c+'_'+String.valueOf(system.today())+'_Photo1'+'_'+objAttachment.Name;
attachmentList.add(objAttachment);
checkAttachment = true;
}
if(objAttachment2.Body != null){
objAttachment2.ParentId = artist.id;
objAttachment2.Name = artist.Id+'_'+artist.Artist_Group_Name_English__c+'_'+String.valueOf(system.today())+'_Photo2'+'_'+objAttachment2.Name;
attachmentList.add(objAttachment2);
checkAttachment = true;
}
if(objAttachment3.Body != null){
objAttachment3.ParentId = artist.id;
objAttachment3.Name = artist.Id+'_'+artist.Artist_Group_Name_English__c+'_'+String.valueOf(system.today())+'_Photo3'+'_'+objAttachment3.Name;
attachmentList.add(objAttachment3);
checkAttachment = true;
}
List<Attachment> attachmentListother = new List<Attachment>();
if(objAttachmentt.Body != null){
objAttachmentt.ParentId = artist.id;
objAttachmentt.Name = artist.Id+'_'+artist.Artist_Group_Name_English__c+'_'+String.valueOf(system.today())+'_Technical'+'_'+objAttachmentt.Name;
attachmentList.add(objAttachmentt);
}
if(objAttachments.Body != null){
objAttachments.ParentId = artist.id;
objAttachments.Name = artist.Id+'_'+artist.Artist_Group_Name_English__c+'_'+String.valueOf(system.today())+'_Stage'+'_'+objAttachments.Name;
attachmentList.add(objAttachments);
}
Insert attachmentListother;
if(attachmentList.size() > 0 && checkAttachment){
insert attachmentList;
attachmentList = null;
// if successfully inserted new contact, then displays the thank you page.
return Page.ack;
}else{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please attach at least one photo attachment.'));
return null;
}
}
}
I see. So objAttachmentt.Name actually has the value of "field2.png" in the example you gave. You just need to strip out the first part of the full filename, right? If so, you can do this: List<String> splitStrList = objAttachmentt.Name.Split('\\.');
String fileExtension = splitStrList[1];
objAttachmentt.Name = artist.Id+'_'+artist.Artist_Group_Name_English__c+'_'+String.valueOf(system.today())+'_Technical'+'.' + fileExtension;