-2

Trigger is not go into the If Condition and Not compare Value. How to comapare oldAccount Name is notEqual to ModifiedAccount Name? And How to write query for the Id to compare the Name in the code.If anyother mistake by me please told me

trigger SendEmailtoContact on Account (before Update,after Update) {

        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        List<String> Name = new List<String>();
       // Account ac = new Account();
    List<String> sendTo = new List<String>();
    List<Contact> con = new List<Contact>();

     //  ac = [SELECT Id,Name FROM Account];
    for(Account acc : trigger.old){
       // System.debug('accName::' + acc.Name);
    //   String oldName = String.valueof('acc.Id');
         //String oldName = trigger.oldmap.get('acc.Id').Name;
        **if(acc.Name != acc.Name){
            Name.add(acc.Name);
            System.debug('Name::' + Name);**
        }
    }

    if(Name.size()>0)
    {
        con = [SELECT lastname,E_mail__c FROM Contact WHERE AccountId IN:Name];
        System.debug('----contact----'+con);
        if(con.size()>0){
            for(Contact c : con){
                sendTo.add(c.E_mail__c);
                system.debug('emali::'+sendTo);
            }
        }
    }

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setSenderDisplayName('Email Alert');

    mail.setSubject('Account Name change');
    String body = 'Dear Employee Account Name is change.';
    mail.setToAddresses(sendTo);
    mail.setHtmlBody(body);
    mails.add(mail);
    try{
        Messaging.SendEmail(mails);
    }
    catch(Exception e){
            System.debug('-----Exception------' +e);        
    }
}

Error:Error:Apex trigger SendEmailtoContact caused an unexpected exception, contact your administrator: SendEmailtoContact: execution of BeforeUpdate caused by: System.StringException: Invalid id: acc.Id: External entry point

  • Why not just use a workflow rule and email alert? – Eric Mar 5 at 6:50
1

There are quite a few changes that you need to make in order for this to work

  • You need to identify in which context your mail should be triggered, as per your code it currently fires on before and after update. I would prefer to have it in after update context. So for that, you will have surround your code with a check for the after update.
  • You are currently not comparing the oldValue and newValue in your code, instead you are just checking the same values which will never be true.
  • You will have to get the oldValue from Trigger.oldMap context variable. And I see that you tried in the commented code which won't give you the old value. You can get the old value by using String oldName = trigger.oldmap.get(acc.Id).Name and not String oldName = trigger.oldmap.get('acc.Id').Name. Note that you will have to pass the Account's Id and you don't have to enclose your variable in single quotes.
  • And the reason you are getting the above mentioned error is because you are trying to filter out the Contacts based on AccountId and you are trying to compare AccountId field with the List of Names, which again is not correct.

Below code should work for you

trigger SendEmailtoContact on Account (before update, after update) {
    if(Trigger.isAfter && Trigger.isUpdate){
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        Set<Id> setAccountIds = new Set<Id>();
        List<String> sendTo = new List<String>();
        List<Contact> con = new List<Contact>();
        for(Account acc : trigger.New){
            if(acc.Name != trigger.oldmap.get(acc.Id).Name){
                setAccountIds.add(acc.Id);
                System.debug('setAccountIds::' + setAccountIds);
            }
        }

        if(!setAccountIds.isEmpty()) {
            for(Contact c : [SELECT lastname,E_mail__c FROM Contact WHERE AccountId IN:setAccountIds]){
                if(string.IsNotBlank(c.E_mail__c)){
                    sendTo.add(c.E_mail__c);
                }
            }
        }

        if(!sendTo.isEmpty()){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('Email Alert');

            mail.setSubject('Account Name change');
            String body = 'Dear Employee Account Name is change.';
            mail.setToAddresses(sendTo);
            mail.setHtmlBody(body);
            mails.add(mail);
            try{
                Messaging.SendEmail(mails);
            }
            catch(Exception e){
                System.debug('-----Exception------' +e);        
            }
        }
    }
}
0

Putting something inside ' characters makes it a String. You want to use the variable itself:

String oldName = trigger.oldmap.get(acc.Id).Name;
  • Thank you for help – D.Shah Mar 5 at 6:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.