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
venkatsforcevenkatsforce 

Clone a record using textfield

I want to clone a record in same object uisng textfield......

For example:

 

if i have a Quantity__c field in object and fill the field with some numbers like 1,2,3 ....,and i press the save button, automatically it creates 3records or 2records in samke object as per the basis of quantity field uisng triggers(After insert).......

 

 

 

thanks

=========

Venkat Sforce

Best Answer chosen by Admin (Salesforce Developers) 
jbroquistjbroquist

First off I would clone the object using a number field and not a text field for Quantity. In the example below lets assume the SObject you were wanting to clone was named Service__c.

 

trigger AfterServiceUpdate on Service__c (after insert, after update) 
{
	//instantiate list of cloned services to insert
	Service__c[] services = new Service__c[]{};

	//iterate through updated sprockets
	for(Service__c service : Trigger.new)
	{
		Service__c oldService = (Trigger.isUpdate) ? Trigger.oldMap.get(service.Id) : null;

		//check to see if the quantity has changed
		//this logic could be expanded to find the difference of the two quantities
		//and create the appropriate amount of records based on the difference
		if(Trigger.isInsert || (Trigger.isUpdate && service.Quantity__c != oldService.Quantity__c))
		{
			//clone the record Quanitiy__c number of times
			for(Integer i=0; i<service.Quantity__c; i++)
			{
				Service__c clonedService = service.clone();
				clonedService.Quantity__c = 0;
				services.add(clonedService);
			}
		}
	}

	//add cloned services
	if(services.size() > 0) insert services;
}

 

 

And here is a basic unit test to verify to works:

@isTest
private class AfterServiceUpdateTest {
	
	@isTest static void test_method_one() 
	{
		Service__c s = new Service__c();
		s.Quantity__c = 0;
		insert s;

		Test.startTest();

		s.Quantity__c = 2;
		update s;

		Service__c[] allServices = [SELECT Id FROM Service__c];
		//there should now be a total of 3 services since we updated the quanity to 2
		system.assertEquals(3, allServices.size());

		Test.stopTest();
	}
	
}

 Let me know if you have any questions.

All Answers

prakash_sfdcprakash_sfdc
Hi,

Why can't you have a parent object having Quantity field ? I don't think DML is allowed in Trigger.new
jbroquistjbroquist

First off I would clone the object using a number field and not a text field for Quantity. In the example below lets assume the SObject you were wanting to clone was named Service__c.

 

trigger AfterServiceUpdate on Service__c (after insert, after update) 
{
	//instantiate list of cloned services to insert
	Service__c[] services = new Service__c[]{};

	//iterate through updated sprockets
	for(Service__c service : Trigger.new)
	{
		Service__c oldService = (Trigger.isUpdate) ? Trigger.oldMap.get(service.Id) : null;

		//check to see if the quantity has changed
		//this logic could be expanded to find the difference of the two quantities
		//and create the appropriate amount of records based on the difference
		if(Trigger.isInsert || (Trigger.isUpdate && service.Quantity__c != oldService.Quantity__c))
		{
			//clone the record Quanitiy__c number of times
			for(Integer i=0; i<service.Quantity__c; i++)
			{
				Service__c clonedService = service.clone();
				clonedService.Quantity__c = 0;
				services.add(clonedService);
			}
		}
	}

	//add cloned services
	if(services.size() > 0) insert services;
}

 

 

And here is a basic unit test to verify to works:

@isTest
private class AfterServiceUpdateTest {
	
	@isTest static void test_method_one() 
	{
		Service__c s = new Service__c();
		s.Quantity__c = 0;
		insert s;

		Test.startTest();

		s.Quantity__c = 2;
		update s;

		Service__c[] allServices = [SELECT Id FROM Service__c];
		//there should now be a total of 3 services since we updated the quanity to 2
		system.assertEquals(3, allServices.size());

		Test.stopTest();
	}
	
}

 Let me know if you have any questions.

This was selected as the best answer
ItswasItswas
Hi Venkat
trigger CloneonOpportunityQuantity on Account(before insert){integer i ;List<Account> ObjMainAcc = new List<Account>();
for(Account acc : Trigger.New)
{ for(i=0;i<acc.NumberOfEmployees;i++){
Account cloNewAcc = new Account();
cloNewAcc = acc.Clone();
ObjMainAcc.add(cloNewAcc); }
}
if(!ObjMainAcc.Isempty())
insert ObjMainAcc;
}
Set a class (to check the recursive)
public class checkRecussiveValue
{public static boolean chck = true;}

Please follow the above code which is dummy for your requirment.Hope this help you.
let me know whether you find the solution or not.

Regards,
Itsaws.
venkatsforcevenkatsforce

Thanks  jbroquist..........

 

i got the result........i edit somethings in tht code and got the result....

 

trigger cloningrecords on Members__c (after insert)
{
  if(Trigger.isInsert)
  {
    Members__c[] memb = new Members__c[]{};
    for(Members__c member : Trigger.new)
    {
      for(Integer i=0; i<member.Quantity__c; i++)
       {
          for(Members__c oldmember:[Select Id,Name,Member_Intrest__c from Members__c where Id = :member.Id])
          {
            if(member.Quantity__c != null)
            {
                Members__c clonedMember = new Members__c();
                clonedMember.Quantity__c = null;
                clonedMember.Member_Intrest__c = member.Member_Intrest_Name;
                clonedMember.Name = member.Name;
                memb.add(clonedMember);
            }
        }
    }
    if(memb.size() > 0)
    {
    insert memb;
    }
    }
}
}