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
Jerry ClifftJerry Clifft 

Slight tweak needed in trigger for update

Ok, I think this should be an easy one, but I am overlooking something I fear. This trigger should copy a string of data into a field on the Opportunity from a custom object called equipment__c. When I run the debug, everything looks fine, until the last line, where the data is written tot he Opp. For some reason, the Opp is not being updated.

trigger xEquipment_HiddenFieldOpportunity on Equipment__c (After Insert, After Update) {  

    Set<Id> oppId=new Set<Id>();
    Set<Id> eqId=new Set<Id>();
    for(equipment__c CA1:Trigger.New)    {
        oppId.add(CA1.Opportunity__c);
        eqId.add(CA1.Id);

        Map<Id,Opportunity> opptyMap=new  Map<Id,Opportunity> ([select Id,  Hidden_Bulk_Equipment__c from Opportunity where Id in :oppId ]);
            System.debug('Map Opportunity Size '+opptymap.size());
            System.debug('=== contents of opptymap: ' +opptymap);
           
        Map<Id,Equipment__c> equipMap=new  Map<Id,Equipment__c> ([select Id, Name, Statis__c, CaseID__c from Equipment__c where Opportunity__c in :oppId]);
            System.debug('Map Equipment Size '+equipmap.size());
            System.debug('=== contents of equipmap: ' +equipmap);
           
       if(opptyMap.size() > 0 && equipMap.size() > 0)    {
      
           list<Opportunity> opeq = [select id, Hidden_Bulk_Equipment__c from Opportunity where Id in :oppId];
               System.debug('List Opportunity Size '+opeq.size());
               System.debug('=== contents of Opp List: ' +opeq);
                     
           list<equipment__c> eq = [select id, Name, statis__c, Opportunity__c, lastModifiedDate, LastModifiedBy.Name from equipment__c where Opportunity__c in :oppId AND (statis__c = 'Activation Requested' OR statis__c = 'Drop Requested' )];    {
               System.debug('List Equipment Size '+opeq.size());
               System.debug('=== contents of Eq List: ' +eq);
              
               if(opptyMap.size() > 0 && equipMap.size() > 0)    {
                   List<String> NewList= new List<String>();
                   for (equipment__c eq2: eq )    {
                       NewList.add(eq2.Statis__c);
                       NewList.add(' - ');
                       NewList.add(eq2.Name);
                       NewList.add(' - ');
                       NewList.add(eq2.LastModifiedBy.Name);
                       NewList.add(' on ');
                       String str1 = '' + eq2.lastModifiedDate;
                       NewList.add(str1);
                       NewList.add(' GMT 0');
                       NewList.add(' <br />');
                       System.debug('=== contents of NewList: ' +NewList);
                                                }
                   String s = '';

                   for(String c : NewList)    {
                       s = s + c;
                       system.debug('This data should go into the Opp' +  c);
                                              }
                   opeq[0].Hidden_Bulk_Equipment__c = s;

                }
            }
        }
    }
}
Ramu_SFDCRamu_SFDC
Hi, as this is after insert, after update trigger. It shoud end with an update statement something like 'UPDATE Opeq;' to update the changes.

Chidambar ReddyChidambar Reddy
Hi Jerry,

The following trigger will work, check the bold text, you can comment the cleared text.


trigger xEquipment_HiddenFieldOpportunity on Equipment__c (After Insert, After Update) { 

    Set<Id> oppId=new Set<Id>();
    Set<Id> eqId=new Set<Id>();
    for(equipment__c CA1:Trigger.New)    {
        oppId.add(CA1.Opportunity__c);
        eqId.add(CA1.Id);
}

        Map<Id,Opportunity> opptyMap=new  Map<Id,Opportunity> ([select Id,  Hidden_Bulk_Equipment__c from Opportunity where Id in :oppId ]);
            System.debug('Map Opportunity Size '+opptymap.size());
            System.debug('=== contents of opptymap: ' +opptymap);
          
        Map<Id,Equipment__c> equipMap=new  Map<Id,Equipment__c> ([select Id, Name, Statis__c, CaseID__c from Equipment__c where Opportunity__c in :oppId]);
            System.debug('Map Equipment Size '+equipmap.size());
            System.debug('=== contents of equipmap: ' +equipmap);
          

      /* if(opptyMap.size() > 0 && equipMap.size() > 0)    {
     
           list<Opportunity> opeq = [select id, Hidden_Bulk_Equipment__c from Opportunity where Id in :oppId];
               System.debug('List Opportunity Size '+opeq.size());
               System.debug('=== contents of Opp List: ' +opeq);
                    
           list<equipment__c> eq = [select id, Name, statis__c, Opportunity__c, lastModifiedDate, LastModifiedBy.Name from equipment__c where Opportunity__c in :oppId AND (statis__c = 'Activation Requested' OR statis__c = 'Drop Requested' )];    {
               System.debug('List Equipment Size '+opeq.size());
               System.debug('=== contents of Eq List: ' +eq);*/

 if(oppId.size()>0){            


List<Opportunity> oppupdatelist = new List<Opportunity>();
List<Opportunity> oppswitheqs = [select Id,  Hidden_Bulk_Equipment__c,(select id, Name, statis__c, Opportunity__c, lastModifiedDate, LastModifiedBy.Name from equipment__r where (statis__c = 'Activation Requested' OR statis__c = 'Drop Requested' )) from Opportunity where Id in :oppId ];

       //if above query does not work
    /*List<Opportunity> oppswitheqs = [select Id,  Hidden_Bulk_Equipment__c,(select id, Name, statis__c, Opportunity__c, lastModifiedDate, LastModifiedBy.Name from equipment__r ) from Opportunity where Id in :oppId AND (equipment__r.statis__c =: 'Activation Requested' OR equipment__r.statis__c =: 'Drop Requested' ) ] */

}
                  
for(Opportunity Opp :oppswitheqs){
             List<String> NewList= new List<String>();
                   for (equipment__c eq2: Opp.equipment__r )    {
                       NewList.add(eq2.Statis__c);
                       NewList.add(' - ');
                       NewList.add(eq2.Name);
                       NewList.add(' - ');
                       NewList.add(eq2.LastModifiedBy.Name);
                       NewList.add(' on ');
                       String str1 = '' + eq2.lastModifiedDate;
                       NewList.add(str1);
                       NewList.add(' GMT 0');
                       NewList.add(' <br />');
                       System.debug('=== contents of NewList: ' +NewList);
                   }
                  
                  String s = '';

                   for(String c : NewList)    {
                       s = s + c;
                       system.debug('This data should go into the Opp' +  c);
                     }
                   Opp.Hidden_Bulk_Equipment__c = s;
oppupdatelist.add(Opp);
                }
if(oppupdatelist.size()>0){
        update oppupdatelist;
}


}

-Thank you
Jerry ClifftJerry Clifft
That does indeed work, it now writies to the opportunity properly. However, I am now faced with a new issue:
System.LimitException: Too many SOQL queries: 101

Any ideas/solutions?

Here is a copy of the current working trigger, that does fine for 1 to 50 inserts at once. Above that 50 lines, I get the error.

trigger xEquipment_HiddenFieldOpportunity on Equipment__c (After Insert, After Update) {  

    Set<Id> oppId=new Set<Id>();
    Set<Id> eqId=new Set<Id>();
    for(equipment__c CA1:Trigger.New)    {
        oppId.add(CA1.Opportunity__c);
        eqId.add(CA1.Id);
        if(oppId.size()>0){           


List<Opportunity> oppupdatelist = new List<Opportunity>();
System.debug('=== contents of List Opp: ' +oppupdatelist );


List<Opportunity> oppswitheqs = [select Id, (select id, Name, statis__c, Opportunity__c, lastModifiedDate, LastModifiedBy.Name from equipment__r where (statis__c = 'Activation Requested' OR statis__c = 'Drop Requested' )) from Opportunity where Id in :oppId ];
System.debug('=== contents of List Opp with EQ: ' +oppswitheqs );

                 
for(Opportunity Opp :oppswitheqs){
             List<String> NewList= new List<String>();
                   for (equipment__c eq2: Opp.equipment__r )    {
                       NewList.add(eq2.Statis__c);
                       NewList.add(' - ');
                       NewList.add(eq2.Name);
                       NewList.add(' - ');
                       NewList.add(eq2.LastModifiedBy.Name);
                       NewList.add(' on ');
                       String str1 = '' + eq2.lastModifiedDate;
                       NewList.add(str1);
                       NewList.add(' GMT 0');
                       NewList.add(' <br />');
                       System.debug('=== contents of NewList: ' +NewList);
                   }
                 
                  String s = '';

                   for(String c : NewList)    {
                       s = s + c;
                       system.debug('This data should go into the Opp' +  c);
                     }
                   Opp.Hidden_Bulk_Equipment__c = s;


oppupdatelist.add(Opp);
                }
if(oppupdatelist.size()>0){
Database.update(oppswitheqs);

}
}

}
}
Ramu_SFDCRamu_SFDC
The error is due to the usage of SOQL query within FOR loop. Try using MAPS to create a dataset of id's and Opportunities and make use of the map in the concluding for loop.
Chidambar ReddyChidambar Reddy
Hi Jerry, 

I think you have not observed my code correctly. I ended the first for loop like below

for(equipment__c CA1:Trigger.New)    {
        oppId.add(CA1.Opportunity__c);
        eqId.add(CA1.Id);
}

But you forgot to end that loop, that caused the error.

You can copy the below code.


trigger xEquipment_HiddenFieldOpportunity on Equipment__c (After Insert, After Update) { 

    Set<Id> oppId=new Set<Id>();
    Set<Id> eqId=new Set<Id>();
    for(equipment__c CA1:Trigger.New)    {
        oppId.add(CA1.Opportunity__c);
        eqId.add(CA1.Id);
}//End of for loop



        if(oppId.size()>0){          

List<Opportunity> oppupdatelist = new List<Opportunity>();
System.debug('=== contents of List Opp: ' +oppupdatelist );


List<Opportunity> oppswitheqs = [select Id, (select id, Name, statis__c, Opportunity__c, lastModifiedDate, LastModifiedBy.Name from equipment__r where (statis__c = 'Activation Requested' OR statis__c = 'Drop Requested' )) from Opportunity where Id in :oppId ];
System.debug('=== contents of List Opp with EQ: ' +oppswitheqs );

                
for(Opportunity Opp :oppswitheqs){
            
                              List<String> NewList= new List<String>();

                   for (equipment__c eq2: Opp.equipment__r )    {
                       NewList.add(eq2.Statis__c);
                       NewList.add(' - ');
                       NewList.add(eq2.Name);
                       NewList.add(' - ');
                       NewList.add(eq2.LastModifiedBy.Name);
                       NewList.add(' on ');
                       String str1 = '' + eq2.lastModifiedDate;
                       NewList.add(str1);
                       NewList.add(' GMT 0');
                       NewList.add(' <br />');
                       System.debug('=== contents of NewList: ' +NewList);
                   }
                
                  String s = '';
                   for(String c : NewList)    {
                       s = s + c;
                       system.debug('This data should go into the Opp' +  c);
                     }
                     
                    Opp.Hidden_Bulk_Equipment__c = s;


                                oppupdatelist.add(Opp);


                }//End of for(Opportunity Opp :oppswitheqs)
           

            if(oppupdatelist.size()>0){
                               Database.update(oppswitheqs);

                           }


}//End of if(oppId.size()>0)

}


-Thank you,
You can choose this as best answer, if it resolves your issue.