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
vamshi(Nani)vamshi(Nani) 

Trigger error..

This is the trigger erroHI  this code makes me hell..
This is the trigger code on my opportunity but throwing above exception can you please help me... Let me give you detail explination.

I am new to triggers..the trigger is when I insert an opportunity a new opportunity should be created automatically on the opportunity  I write the trigger is below..but it is throwing exception as screen shot..

 

trigger new_oppy on Opportunity (after insert)
{  
    
    if(Trigger.isinsert)
    {
        opportunity o=new opportunity();
        o.name = 'vamshi opportunity';
        o.closedate = date.today();
        o.StageName='closedwon';
        insert o;
    }

 

 

ERROR:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger wrehoue.new_oppy caused an unexpected exception, contact your administrator: wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, wrehoue.new_oppy: maximum trigger depth exceeded Opportunity trigger event AfterInsert for [0069000000LVsuf] Opportunity trigger event AfterInsert for [0069000000LVsug] Opportunity trigger event AfterInsert for [0069000000LVsuh] Opportunity trigger event AfterInsert for [0069000000LVsui] Opportunity trigger event AfterInsert for [0069000000LVsuj] Opportunity trigger event AfterInsert for [0069000000LVsuk] Opportunity trigger event AfterInsert for [0069000000LVsul] Opportunity trigger event AfterInsert for [0069000000LVsum] Opportunity trigger event AfterInsert for [0069000000LVsun] Opportunity trigger event AfterInsert for [0069000000LVsuo] Opportunity trigger event AfterInsert for [0069000000LVsup] Opportunity trigger event AfterInsert for [0069000000LVsuq] Opportunity trigger event AfterInsert for [0069000000LVsur] Opportunity trigger event AfterInsert for [0069000000LVsus] Opportunity trigger event AfterInsert for [0069000000LVsut] Opportunity trigger event AfterInsert for [0069000000LVsuu]: []: Trigger.wrehoue.new_oppy: line 11, column 1

Best Answer chosen by vamshi(Nani)
Denis VakulishinDenis Vakulishin
Anyway you should determine condition on which new Opp won't be created for renewal Opp. In my sample it's a new custom field "ParentOpp__c", also it could be the name(if name ends with "_renewal")

All Answers

James LoghryJames Loghry
So you're trigger needs a lot of work.  I would first recommend reading a getting started with triggers document like this: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_qs_HelloWorld.htm

T
hat being said, you're trigger is kicking off an infinite loop anytime you are creating an Opportunity, because the trigger itself creates another opportunity.  

We could probably help you more if you provide us some details around what you're trigger is supposed to accomplish.
vamshi(Nani)vamshi(Nani)

Hi James,

 

Thank you for your support,

My requirement is when I created an opportunity a renewal opportunity should be created automatically...

Ex: if I create an Opportunity with the name: singapore_production then a rerewal should be created automatically with Singapore_production_renewal..This is my requirement don't worry about remaining...fileds please paste a sample code...

Vamshi

Denis VakulishinDenis Vakulishin
trigger new_oppy on Opportunity (after insert)
{  
    
    if(Trigger.isInsert&&Trigger.isAfter)
    {
        List<Opportunity> newOpps = new List<Opportunity>();
        for(Opportunity opp: Triger.new){
           if(opp.ParentOpp__c!=null) continue;
           Opportunity newOpp=new Opportunity();
           newOpp.name = opp.Name+'_renewal';
           newOpp.closedate = date.today();
           newOpp.StageName='Qualified';//Set your initial stage name
           newOpp.ParentOpp__c = opp.Id;
           newOpps.add(newOpp);
        }
        insert newOpps;
    }
}

Denis VakulishinDenis Vakulishin
Anyway you should determine condition on which new Opp won't be created for renewal Opp. In my sample it's a new custom field "ParentOpp__c", also it could be the name(if name ends with "_renewal")
This was selected as the best answer
vamshi(Nani)vamshi(Nani)

Denis..

this is the error i'm getting.. I think some where the trigger recursion happens here..

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger wrehoue.new_oppy caused an unexpected exception, contact your administrator: wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, wrehoue.new_oppy: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: STRING_TOO_LONG, Name: data value too large: nani_renewal_renewal_renewal_renewal_renewal_renewal_renewal_renewal_renewal_renewal_renewal_renewal_renewal_renewal_renewal (max length=120): [Name] Trigger.wrehoue.new_oppy: line 17, column 1: [] Trigger.wrehoue.new_oppy: line 17, column 1: [] Trigger.wrehoue.new_oppy: line 17, column 1: [] Trigger.wrehoue.new_oppy: line 17, column 1: [] Trigger.wrehoue.new_oppy: line 17, column 1: [] Trigger.wrehoue.new_oppy: line 17, column 1: [] Trigger.wrehoue.new_oppy: line 17, column 1: [] Trigger.wrehoue.new_oppy: line 17, column 1: [] Trigger.wrehoue.new_oppy: line 17, column 1: [] Trigger.wrehoue.new_oppy: line 17, column 1: [] Trigger.wrehoue.new_oppy: line 17, column 1: [] Trigger.wrehoue.new_oppy: line 17, column 1: [] Trigger.wrehoue.new_oppy: line 17, column 1: [] Trigger.wrehoue.new_oppy: line 17, column 1: []: Trigger.wrehoue.new_oppy: line 17, column 1

vamshi(Nani)vamshi(Nani)

Awesome...what is happening to my previous code...why we created a custom field...where is my code goes in to recursion..can you please explain..

sorry for eating your time...

Denis VakulishinDenis Vakulishin
It's because you're creating new sObject of the same type. So, after Opp is created trigger creates new, after insert in trigger it fires trigger again, and so on...
So you should add some check to prevent recursion. E.g. as I tell you previously the solution mught be adding new Custom Field ad fill it only in trigger or check Opp's name(edns wtih "_renewal") 

Custom field you can create via web interface.
Name check could be like 
trigger new_oppy on Opportunity (after insert)
{  
    
    if(Trigger.isInsert&&Trigger.isAfter)
    {
        List<Opportunity> newOpps = new List<Opportunity>();
        for(Opportunity opp: Triger.new){
           if(opp.Name.endsWith('_renewal')) continue;
           Opportunity newOpp=new Opportunity();
           newOpp.name = opp.Name+'_renewal';
           newOpp.closedate = date.today();
           newOpp.StageName='Qualified';//Set your initial stage name
           newOpps.add(newOpp);
        }
        insert newOpps;
    }
}


Akshay KopardeAkshay Koparde
I Have written a simple trigger to auto populate the decription feild on contact on creation of contact. Trigger is working good but my test class is failing. Can anyone please help me to find out the error.

trigger ContactDescription on Contact (before insert ,before update) {
    for(contact myContact : Trigger.new)
    {
            myContact.Description='Contact Description updated by Contact description trigger';  
        
    }
   
}

/*Test Class*/
@iSTest
public class TestContactDescription {
    static testmethod Void ContactDescriptionTest()
    {
        contact mytestContact =new contact();
        mytestContact.LastName='Wangdo';
        mytestContact.FirstName='Phunsukh';
        insert myTestContact;
       system.debug('new contact inserted '+myTestContact.FirstName+' '+MyTestContact.LastName); 
       system.assertEquals('Contact Description updated by Contact description trigger',MyTestContact.Description);
       
       system.debug('Description updated by trigger');
         
        myTestContact.Description='Not updated from the trigger';
        update myTestContact;
        system.assertNotEquals('Contact Description updated by Contact description trigger',MyTestContact.Description);
           system.debug('Description not updated by trigger'); 
       
        
        list<contact>contactList=new list<contact>();
        for(Integer i=0;i<200;i++)
        {
            contact contact_trig=new contact();
            contact_trig.LastName='Trigger ' + i;
            contactList.add(contact_trig);
        }
        insert contactList;
        system.debug('No of contacts inserted is '+contactList.size());
        
    }
        

}


Thank you!