+ Start a Discussion
TriptisfdcTriptisfdc 

I am new to salesforce Trigger. can someone help me on this?

When an Contact is inserted under an Account, then its SerialDate field should be 1 month ahead than the AssignDate field of its parent Account.
Steven NsubugaSteven Nsubuga
Try this
Trigger ContactSerialDate on Contact (before insert, before update) {
	
	Set<Id> accountIds = new Set<Id>();
    List<Account> accountsToUpdate = new List<Account>();

    for (Contact c : Trigger.new) {
        accountIds.add(c.accountId);
    }

    Map<id, Account> acctMap = new Map<id, Account>([select id, AssignDate__c from Account where id IN :accountIds]);
    
    for (Contact c : Trigger.new) {
        c.SerialDate__c = acctMap.get(c.accountId).AssignDate__c.addMonths(1);
    }
}

 
Prashant Pandey07Prashant Pandey07
+Steven..
 
Trigger ContactSerialDate on Contact (before insert, before update) {
	
	Set<Id> accountIds = new Set<Id>();
    List<Account> accountsToUpdate = new List<Account>();

    for (Contact c : Trigger.new) {
        accountIds.add(c.accountId);
    }

    Map<id, Account> acctMap = new Map<id, Account>([select id, AssignDate__c from Account where id IN :accountIds]);
    
    for (Contact c : Trigger.new) {
     if(acctMap.get(c.accountId)!=null   ||  acctMap.get(c.accountId).AssignDate__c!=null)
        c.SerialDate__c = acctMap.get(c.accountId).AssignDate__c.addMonths(1);
    }

}

--
Thanks,
Prashant
Steven NsubugaSteven Nsubuga
Try this
Trigger ContactSerialDate on Contact (before insert, before update) {
	
	Set<Id> accountIds = new Set<Id>();
	
    for (Contact c : Trigger.new) {
        accountIds.add(c.accountId);
    }
	
    Map<id, Account> acctMap = new Map<id, Account>([select id, AssignDate__c from Account where id IN :accountIds]);
    Map<String, List<Contact>> acctContacts = new Map<String, List<Contact>>();
	
	for (Contact c : Trigger.new) {
		List<Contact> contacts = acctContacts.get(c.accountId);
		if (contacts == null) {
			contacts = new List<Contact>();
		}
		contacts.add(c);
		if(acctMap.get(c.accountId)!=null && acctMap.get(c.accountId).AssignDate__c!=null) {
			c.SerialDate__c = acctMap.get(c.accountId).AssignDate__c.addMonths(contacts.size());
		}
		acctContacts.put(c.accountId, contacts);
    }
}

 
Ajay K DubediAjay K Dubedi
Hi,

SerialDate field and AssignDate field is not the standard field of contact and account.
 
if you talk about the created date and Last Modified date both are same.
 
trigger CreateAccountContact on Account (After insert) {   
    List<Contact> conList = new List<Contact>();
    for(Account acc:Trigger.New){
        if(acc.Id!=Null){
            Contact c = new Contact();
            c.LastName = acc.Name;
            c.AccountId =acc.Id;
            c.Fax=acc.Fax;
            c.MailingStreet=acc.BillingStreet;
            c.MailingCity=acc.BillingCity;
            c.MailingState=acc.BillingState;
            c.MailingPostalCode=acc.BillingPostalCode;
            c.MailingCountry=acc.BillingCountry;
            c.Phone=acc.Phone;
                conList.add(c);
        }
    }
    
    insert conList;
}

Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi
Steven NsubugaSteven Nsubuga
In case you still need it, 
Trigger ContactSerialDate on Contact (before insert) {
    
    Set<Id> accountIds = new Set<Id>();
    Map<String, List<Contact>> acctIdContacts = new Map<String, List<Contact>>();
    
    Map<String, Integer> acctContactsCount = new Map<String, Integer>();
    
    for (Contact c : Trigger.new) {
        accountIds.add(c.accountId);
        
        List<Contact> contacts = acctIdContacts.get(c.accountId);
        if (contacts == null) {
            contacts = new List<Contact>();
        }
        contacts.add(c);
        acctIdContacts.put(c.accountId, contacts);
    }
    
    Map<id, Account> acctMap = new Map<id, Account>([select id, AssignDate__c from Account where id IN :accountIds]);
    List<AggregateResult> ars = [SELECT AccountId, count(Id) FROM Contact WHERE AccountId IN :accountIds group by AccountId];
    for (AggregateResult ar : ars) {
        acctContactsCount.put(String.valueOf(ar.get('AccountId')), Integer.valueOf(ar.get('expr0')));
    }
    
    for (Contact c : Trigger.new) {
        if (acctContactsCount.get(c.accountId) == null) {
            acctContactsCount.put(c.accountId, 0);
        }
    }
    
    for (List<Contact> contacts : acctIdContacts.values()) {
        for (Integer s = 1; s < contacts.size() + 1; s++) {
            Contact c = contacts.get(s-1);
            integer index = Trigger.new.indexOf(c);
            Trigger.new.get(index).SerialDate__c = acctMap.get(c.accountId).AssignDate__c.addMonths((acctContactsCount.get(c.accountId) + s));
        }
    }
}

and test class
@isTest
private class ContactSerialDateTest {

    @isTest static void testTrigger(){
    
        // Create Account records
        List<Account> accounts = new List<Account>();
        for (Integer s = 1; s < 3; s++) {
            Account acct = new Account(Name = 'TestAccount' + s, AssignDate__c = date.today().addDays(s));
            accounts.add(acct);
        }
        insert accounts;
        
        // Create Contact records
        List<Contact> contacts = new List<Contact>();
        for (Integer s = 0; s < 2; s++) {
            Contact ct = new Contact(AccountId = accounts[s].Id, LastName = 'TestContact' + s);
            contacts.add(ct);
        }
        insert contacts;
        
        List<Contact> contacts2 = [SELECT Account.Name, Account.AssignDate__c, SerialDate__c FROM Contact];
        for (Contact c : contacts2) {
            System.assertEquals(c.Account.AssignDate__c.addMonths(1), c.SerialDate__c);
        }
        
        // Create additional Contact records for 1 of the Accounts
        List<Contact> contacts3 = new List<Contact>();
        for (Integer s = 0; s < 2; s++) {
            Contact ct = new Contact(AccountId = accounts[0].Id, LastName = 'TestContact-' + s);
            contacts3.add(ct);
        }
        insert contacts3;
        List<Contact> contacts4 = [SELECT Account.Name, Account.AssignDate__c, SerialDate__c FROM Contact WHERE AccountId =:accounts[0].Id order by SerialDate__c ASC];
        for (Integer i = 1; i < contacts4.size() + 1; i++) {
            System.assertEquals(contacts4[i-1].Account.AssignDate__c.addMonths(i), contacts4[i-1].SerialDate__c);
        }
    }
}