• Yashasvi
  • NEWBIE
  • 20 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 7
    Replies
Hi,

I have an Apex Trigger on Qualification__c object which will trigger after update.
Fields in Qualification__c object are as below:
eSigner_First_Name__c (Text), eSigner_Last_Name__c (Text), eSigner_Email__c (Email), Company_Submitting__c (lookup to Account), Qualification_Contact__c (lookup to Contact).

Please see the Trigger below. I request you to help me in writting a test class for this.

trigger CreateNewContact on Qualification__c (after update) {

        Set<String> setID = new Set<String>();
        Set<String> setSignerName = new Set<String>();
        for (Qualification__c sub : Trigger.New) {
            if((sub.eSigner_Last_Name__c != null)&&(sub.eSigner_Email__c != null) ) {
                setID.add(sub.id);
                setSignerName.add(sub.eSigner_First_Name__c + ' ' + sub.eSigner_Last_Name__c);
               
            }  
        }
       
        List<Qualification__c> lstQual = [SELECT id,eSigner_First_Name__c,eSigner_Last_Name__c,eSigner_Email__c,Company_Submitting__r.Id,Company_Submitting__r.Name FROM Qualification__c WHERE id in :setID];
        List<Contact> lstContact = [ SELECT Id, FirstName, LastName FROM Contact WHERE Name in :setSignerName ];

        Map<String,Contact> mapNameWiseContact = new Map<String,Contact>();
        for(Contact cont : lstContact) {
            mapNameWiseContact.put(cont.FirstName + ' ' + cont.LastName,cont);
        }
       
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();//Used to send email
        List<Contact> cList = new List<Contact>();
        for (Qualification__c sub : lstQual) {
            if((sub.eSigner_Last_Name__c != null)&&(sub.eSigner_Email__c != null) ) {
                 if(mapNameWiseContact.containsKey(sub.eSigner_First_Name__c + ' ' + sub.eSigner_Last_Name__c) == false) {
                        Contact c = new Contact();
                        c.FirstName = sub.eSigner_First_Name__c;
                        c.LastName = sub.eSigner_Last_Name__c;
                        c.Email = sub.eSigner_Email__c;
                        c.Title = 'eSigner';
                        c.AccountId = sub.Company_Submitting__r.Id;
                        cList.add(c);
                 } else { //The test code I've written is not entering the else loop.
                        
                       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                       List<String> sendTo = new List<String>();
                       sendTo.add('xyz@gmail.com' (mailto:'xyz@gmail.com'));
                       mail.setToAddresses(sendTo);
                       mail.setReplyTo('yashasvi@gmail.com' (mailto:'yashasvi@gmail.com'));
                       mail.setSenderDisplayName('Yashasvi');
                       
                       mail.setSubject('Subs Qualification ALERT! eSigner Contact NOT CREATED');
                       String body = 'Hi XYZ, ' + '<br/>' + '<br/>';
                       body += 'eSigner Information below as in Qualification Statement, already exists in Contacts. <br/>';
                       body += 'eSigner Name: ' + sub.eSigner_First_Name__c + ' ' + sub.eSigner_Last_Name__c + ',' +'<br/>';
                       body += 'eSigner Email: '+ sub.eSigner_Email__c + '<br/> <br/>';
                       body += 'So, new Contact is not created';
                       mail.setHtmlBody(body);
                       mails.add(mail);

                 }
             }
       }
       Messaging.sendEmail(mails);
      
       if(cList.size() > 0 ) {
            insert cList;
       }
}

Test Code is as shown:
@isTest
public class TestCreateNewContact {
    static testMethod void CreateNewContactTest() {

            Account a = new Account();
            a.Name = 'Test Account';
        insert a;

        List<Contact> contacts = new List<Contact>();
        for(Integer k=0;k<200;k++){
            Contact c = new Contact();
            c.LastName = 'Last Name'+k;
            c.AccountId = a.Id;
            contacts.add(c);
        }
        insert contacts;
       
        List<Contact> cList = [SELECT Id, Name FROM Contact WHERE AccountId = :a.Id];
        System.debug('Number of Contacts: '+ cList.size());
        List<Qualification__c> subQuals = new List<Qualification__c>();
       
        for(Integer p=0;p<200;p++){
            Qualification__c sub = new Qualification__c();
            sub.Company_Submitting__c = a.Id;
            sub.Qualification_Contact__c = cList[p].Id;
            subQuals.add(sub);
        }
        insert subQuals;
        List<Qualification__c> subList = [SELECT Id, Name, Company_Submitting__c, eSigner_Email__c, eSigner_First_Name__c, eSigner_Last_Name__c FROM Qualification__c];
        System.debug('Number of Sub Quals; '+ subList.size());
       
        List<Qualification__c> quals = new List<Qualification__c>();
        Integer i=0;
        for(Qualification__c s : subList)  {
            s.eSigner_First_Name__c = 'Test'+i;
         s.eSigner_Last_Name__c = 'Last';
         s.eSigner_Email__c = 'esigner@gmail.com';
            quals.add(s);
         i++;
     }
        update quals; // This code is showing Error here.
      
        List<Contact> totalCon = [SELECT id FROM Contact];
        System.debug('Total Contacts: '+totalCon.size());
    }
}

I have been trying for a day, but can't figure out. Any help would be really appreciated.

Thanks in advance.
Hi,

I am trying to create a new Contact upon updating a record in a custom object (Qualification__c).

Fields in Qualification__c object are: (1) Company__c (Lookup to Accounts)  (2) Signer_Name__c (Text)  (3) Email_Signer__c (Email)
Upon updating Qualification__c, all the three fields must be used to create a new Contact.
Signer_Name__c must be Contact's Last Name
Email_Signer__c must be Contact's Email field
Company__c must be Contact's Account.

The trigger I've written creates a new contact with Name and Email populated, but Account field on Contact remains empty. I don't know where my code is wrong. Please see my cide below and help me with this.

trigger CreateNewContact on Qualification__c (after update) {
     
List<Contact> cList = new List<Contact>();
        for (Qualification__c sub : Trigger.New){
            if((sub.Signer_Name__c != null)&&(sub.Email_Signer__c != null) ){
                 List<Contact> contactExists = [SELECT Id, LastName FROM Contact WHERE LastName = :sub.Signer_Name__c];
                 if(contactExists == null) {
                        Contact c = new Contact();
                        c.LastName = sub.Signer_Name__c;
                        c.Email = sub.Email_Signer__c;
                        c.AccountId = sub.Company_Submitting__r.Id;
                        cList.add(c);
                  }
             }
             insert cList;
       }
}
 
Hi,
I'm trying to update a Text Field (Address__c) in a Custom Object (Qualification__c) with BillingAddress in its lookup Account Field (Company__c). For this I have created a formula field (Billing_Address__c) in Account object which pulls the data from BillingAddress.

Then, I have created a workflow rule on Qualification__c object.
Rule Criteria = " Qualification: Address EQUALS null ", and
Evaluation Criteria = "Evaluate the rule when a record is created, and any time it’s edited to subsequently meet criteria".
A Field Update Action associated with the Rule is as below:
Field to Update = Qualification: Address
Field Data type = Text
Formula Value = " Company__r.Billing_Address__c ".

For testing, I have created a Qualification record with Address__c leaving empty, but the field is not being updated with Billing_Address__c as stated. I request you to help me with this.
Hi,

I am trying to create a new Contact upon updating a record in a custom object (Qualification__c).

Fields in Qualification__c object are: (1) Company__c (Lookup to Accounts)  (2) Signer_Name__c (Text)  (3) Email_Signer__c (Email)
Upon updating Qualification__c, all the three fields must be used to create a new Contact.
Signer_Name__c must be Contact's Last Name
Email_Signer__c must be Contact's Email field
Company__c must be Contact's Account.

The trigger I've written creates a new contact with Name and Email populated, but Account field on Contact remains empty. I don't know where my code is wrong. Please see my cide below and help me with this.

trigger CreateNewContact on Qualification__c (after update) {
     
List<Contact> cList = new List<Contact>();
        for (Qualification__c sub : Trigger.New){
            if((sub.Signer_Name__c != null)&&(sub.Email_Signer__c != null) ){
                 List<Contact> contactExists = [SELECT Id, LastName FROM Contact WHERE LastName = :sub.Signer_Name__c];
                 if(contactExists == null) {
                        Contact c = new Contact();
                        c.LastName = sub.Signer_Name__c;
                        c.Email = sub.Email_Signer__c;
                        c.AccountId = sub.Company_Submitting__r.Id;
                        cList.add(c);
                  }
             }
             insert cList;
       }
}
 
Hi,
I'm trying to update a Text Field (Address__c) in a Custom Object (Qualification__c) with BillingAddress in its lookup Account Field (Company__c). For this I have created a formula field (Billing_Address__c) in Account object which pulls the data from BillingAddress.

Then, I have created a workflow rule on Qualification__c object.
Rule Criteria = " Qualification: Address EQUALS null ", and
Evaluation Criteria = "Evaluate the rule when a record is created, and any time it’s edited to subsequently meet criteria".
A Field Update Action associated with the Rule is as below:
Field to Update = Qualification: Address
Field Data type = Text
Formula Value = " Company__r.Billing_Address__c ".

For testing, I have created a Qualification record with Address__c leaving empty, but the field is not being updated with Billing_Address__c as stated. I request you to help me with this.