+ Start a Discussion
Koustubh KulkarniKoustubh Kulkarni 

Create multiple records of same other fields but different one field?

Hello,
I am new to salesforce. I have one junction object called allocation__c which is junction between contact and project__c. Allocation has various fields. after selecting time period(1 week,2 week,1 month,2 months etc.) i want to generate multiple records with same field values but each record for only one week.i.e. if i select 1 month then there should be four records with other parameters remaining same but just change in start and end date. how can we do that??
Best Answer chosen by Koustubh Kulkarni
Vishal_GuptaVishal_Gupta
Hi Koustubh,

Please use the below code for implementing your requirement, make changes according to your buisness rules :
 
trigger ProjectTrg on Project__c (after insert)
{
	List<Allocation__c> allocations = new List<Allocation__c>();
	
	for( Project__c p : trigger.new )
	{
		if( p.Time_Period__c != null )
		{
			Integer vNoOfWeek = TriggerUtil.getNumberOfWeeks(p.Time_Period__c);
			allocations.addAll(TriggerUtil.createAllocations(vNoOfWeek,p));
		}
	}
	
	if( allocations.size() > 0 )
		insert allocations;
}

public class TriggerUtil
{
	//Modify below code with your requirement
	public static List<Allocation__c> createAllocations(Integer vWeeksCount,Project__c vProject)
	{
		Integer startD = 0;
		Integer endD = startD + 7;
		List<Allocation__c> lstAllocation = new List<Allocation__c>();
		for( Integer i = 0; i < vWeeksCount; i++ )
		{
			Allocation__c allo = new Allocation__c( Start_Date__c = Date.today().addDays( startD ), End_Date__c = Date.today().addDays( endD ), Contact__c = vProject.Contact__c, Project__c = vProject.Id );
			lstAllocation.add(allo);
			
			startD += 8;
			EndD += 7;
		}
		return lstAllocation;
	}
	
	//Modify below code with your requirement
	public static Integer getNumberOfWeeks(String vTimePeriod)
	{
		if(vTimePeriod == '1 Week')
			return 1;
		else if(vTimePeriod == '2 Week')	
			return 2;
		else if(vTimePeriod == '3 Week')	
			return 3;
		else if(vTimePeriod == '1 Month')	
			return 4;	
		else if(vTimePeriod == '2 Month')	
			return 8;
		return 0;		
	}
}
Let me know if I can help you more.

Thanks,
Vishal
 

All Answers

Vishal_GuptaVishal_Gupta
Hi Koustubh,

You can write down your code in allocation__c trigger. 

Please let me know if I can help you in writing that trigger.

Thanks,
Vishal
Koustubh KulkarniKoustubh Kulkarni
Thanks vishal.can you please tell me how to do that by trigger?
Vishal_GuptaVishal_Gupta
Hi Koustubh,

Please use the below code for implementing your requirement, make changes according to your buisness rules :
 
trigger ProjectTrg on Project__c (after insert)
{
	List<Allocation__c> allocations = new List<Allocation__c>();
	
	for( Project__c p : trigger.new )
	{
		if( p.Time_Period__c != null )
		{
			Integer vNoOfWeek = TriggerUtil.getNumberOfWeeks(p.Time_Period__c);
			allocations.addAll(TriggerUtil.createAllocations(vNoOfWeek,p));
		}
	}
	
	if( allocations.size() > 0 )
		insert allocations;
}

public class TriggerUtil
{
	//Modify below code with your requirement
	public static List<Allocation__c> createAllocations(Integer vWeeksCount,Project__c vProject)
	{
		Integer startD = 0;
		Integer endD = startD + 7;
		List<Allocation__c> lstAllocation = new List<Allocation__c>();
		for( Integer i = 0; i < vWeeksCount; i++ )
		{
			Allocation__c allo = new Allocation__c( Start_Date__c = Date.today().addDays( startD ), End_Date__c = Date.today().addDays( endD ), Contact__c = vProject.Contact__c, Project__c = vProject.Id );
			lstAllocation.add(allo);
			
			startD += 8;
			EndD += 7;
		}
		return lstAllocation;
	}
	
	//Modify below code with your requirement
	public static Integer getNumberOfWeeks(String vTimePeriod)
	{
		if(vTimePeriod == '1 Week')
			return 1;
		else if(vTimePeriod == '2 Week')	
			return 2;
		else if(vTimePeriod == '3 Week')	
			return 3;
		else if(vTimePeriod == '1 Month')	
			return 4;	
		else if(vTimePeriod == '2 Month')	
			return 8;
		return 0;		
	}
}
Let me know if I can help you more.

Thanks,
Vishal
 
This was selected as the best answer
Koustubh KulkarniKoustubh Kulkarni
Thank you very much Vishal. I think your code's logic is perfect. i will apply your solution for my problem and will let you know if there is any problem or not. THANK YOU VERY MUCH!!
Koustubh KulkarniKoustubh Kulkarni
hey vishal can you tell me relationship between project_c and allocation_c in your code?field name?
 
Vishal_GuptaVishal_Gupta
Hi Koustubh,

Alloction is child of Project object or technically say Allocation object has Lookup of Project.

Please mark answer as best answer so it will help others in community.

Thanks,
Vishal
Koustubh KulkarniKoustubh Kulkarni
hey vishal, just wanted to ask you that how to take name on the lookup filed on text field. I have implemented the code but there is obly one glitch that i cant map name on the lookup field to text field.can you help me??
Vishal_GuptaVishal_Gupta
Hi Koustubh,

You can write code like this :

customLookupField__r.Name 

Thansk,
Vishal
Koustubh KulkarniKoustubh Kulkarni
I knew that but its not showing anything.
Vishal_GuptaVishal_Gupta
Hi Koustubh,

Can you share your code so I can check and let you know.

Thanks,
Vishal
Koustubh KulkarniKoustubh Kulkarni
I have taken name from contact object to allocation object as well as project name from project object. and there is lookup relationship between allocation and allocationperweek....i want to display contact name and project name on the allocationperweek object 
Koustubh KulkarniKoustubh Kulkarni
public class TriggerUtil 
{
//Modify below code with your requirement
    public static List<Allocationsperweek__c> createAllocations(Integer vWeeksCount,Allocation__c vProject)
    {
        Integer startD = 0;
        Integer endD = startD + 4;
        List<Allocationsperweek__c> lstAllocation = new List<Allocationsperweek__c>();
        for( Integer i = 0; i < vWeeksCount; i++ )
        {
            Allocationsperweek__c allo = new Allocationsperweek__c( Start_Date__c = Date.today().addDays( startD ), End_Date__c = Date.today().addDays( endD ),Allocation__c = vProject.Id,Allocated_Percentage__c=vProject.Allocated_Percentage__c,Employee_name__c=vProject.Employee_name__r.Name,Project_Name__c=vProject.Project_Name__r.Name,Reported_By__c=vProject.Reported_By__r.Name);
            lstAllocation.add(allo);
            
            startD += 7;
            EndD += 7;
        }
        return lstAllocation;
    }
    
    //Modify below code with your requirement
    public static Integer getNumberOfWeeks(String vTimePeriod)
    {
        if(vTimePeriod == '1 Week')
            return 1;
        else if(vTimePeriod == '2 Week')    
            return 2;
        else if(vTimePeriod == '3 Week')    
            return 3;
        else if(vTimePeriod == '1 Month')   
            return 4;   
        else if(vTimePeriod == '2 Month')   
            return 8;
        else if(vTimePeriod == '3 Month')   
            return 12;
        return 0;       
    }

}
Koustubh KulkarniKoustubh Kulkarni
any clue whats wrong???
 
Vishal_GuptaVishal_Gupta
Hi,

Please do query on the related objects to get the value, in trigger we can't get the related fields value directly, we need to do query first and then can access it.

Thanks,
Vishal
Koustubh KulkarniKoustubh Kulkarni
can you explain how to do it with above code?? i am getting all other fields but not these 2 lookup values. can you please give me any help querying that. Thanks