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
Linda 98Linda 98 

Display error pop up from apex class-URGENT

Hi

i have a apex class which sends email.That apex class is called in custom button.All works fine.
Now i had introduced a condition when the apex class has to be executed.It wrks fine.But i want a way to show to user that the button was exeecuted and what happened  like 'success' -when m condition becomes true and 'email not sent' -when the condition becomes false.
please help.

my apex class:

global class SendemailController{

    webservice static void sendEmail(id oppid){
    ifMY IF CONDITION){   //MY IF CONDITION
        System.debug('**********T.ownerid');
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setTargetObjectId(Ownerid) ;
    mail.setWhatId(oppid) ;

/*
MY OTHER LOGIC

*/
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });   
}

my custom buttn which send's email:

{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')}
{!REQUIRESCRIPT('/soap/ajax/29.0/apex.js')}
document.location.reload(true);

var result = sforce.apex.execute('SendemailController','sendEmail,{oppid:'{customobject__c.Id}'});


Please help!!
Maciej SobkowiczMaciej Sobkowicz
I would change sendEmail method to return Boolean value. And then in custom button js definition based on result == true | false you can display some information to the user. The easiest but not very elegant way would be to use JS alert. If this is a VF page you can quite easily create some section with result info and fill it with JS
Linda 98Linda 98
Can i have a snippet if what you meant.Please
Maciej SobkowiczMaciej Sobkowicz
Ok, sure. As mentioned in my previous post I don't know exactly how would you like to present this message to the user so I will just use the simpliest way (JS alert). Here is modified controller:
global class SendEmailController {

    webservice static Boolean sendEmail(id oppid){
        if (MY IF CONDITION){   //MY IF CONDITION
            System.debug('**********T.ownerid');
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setTargetObjectId(Ownerid) ;
            mail.setWhatId(oppid);
            /*
               MY OTHER LOGIC
            */
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            return true;
        }
        return false;
    }

and your custom button definition with JS alert:
{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')}
{!REQUIRESCRIPT('/soap/ajax/29.0/apex.js')}
document.location.reload(true);

var result = sforce.apex.execute('SendemailController','sendEmail,{oppid:'{customobject__c.Id}'});

if (result) {
    alert('success');
} else {
    alert('email not sent');
}