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
kittu9kittu9 

How to clone through apex code??

trigger CloneRecord on Opportunity (after update)
{
for(Opportunity o : Trigger.new)
{
if(Trigger.isUpdate)
{
if(o.IL_Auto_Renew__c == true && o.ForecastCategoryName == 'Closed' && o.StageName == 'won' )
{

}
}
}
}

If above condition satisfies, Have to clone the record through apex class.

Stage = Agreement
Start date = exactly one year tats 12 months from the closed date of the previous record that is from which this one is getting cloned
End date = Start date of this record plus duration of the previous record. Duration of previous record can be calculated by inserting formula in the trigger as closed date minus created date.
 
Can any give ideas how to clone through apex code ASAP.
ArleneArlene

I did not actually compile it, so there may be errors, but I believe the following will work for the body of your trigger:

 

List<Opportunity> clonedRecords = new List<Opportunity>();

for(Opportunity o : Trigger.new) {

    if(o.IL_Auto_Renew__c == true && o.ForecastCategoryName == 'Closed' && o.StageName == 'won') {

        newOpp = o.clone(false, false, false, false);

        newOpp.StageName = 'Agreement';

        newOpp.StartDate = o.EndDate.addYears(1);

        // add as many changes to the record as you want, and then

        clonedRecords.add(newOpp);

    }

}

if (clonedRecords.size() > 0) insert clonedRecords;

 

See http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject.htm  to see why I specified false,false,false,false on the clone.

kittu9kittu9

trigger CloneRecord on Opportunity (after update)

{

List clonedRecords = new List();

Opportunity newOpp;

for(Opportunity o : Trigger.new)

{

if(o.StageName == 'Qualification')

{

newOpp = new Opportunity(o.clone(false, false, false, false));

newOpp.StageName = 'Qualification';

newOpp.StartDate__c = o.EndDate.addYears(1); // add as many changes to the record as you want, and then clonedRecords.add(newOpp);

}

}

if (clonedRecords.size() > 0) insert clonedRecords; }

 

Getting the following error

 

    Error: Compile Error: SObject constructor must use name=value pairs at line 11 column 18   

ArleneArlene

I think it might be how this site is handling line breaks?  There should be a new line betweeen "then" and "ClonedRecords.add".  It's a little hard for me to tell where the error is, because I don't know which line is line 11.   But I think using the "new" was a mistake, and the line should read:

 

newOpp = o.clone(false, false, false, false);

 

Also, I think this: 

List clonedRecords = new List();

 

I think needs to be this:

List<Opportunity> clonedRecords = new List<Opportunity> ();

Vinit_KumarVinit_Kumar

I see you are missing name field for newOpp while inserting the cloned records.As name field is mandatory  field,it is throwing the error,

 

Try adding

 

newOpp.name='<Some name>';

 

I guess this should resolve it.

kittu9kittu9

trigger CloneRecord on Opportunity (after update)
{
List<Opportunity> clonedRecords = new List<Opportunity> ();

Opportunity newOpp;

for(Opportunity o : Trigger.new)
{
if(o.StageName == 'Qualification')
{
newOpp = new Opportunity(o.clone(false, false, false, false)); // this is the 11th line

newOpp.name='Clone Opportunity';

newOpp.StageName = 'Qualification';

newOpp.StartDate__c = o.EndDate.addYears(1); // add as many changes to the record as you want, and then clonedRecords.add(newOpp);

clonedRecords.add(newOpp);
}
}
if (clonedRecords.size() > 0) insert clonedRecords;
}

while saving this trigger ,the following error is coming please help me on this

Error: Compile Error: SObject constructor must use name=value pairs at line 11 column 12

 

any syntax error in 11 th line please let me know

ArleneArlene

The 11th line should not have a new in it:

 

newOpp = o.clone(false, false, false, false);

Suraj Tripathi 47Suraj Tripathi 47
Hi Kittu9,
"Try this code there is no error in this code."
trigger CloneRecord on Opportunity (after update)
{
List<Opportunity> clonedRecords = new List<Opportunity> ();

Opportunity newOpp;

for(Opportunity o : Trigger.new)
{
if(o.StageName == 'Qualification')
{
newOpp = o.clone(false, false, false, false);

newOpp.name='Clone Opportunity';

newOpp.StageName = 'Qualification';

newOpp.StartDate__c = o.EndDate.addYears(1);
clonedRecords.add(newOpp);
clonedRecords.add(newOpp);
}
}
if (clonedRecords.size() > 0) 
insert clonedRecords;
}
If you find your Solution then mark this as the best answer. 

Thank you!

Regards 
Suraj Tripathi