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
LudivineLudivine 

Help with EndWith or Substring

Dear all,

I am Struggling to find how to search the end of an email in my Apex Class.
I wrote this but it doesn't work somebody helps me?

//error message if non amcor email in Contact list
            Integer i = con.email.length();
            if (con.email.substring(con.email,i - 10,10) != '@amcor.com'){ 
                isDisabled = true;
                apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.ERROR, 'You cannot send an email to this person. Email is external from Amcor domain');
                apexpages.addmessage(msg);
                System.debug('@@DEBUG non amcor email: '+con);
                return null;
            }

The error message I got is as follow :

Error: Event_Ext Compile Error: Method does not exist or incorrect signature: [String].substring(String, Integer, Integer)

Many thanks
Best Answer chosen by Ludivine
James LoghryJames Loghry
Iinstead of using substring and as the title of your post suggests, use the "endsWith" String function.

if (!String.isEmpty(con.email) && con.email.endsWith('@amcor.com')){
    isDisabled = true;
    Apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.ERROR, 'You cannot send an email to this person. Email is external from Amcor domain');
    Apexpages.addmessage(msg);
    System.debug('@@DEBUG non amcor email: '+con);
    return null;
}

More String methods and documentation here: http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_string.htm

All Answers

Mark TroupeMark Troupe
You dont need to pass con.email into the substring function as you are calling this function on the email string already.

Try this:  

if (con.email.substring(i - 10,10) != '@amcor.com'){
James LoghryJames Loghry
Iinstead of using substring and as the title of your post suggests, use the "endsWith" String function.

if (!String.isEmpty(con.email) && con.email.endsWith('@amcor.com')){
    isDisabled = true;
    Apexpages.Message msg = new Apexpages.Message(ApexPages.Severity.ERROR, 'You cannot send an email to this person. Email is external from Amcor domain');
    Apexpages.addmessage(msg);
    System.debug('@@DEBUG non amcor email: '+con);
    return null;
}

More String methods and documentation here: http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_string.htm
This was selected as the best answer
LudivineLudivine
Thanks James,

How do you write the EndsWith function if it is different from Amcor.com?
Mark TroupeMark Troupe
yep endsWith is better, for not do this:

if (!String.isEmpty(con.email) && !con.email.endsWith('@amcor.com')){


LudivineLudivine
Hi Marc and James,

Thank you so much for your help and really good support, my issue is solved now :)
What an excellent team!!!

Have a nice day.