You need to sign in to do that
Don't have an account?

How to perform URL check at apex code level?
My page is a form for user to input data. There are 4 fields for them to input URL, but not necessary. Therefore, validation rule at field level is not suitable for my case. And I want to search for a method to validate URL at apex code level.
Here is my code, and my 4 URL field are called Website__c, Facebook__c, SoundCloud__c and YouTube__c in custom object "artist".
Thank you.
Here is my code, and my 4 URL field are called Website__c, Facebook__c, SoundCloud__c and YouTube__c in custom object "artist".
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; if(artist.Id == null){ insert artist; } List<Attachment> attachmentList = new List<Attachment>(); if(objAttachment.Body != null){ objAttachment.ParentId = artist.id; attachmentList.add(objAttachment); checkAttachment = true; } if(objAttachment2.Body != null){ objAttachment2.ParentId = artist.id; attachmentList.add(objAttachment2); checkAttachment = true; } if(objAttachment3.Body != null){ objAttachment3.ParentId = artist.id; attachmentList.add(objAttachment3); checkAttachment = true; } List<Attachment> attachmentListother = new List<Attachment>(); if(objAttachmentt.Body != null){ objAttachmentt.ParentId = artist.id; attachmentList.add(objAttachmentt); } if(objAttachments.Body != null){ objAttachments.ParentId = artist.id; attachmentList.add(objAttachments); } Insert attachmentListother; if(attachmentList.size() > 0 && checkAttachment){ insert attachmentList; // 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; } } }
Thank you.
Tweak it as per your requirement.
Do mark it as a best answer if it helped solve your problem.
All Answers
This will return true/false for a valid/invalid URL, and it's working for me..
System.NullPointerException: Script-thrown exception
Error is in expression '{!save}' in component <apex:commandButton> in page form: Class.System.Pattern.matcher: line 30, column 1
Class.extattachfile.save: line 38, column 1
Here is my code:
www.google.com will automatically get typecasted with the applicable protocol.
This should fail for abc / 123 inputted in the URL field, but will pass for www.google.com.
Thank you very much, coz I am vreally new for apex.
Tweak it as per your requirement.
Do mark it as a best answer if it helped solve your problem.
One more thing to ask. How to solve the view state size problem. When I test the page with small-sized attachment, it passes. But when I test the page with 5 5-mb attachment, the actual view state size is obviously too large (22,823KB) which is over the maximum view state size limit (135KB). Is there any solution to make the page view state size itself lower, to make there are more view state for attachment processing.
Just do :
objAttachment.Body = null;
objAttachment2.Body = null
objAttachment3.Body = null
objAttachmentt.Body = null
objAttachments.Body = null
and also :
attachmentListother.clear();
attachmentList.clear();
It seems working fine after you suggestion. Is there any improvement for the above code? Thank you.
Also, you can refer various design patterns to learn more on Apex