• ravi teja gutha
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 5
    Replies

global class SendNPSbutton {
    @AuraEnabled
    public static void sendNPSEmail(Id cId)
    {
        EmailTemplate emiailTemp = new EmailTemplate();
        String htmlBody; 
        String plainBody;
        String subject;
        List<String> sendTo = new List<String>();
        contact cc = [select id from contact limit 1];
        
        Closing__c cpps = [SELECT Id,RecordTypeId,RecordType.Name,Seller_Email__c,Buyer_Email__c,Owner.FirstName,
                           Buyer_Account__c,Seller_Account__c,Seller_Account__r.Name,Appointment__r.Name
                           FROM Closing__c where Id=: cId LIMIT 1];
        
        if(cpps.Id != null)
        {
            if(cpps.RecordType.Name =='Listing Team'){
                sendTo.add(cpps.Seller_Email__c);
                emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_npsSeller'];
                subject = emiailTemp.Subject;
                htmlBody= emiailTemp.HtmlValue;
                htmlBody = htmlBody.replace('{!Closing__c.Seller_Account__c}', cpps.Seller_Account__r.name);
                htmlBody = htmlBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
                plainBody = emiailTemp.Body;
                plainBody = plainBody.replace('{!Closing__c.Seller_Account__c}', cpps.Seller_Account__c);
                plainBody = plainBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
            }else{
                sendTo.add(cpps.Buyer_Email__c);
                emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_nps'];
                subject = emiailTemp.Subject;
                htmlBody= emiailTemp.HtmlValue;
                htmlBody = htmlBody.replace('{!Closing__c.Buyer_Account__c}', cpps.Appointment__r.Name);
                htmlBody = htmlBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
                plainBody = emiailTemp.Body;
                plainBody = plainBody.replace('{!Closing__c.Seller_Account__c}', cpps.Seller_Account__c);
                plainBody = plainBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
            }
            // process the merge fields
            // String subject = emailTemplate.Subject;
            //  subject = subject.replace('{!Contact.FirstName}', c.FirstName);

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setTemplateId(emiailTemp.Id);
            List<String> ccTo = new List<String>();
            ccTo.add('xyz@gomail.com');
            mail.setCcAddresses(ccTo);
            mail.setReplyTo('xyz@gomail.com');
            mail.setSenderDisplayName('XYZ');
            mail.setTargetObjectId(cc.id);
            mail.setTreatTargetObjectAsRecipient(false);
            mail.setWhatId(cpps.Id);
            mail.setToAddresses(sendTo);     
            mail.setBccSender(false);
            mail.setUseSignature(false);
            mail.setHtmlBody(htmlBody);
            mail.setSubject(subject);
            mail.setPlainTextBody(plainBody);
            mail.setSaveAsActivity(false);  
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
           
        }
    }
    
}

 

TEST Class:

@istest(SeeAllData=true)

public class SendNPSButton_test{
    
    static testmethod void testvalidate(){
        List<String> sendTo = new List<String>();
        Account acc = new Account();
        acc.Name = 'Name test';
        insert acc;
        Closing__c cl = new Closing__c();
        //cl.RecordTypeId=Schema.SObjectType.Closing__c.getRecordTypeInfosByName().get('Buying Team').getRecordTypeId();
        cl.Buyer_Email__c = 'ravi.7293@gmail.com';
        cl.Seller_Email__c ='ravi.7293@gmail.com';
        cl.Seller_Account__c = acc.Id;
        insert cl;
       EmailTemplate emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_npsSeller'];
        /*Closing__c cpps = [SELECT Id,RecordTypeId,RecordType.Name,Seller_Email__c,Buyer_Email__c,Owner.FirstName,
                           Buyer_Account__c,Seller_Account__c,Seller_Account__r.Name,Appointment__r.Name
                           FROM Closing__c where Id=: cl.Id LIMIT 1];*/
        system.debug('cl>>: '+ cl);
        Test.startTest();
       // Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         //   mail.setTemplateId(emiailTemp.Id);
        //mail.setToAddresses(sendTo);
        //Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        system.debug('cl11>>: '+ cl.Seller_Email__c);
        system.debug('cID>>: '+ cl.ID);
        SendNPSbutton.sendNPSEmail(cl.Id);
        //Integer emailInvocations = Limits.getEmailInvocations();
        Test.stopTest();
        //system.assertEquals(1, emailInvocations, 'An email should be sent');
        
    }
}

 

 

global class SendNPSbutton {
    @AuraEnabled
    public static void sendNPSEmail(Id cId)
    {
        EmailTemplate emiailTemp = new EmailTemplate();
        String htmlBody; 
        String plainBody;
        String subject;
        List<String> sendTo = new List<String>();
        contact cc = [select id from contact limit 1];
        
        Closing__c cpps = [SELECT Id,RecordTypeId,RecordType.Name,Seller_Email__c,Buyer_Email__c,Owner.FirstName,
                           Buyer_Account__c,Seller_Account__c,Seller_Account__r.Name,Appointment__r.Name
                           FROM Closing__c where Id=: cId LIMIT 1];
        
        if(cpps.Id != null)
        {
            if(cpps.RecordType.Name =='Listing Team'){
                sendTo.add(cpps.Seller_Email__c);
                emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_npsSeller'];
                subject = emiailTemp.Subject;
                htmlBody= emiailTemp.HtmlValue;
                htmlBody = htmlBody.replace('{!Closing__c.Seller_Account__c}', cpps.Seller_Account__r.name);
                htmlBody = htmlBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
                plainBody = emiailTemp.Body;
                plainBody = plainBody.replace('{!Closing__c.Seller_Account__c}', cpps.Seller_Account__c);
                plainBody = plainBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
            }else{
                sendTo.add(cpps.Buyer_Email__c);
                emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_nps'];
                subject = emiailTemp.Subject;
                htmlBody= emiailTemp.HtmlValue;
                htmlBody = htmlBody.replace('{!Closing__c.Buyer_Account__c}', cpps.Appointment__r.Name);
                htmlBody = htmlBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
                plainBody = emiailTemp.Body;
                plainBody = plainBody.replace('{!Closing__c.Seller_Account__c}', cpps.Seller_Account__c);
                plainBody = plainBody.replace('{!Closing__c.OwnerFirstName}', cpps.Owner.FirstName);
            }
            // process the merge fields
            // String subject = emailTemplate.Subject;
            //  subject = subject.replace('{!Contact.FirstName}', c.FirstName);

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setTemplateId(emiailTemp.Id);
            List<String> ccTo = new List<String>();
            ccTo.add('xyz@gomail.com');
            mail.setCcAddresses(ccTo);
            mail.setReplyTo('xyz@gomail.com');
            mail.setSenderDisplayName('XYZ');
            mail.setTargetObjectId(cc.id);
            mail.setTreatTargetObjectAsRecipient(false);
            mail.setWhatId(cpps.Id);
            mail.setToAddresses(sendTo);     
            mail.setBccSender(false);
            mail.setUseSignature(false);
            mail.setHtmlBody(htmlBody);
            mail.setSubject(subject);
            mail.setPlainTextBody(plainBody);
            mail.setSaveAsActivity(false);  
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
           
        }
    }
    
}

 

TEST Class:

@istest(SeeAllData=true)

public class SendNPSButton_test{
    
    static testmethod void testvalidate(){
        List<String> sendTo = new List<String>();
        Account acc = new Account();
        acc.Name = 'Name test';
        insert acc;
        Closing__c cl = new Closing__c();
        //cl.RecordTypeId=Schema.SObjectType.Closing__c.getRecordTypeInfosByName().get('Buying Team').getRecordTypeId();
        cl.Buyer_Email__c = 'ravi.7293@gmail.com';
        cl.Seller_Email__c ='ravi.7293@gmail.com';
        cl.Seller_Account__c = acc.Id;
        insert cl;
       EmailTemplate emiailTemp = [SELECT Id, Name,Subject, DeveloperName,HtmlValue, Body FROM EmailTemplate where DeveloperName = 'NPS_Survey_npsSeller'];
        /*Closing__c cpps = [SELECT Id,RecordTypeId,RecordType.Name,Seller_Email__c,Buyer_Email__c,Owner.FirstName,
                           Buyer_Account__c,Seller_Account__c,Seller_Account__r.Name,Appointment__r.Name
                           FROM Closing__c where Id=: cl.Id LIMIT 1];*/
        system.debug('cl>>: '+ cl);
        Test.startTest();
       // Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         //   mail.setTemplateId(emiailTemp.Id);
        //mail.setToAddresses(sendTo);
        //Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        system.debug('cl11>>: '+ cl.Seller_Email__c);
        system.debug('cID>>: '+ cl.ID);
        SendNPSbutton.sendNPSEmail(cl.Id);
        //Integer emailInvocations = Limits.getEmailInvocations();
        Test.stopTest();
        //system.assertEquals(1, emailInvocations, 'An email should be sent');
        
    }
}