0

I need to take variable from my void method based on the given argument when calling the method in another class.

template = //here I want to take variable "ExpiringContract"

Below is the code;

public class automatedCommunication{

    public OrgWideEmailAddress orgWideMail = [SELECT Id FROM OrgWideEmailAddress];
    public String country = '';

    public Id emailtemplate;
    public Id invPDFattachmemt{get;set;}
    public Attachment attach{get;set;}
    public EmailTemplate template;
    public List<Invoice__c> linv;

    public String senderName = 'Lamudi';
    public String defaultReplyEmailID = 'wecare@lamudi.co.id';
    public String defaultReplyEmailPH = 'wecare@lamudi.com.ph';
    public String defaultReplyEmailMX = 'wecare@lamudi.com.mx';

    public void setEmailTemplate(String count){

         //Template for ID
        if(count == 'ID'){
        List<EmailTemplate> ExpiringContract = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: 'Contract renewal notice ID' LIMIT 1];
        List<EmailTemplate> DueInvoice = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: 'Invoice due reminders ID' LIMIT 1];
        List<EmailTemplate> BlockingOutstanding = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: 'Invoice overdue ID' LIMIT 1];
        List<EmailTemplate> BlockingAccount = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: 'Account has been blocked ID' LIMIT 1];
        List<EmailTemplate> DeactivatingAccount = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: 'Account has been deactivated ID' LIMIT 1];
        List<EmailTemplate> LostWinBack = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: 'lost win back Id' LIMIT 1];
        }

        //Template for MX
        if(count == 'MX'){
        List<EmailTemplate> ExpiringContract = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: '9. Contract renewal notice' LIMIT 1];
        List<EmailTemplate> DueInvoice = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: '10. Invoice Due Reminder' LIMIT 1];
        List<EmailTemplate> BlockingOutstanding = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: '11. Invoice overdue' LIMIT 1];
        List<EmailTemplate> BlockingAccount = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: '12. Account has been blocked' LIMIT 1];
        List<EmailTemplate> DeactivatingAccount = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: '13. Account has been Deactivated' LIMIT 1];
        List<EmailTemplate> LostWinBack = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: '6. Lost Win Back' LIMIT 1];
        }

        //template for PH
        if(count == 'PH'){
        List<EmailTemplate> ExpiringContract = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: 'New Renewal Notice' LIMIT 1];
        List<EmailTemplate> DueInvoice = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: 'Invoice Due in 2 Weeks' LIMIT 1];
        List<EmailTemplate> BlockingOutstanding = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: 'Blocking Outstanding' LIMIT 1];
        List<EmailTemplate> BlockingAccount = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: 'Blocking Account' LIMIT 1];
        List<EmailTemplate> DeactivatingAccount = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: 'Deactivated Contract' LIMIT 1];
        List<EmailTemplate> LostWinBack = [SELECT Id, Name, Description, Subject FROM EmailTemplate WHERE Name =: 'Lost Win Back' LIMIT 1];
        }

    }


    public void sendRenewalReminder(String selectCountry){

        country = selectCountry;
        this.setEmailTemplate(country);

/**** Set Template ****/

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        Set<Id> ids = new Set<Id>();

/**** Set filters and conditions (recipients and criteria -> To:, When?, who?, why?) ****/
        List<Contact> licontact = //QUERY

/**** Start process ****/

        IF(licontact.size() >0){

            /**** Set parameters ****/
            List<Contact> contacts = new List<Contact>();
            contacts = Database.query(expiredConQuery);

            FOR(Contact cont : contacts){
                ids.add(cont.Account.Id);
            }
            List<Account> accounts = [SELECT LastRenewalNoticeDate__c FROM Account WHERE Id in: ids];

            /**** Create emails ****/

            FOR(Contact cont : contacts){

                template = //here I want to take variable "ExpiringContract"

                emailtemplate = template.Id;

                mail = createEmailMessage(emailtemplate, cont.Id, cont.AccountId, cont.Account.AccountManager__r.Email, cont.Account.AccountManager__r.isActive, cont.Account.Owner.Email, cont.Account.Owner.isActive, cont.Account.AccountCountry__c); 

                mails.add(mail);

                if(Integer.valueof(Limits.getQueries()) >= 40){
                        break;
                }
            }

            system.debug(mails.size());
            system.debug(mails);

        /**** Send emails ****/
            IF(mails.size() >0){
                Messaging.sendEmail(mails, false);

                /***** updates Account date of last sendout to avoid multiple sendouts ****/
                FOR(Account acc : accounts){
                    acc.LastRenewalNoticeDate__c = system.today();
                }

                update accounts;
            }
        }
    }
  • Please check that there is a static method called get(String) in EmailTemplate class – Isuru Buddhika Herath Nov 8 '19 at 6:01
  • Hi Isuru I dont understand what you mean? – Adnan Zahry Nov 8 '19 at 7:58
  • As you are not disclosing your variable declarations, it is hard to understand what each variable holds. Also EmailTemplate.get () would reference a static method on the SObject EmailTemplate. That would not work. And SObject.get (String) returns a String, not an object. – Florian Heer Nov 8 '19 at 11:55
  • use edit and remove lines of code that are irrelevant to this issue. Are you trying to use the Sobject class get method? if so, the arguments are either strings or SObjectFields – cropredy Nov 8 '19 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.