function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
123_P123_P 

please solve this question on trigger using map

Create object : Candidate__c
Fields : (All Text fields)
First Name
Last Name
Email
Brokerage
Manage Brokerage
Candidate Status (Webinar - Attended , Webinar -Not Attended)
User Created ( checkbox)


On insert:
1. Create Account if Brokerage !=null
2. Create another account if Manage Brokerage != null and set parentId = Brokerage account id (created in 1 point)
3. Create new contact record and set accountId = Manage Brokerage account id (created in 2 point)
   create new field candidate__c(lookup) on contact and fill it with Condidate__c record id
4. Create task record for contact record (created in point 3)
Task.WhoId = contactId
Task.WhatId = Candidate__c
Task.Subject = 'Portal Contact Setup';
 
Niraj Kr SinghNiraj Kr Singh
Hi Aarush,

You can try this code snipet:
/********* Trigger ********/
trigger CandidateTrigger on Candidate__c(after insert) {
	if(trigger.isAfter && trigger.isInsert) {
		CandidateTrigger.afterInsertProcess(trigger.new);
	}
}

/************ Trigger Handler ************/
public class CandidateTriggerHandler {
	
	public CandidateTriggerHandler() {
		//Do-Initialization if needed
	}
	
	public static void afterInsertProcess(List<Candidate__c> newCandidateList) {
		List<Task> newTaskList = new List<Task>();
		List<Contact> newContactList = new List<Contact>();
		Map<Id, Account> canIdsVsBrokerageAccMap = new Map<Id, Account>();
		Map<Id, Account> canIdsVsMngBrokerageAccMap = new Map<Id, Account>();
		if(!newCandidateList.isEmpty()) {
			//First account (Point-1)
			for(Candidate__c objCand: newCandidateList) {
				if(objCand.Brokerage__c != null) {
					canIdsVsBrokerageAccMap.put(objCand.Id, getAccounts(objCand, null, objCand.Brokerage__c));
				}
			}
			//Insert 1st account list
			if(!canIdsVsBrokerageAccMap.isEmpty()) {
				insert canIdsVsBrokerageAccMap.values(); //list of 1st accounts
			}
			
			//Second Account (Point-2)
			for(Candidate__c objCand: newCandidateList) {
				if(objCand.Brokerage__c != null && objCand.Manage_Brokerage__c != null) {
					canIdsVsMngBrokerageAccMap.put(objCand.Id, getAccounts(objCand, canIdsVsBrokerageAccMap.get(objCand.Id).Id, objCand.Manage_Brokerage__c));
				}
				else {
					//Not added Candidates for Account.
				}
			}
			//Insert 2nd account list
			if(!canIdsVsMngBrokerageAccMap.isEmpty()) {
				insert canIdsVsMngBrokerageAccMap.values(); //list of 2nd accounts
				
				//Create contacts for all 2nd Accounts (Point-3)
				for(Id candId : canIdsVsMngBrokerageAccMap.keySet()) {
					newContactList.add(getContacts(candId, canIdsVsMngBrokerageAccMap.get(candId)));
				}
				if(!newContactList.isEmpty()){
					insert newContactList;
					
					//Create Task (Point-4)
					for(Contact objContact : newContactList) {
						newTaskList.add(getTasks(objContact));
					}
					if(!newTaskList.isEmpty()) {
						insert newTaskList;
					}
				}
			}
		}
	}
	
	//Create Account
	private static Account getAccounts(Candidate__c objCand, Id parentAccId, String strBrokerageOrManage) {
		Account objAccount = new Account();
		objAccount.Name = objCand.First_Name__c + ' ' + objCand.Last_Name__c + ' ' + strBrokerageOrManage;
		objAccount.ParentId = parentAccId;
		//Add other account required fields here one by one.
		
		return objAccount;
	}
	
	//Create Contact
	private static Contact getContacts(Id candId, Account objParentAccount) {
		Contact objContact = new Contact();
		objContact.FullName = objParentAccount.Name;
		objContact.AccountId = objParentAccount.Id;
		objContact.candidate__c = candId;
		//Add other contact required fields here one by one.
		
		return objContact;
	}
	
	//Create Task
	private static Task getTasks(Contact objContact) {
		Task objTask = new Task();
		objTask.WhoId = objContact.Id;
		objTask.WhatId = objContactCandidate__c;
		objTask.Subject = 'Portal Contact Setup';
		//Add other task required fields here one by one.
		
		return objTask;
	}
}
And if it works for you Mark your best answer to help others.

Thanks
Niraj
 
Akshay_DhimanAkshay_Dhiman
Hi Aarush,

Just Try this one out,
There is one line commented in Task creation of whatId, That should Non - commented only when the 
Candidate object is the child object of Account.
 
trigger TriggerCandidate on Candidate__c (after insert) {
    Map<Id ,Contact> ConListMap = new Map<Id ,Contact>();
    Map<Id ,Account> BroConList = new Map<Id ,Account>();
    Map<Id ,Account> mapBroConList = new Map<Id ,Account>();
    Map<Id ,Task> TaskConList = new Map<Id ,Task>();
    
    List<Account> ManageBroacclist = new List<Account>();
    
    for(Candidate__c con : trigger.New)
    {
        if(con.Brokerage__c!=Null && con.Manage_Brokerage__c!=Null)
        {
            Account Brokerageacc = new  Account();
            Brokerageacc.Name = 'Brokerage Acc';
            if(!BroConList.containsKey(con.Id))
            {
                BroConList.put(con.Id, Brokerageacc);
            }
        }
    }
    if(BroConList.values().size()>0)
    {
        insert BroConList.values();
    }
    for(Candidate__c cont : trigger.New)
    {
        if(cont.Brokerage__c!=Null && cont.Manage_Brokerage__c!=Null)
        {
            Account ManageBrokerageAcc = new Account();
            ManageBrokerageAcc.Name = 'Manage Brokerage Account';
             System.debug('BroconList.get(cont.id).Id---->>>>' + BroconList.get(cont.id).Id);
            if(BroconList.containsKey(cont.id)){
                System.debug('BroconList.get(cont.id).Id---->>>>' + BroconList.get(cont.id).Id);
                ManageBrokerageAcc.ParentId = BroconList.get(cont.id).Id;
            }
            
            if(!mapBroconList.containsKey(cont.id))
            {
                mapBroconList.put(cont.id, ManageBrokerageAcc);
            }
            
            contact contactnew = new contact();
            contactnew.LastName = 'Test cont';
            contactnew.Candidate__c = cont.Id;
            if(!conListMap.containsKey(cont.Id)) 
            {
                conListMap.put(cont.Id, contactnew);  
            }
        }
    }
    if(mapBroConList.values().size()>0)
    {
        insert mapBroConList.values();
    }
    if(ConListMap.values().size()>0)
    {
        insert ConListMap.values();
    }
    
    for(Candidate__c cont3 : trigger.New)
    {
        if(cont3.Brokerage__c!=Null && cont3.Manage_Brokerage__c!=Null)
        {
            Task tt = new Task();
            
            if(conListMap.containsKey(cont3.Id)) 
            {
                tt.WhoId = conListMap.get(cont3.Id).Id;
            }
            
           // tt.WhatId = cont3.Id;
            tt.Subject =  'Portal Contact Setup';
            
            if(!TaskConList.containsKey(cont3.id))
            {
                TaskConList.put(cont3.id , tt);
            }
        }
    }
    if(TaskConList.values().size()>0)
    {
        insert TaskConList.values();
    }
    
}


Thanks 
Akshay
123_P123_P
akshay in account acccount is not creating
 
Akshay_DhimanAkshay_Dhiman
Hi Aarush,

It could be because of in your org, there would be some more required fields present,
I could not get you completely, so just clarify what you are saying 
So, that I can help you.

I have tested this code on my end and its working fine absolutely.

Thanks 
Akshay
123_P123_P
thnx akshay its working
akshay can you make it using handler
Niraj Kr SinghNiraj Kr Singh
Hi Aarush,
You can try once the above one which is written using handler.
123_P123_P
i tried but its not working niraj its giving error please check it out
Akshay_DhimanAkshay_Dhiman
Hi Aarush,

of course you can do this by the handler as well -- 

--------------------  Trigger --------------------------------------
 
trigger TriggerCandidate on Candidate__c (after insert) {
if(Trigger.isAfter && Trigger.isInsert)
{
CandidateRecordTriggerHandler.relatedRecordBuilding(trigger.New);
}
}
-------------------- Its Handler Class -------------------
public class CandidateRecordTriggerHandler
{
public static void relatedRecordBuilding(List<Candidate__c> canRecords)
{
Map<Id ,Contact> ConListMap = new Map<Id ,Contact>();
    Map<Id ,Account> BroConList = new Map<Id ,Account>();
    Map<Id ,Account> mapBroConList = new Map<Id ,Account>();
    Map<Id ,Task> TaskConList = new Map<Id ,Task>();
    
    List<Account> ManageBroacclist = new List<Account>();
    
    for(Candidate__c con : canRecords)
    {
        if(con.Brokerage__c!=Null && con.Manage_Brokerage__c!=Null)
        {
            Account Brokerageacc = new  Account();
            Brokerageacc.Name = 'Brokerage Acc';
            if(!BroConList.containsKey(con.Id))
            {
                BroConList.put(con.Id, Brokerageacc);
            }
        }
    }
    if(BroConList.values().size()>0)
    {
        insert BroConList.values();
    }
    for(Candidate__c cont : canRecords)
    {
        if(cont.Brokerage__c!=Null && cont.Manage_Brokerage__c!=Null)
        {
            Account ManageBrokerageAcc = new Account();
            ManageBrokerageAcc.Name = 'Manage Brokerage Account';
             System.debug('BroconList.get(cont.id).Id---->>>>' + BroconList.get(cont.id).Id);
            if(BroconList.containsKey(cont.id)){
                System.debug('BroconList.get(cont.id).Id---->>>>' + BroconList.get(cont.id).Id);
                ManageBrokerageAcc.ParentId = BroconList.get(cont.id).Id;
            }
            
            if(!mapBroconList.containsKey(cont.id))
            {
                mapBroconList.put(cont.id, ManageBrokerageAcc);
            }
            
            contact contactnew = new contact();
            contactnew.LastName = 'Test cont';
            contactnew.Candidate__c = cont.Id;
            if(!conListMap.containsKey(cont.Id)) 
            {
                conListMap.put(cont.Id, contactnew);  
            }
        }
    }
    if(mapBroConList.values().size()>0)
    {
        insert mapBroConList.values();
    }
    if(ConListMap.values().size()>0)
    {
        insert ConListMap.values();
    }
    
    for(Candidate__c cont3 : canRecords)
    {
        if(cont3.Brokerage__c!=Null && cont3.Manage_Brokerage__c!=Null)
        {
            Task tt = new Task();
            
            if(conListMap.containsKey(cont3.Id)) 
            {
                tt.WhoId = conListMap.get(cont3.Id).Id;
            }
            
           // tt.WhatId = cont3.Id;
            tt.Subject =  'Portal Contact Setup';
            
            if(!TaskConList.containsKey(cont3.id))
            {
                TaskConList.put(cont3.id , tt);
            }
        }
    }
    if(TaskConList.values().size()>0)
    {
        insert TaskConList.values();
    }
}


Thanks 
Akshay
123_P123_P
thnx akshay
 
Mohd NabeelMohd Nabeel
Can we do this using before event??? If yes then what would be the difference between the two??