0

I have looked upon the different question posted in the forum but I am not able to understand clearly how to put the DML outside the FOR Loop. In my scenario I have a trigger which is firing whenever the 'Status' of the 'IDEA' is update to 'Issue' . The trigger will insert a new 'CASE' record and will update the 'Idea' with its corresponding 'Case'. I am able to insert the 'Case' outside FOR loop but unable to update 'IDEA' outside the loop. I am receiving he below error when I am trying to update the Status of 'Idea'.

enter image description here

Below is my code.

Trigger

Trigger IdeaTrigger on Idea (After Insert , After Update)
{
     If(Trigger.isAfter)
     {
        If(Trigger.isUpdate)
        {
            for(Idea I : Trigger.New)
            {
                 If((I.Status__c == 'Issue') && (I.CITRACK_Created__c == False) && (I.Title != null))
                 {
                     IdeaTriggerHelper.NewCITRACK(Trigger.New); //Line 53
                 }
            }
         }    
    }
}

Trigger Class

Public Class IdeaTriggerHelper
{
    //New CITrack case to be created when a Idea status is changed to issue.
    Public Static Void NewCITRACK(List<Idea> IdeaIds)
    {

        List<Case> CaseToInsert = New List<Case>();
        Set<Id> IdeaCreatedbyId = New Set<ID>();
        Set<Id> IdeaSet = New Set<Id>();

        RecordType RT = [SELECT Description,DeveloperName,Id,IsActive FROM RecordType WHERE Name = 'CITRACK' and SObjectType = 'Case']; 

        For(Idea I : IdeaIds)
        {
            IdeaCreatedbyId.add(I.CreatedById);
            IdeaSet.add(I.ID);
        }

        List<User> UserList = [SELECT Id,Email FROM User where Id =:IdeaCreatedbyId];

        For(Idea I : IdeaIds)
        {
            For(User U : UserList)
            {
                IF(I.CreatedbyId == U.Id)
                {
                    Case C = New Case();

                    c.Detailed_Subject__c = I.Title;
                    c.Subject = I.Title;
                    c.Description_Rich__c = I.Body;
                    c.Origin = 'Technical Discussion';
                    c.Type = I.type__c;
                    c.Related_Opportunity__c = I.Related_Opportunity__c;
                    c.RecordTypeId = RT.Id;
                    c.Status = 'Active';
                    c.Related_Idea__c = I.Id;
                    c.Idea_Case_Owner__c = U.email;
                    CaseToInsert.add(C);
                }
            }
        }
        Insert CaseToInsert;

        List<Case> Caselist = [Select Id,Related_Idea__c from Case where Related_Idea__c =: IdeaIds];
        List<Idea> IdeaList = [Select Id,CITRACK_Created__c,Related_CITrack_Case__c from Idea where Id =: Ideaset];
        List<Idea> IdeaToUpdate = New List<Idea>();

        For(Idea I : IdeaList)
        {
            For(Case C : CaseList)
            {
                IF(I.Id == C.Related_Idea__c)
                {
                    Idea Ide = New Idea();

                    Ide.CITRACK_Created__c = True;
                    Ide.Related_CITrack_Case__c = C.Id;
                    IdeaToUpdate.add(Ide);
                }
            }
            //Update I; //If I update here the it works fine
        }
        Update IdeaToUpdate;   //Line 432
    }
}

Please let me know where I am going wrong.

Thanks!

1
  • Just a query, why are you passing trigger.new again and again inside the for loop which iterate on trigger.new itself in line inside the trigger. Shouldn't passing "I" which is the instance of the current Idea record in trigger.new enough?
    – Prajith
    Feb 24 '16 at 11:29
2

Take a global list and add all your Idea records which are satisfy your conditions.. and then call your method and pass the list

Trigger IdeaTrigger on Idea (After Insert , After Update)
{
    List<Idea> lstIdea = new List<Idea>();
     If(Trigger.isAfter)
     {
        If(Trigger.isUpdate)
        {
            for(Idea I : Trigger.New)
            {
                 If((I.Status__c == 'Issue') && (I.CITRACK_Created__c == False) && 
                           (I.Title != null))
                 {
                    lstIdea.add(I);//Collect all the Idea in list

                 }
            }
             IdeaTriggerHelper.NewCITRACK(lstIdea);
         }    
    }
}

While looking into your issue. You are not specifying the Id while updating Idea.

For(Idea I : IdeaList)
{
    For(Case C : CaseList)
    {
        IF(I.Id == C.Related_Idea__c)
        {
            Idea Ide = New Idea();
            Ide.id = I.Id; //Add Id
            Ide.CITRACK_Created__c = True;
            Ide.Related_CITrack_Case__c = C.Id;
            IdeaToUpdate.add(Ide);
        }
    }
}
Update IdeaToUpdate;   //Line 432
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.