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
Hector DiazHector Diaz 

TRIGGER doesn´t work "update field"

Hi Guys, i need your help, i am new in apex code.
this is the case:
I have 2 Objects
  • Custom Object, called "QA_Order_Control__c"
  • Opportunity Object
I have 2 picklist fields:
  • "Reject_team__c" inside Custom Object
  • "StageName" inside Opportunity
the logic is:
WHEN "Reject_team__c" = "sss" on Custom Object
UPDATE field "StageName" ="10. Rejected by Operations SSS" on Opportunity.




trigger QARejectUpdatesOppStage1 on QA_Order_Control__c(before insert,before update){

List<Id> conIds = new List<Id>();
    List<Opportunity> newAssetlist =  new List<Opportunity>();
   
    for(QA_Order_Control__c cnt:Trigger.new){
    if(cnt.Reject_team__c=='SSS'){
        conIds.add(cnt.Id);
    }
    }
   
List<Opportunity> childAssets = [Select QA_Order_Control__c, StageName FROM Opportunity WHERE QA_Order_Control__c IN:conIds];

for(Opportunity a : childAssets){
        a.StageName='10. Rejected by Operations SSS';
        newAssetlist.add(a);
    }
  
   if(childAssets.size() > 0)
           
    {
   
    try
         {
            update newAssetlist;
         }
             catch (System.Dmlexception e) 
         {
         system.debug (e);
         }
    }   
   
   
}










Ramu_SFDCRamu_SFDC
Hi , the code might be throwing dml exception as you are checking for the list size of 'childassets' instead of newassetlist. Change the below line  from if(childAssets.size() > 0)    to  if(newAssetlist.size() > 0)

Hope this helps !!
Hector DiazHector Diaz
Hi Ramu

It doesn´t work, what can I do?

Best,
Ramu_SFDCRamu_SFDC
As per your code my understanding is 1. QA_Order_Control__c is the parent of Opportunity
2. When Reject_team__c is set to 'sss' on QA_Order_Control__c all related child opportunities stages need to be set to '10. Rejected by Operations SSS'

As the change on QA object is making the change on Opportunity object you can change the trigger event to After Insert, After Update.

When you say that it is not working, is it throwing any error? please elaborate.
Hector DiazHector Diaz
tks, for you answer Ramu.
1) The relationship between Opportunity and QA_Order_Control__c it´s a "Lookup" in both objects.

2) When Reject_team__c is set to 'sss' on QA_Order_Control__c just one related Opportunity need to be set in stage '10. Rejected by Operations SSS'
i.e one Opportunity record  just have one related QA_Order_Control and viceversa one QA_Order_Control record just have one related Opportunity record.

and when i saved the trigger, not throw any error, it´s clean of any errors.

best,

User-added image
Ramu_SFDCRamu_SFDC
Hi Hector, as per the above diagram I understand that QA_Ordre_Control is a parent object for Opportunity in once way and vice versa in another relation.

So the above code works when the Parent QA_Order_coontrol record is set to 'SSS' by applying the child records field value to 10. Rejected by Operations SSS on the related Opportunity records.


Hector DiazHector Diaz
Hi Ramu
it's a Lookup relationship, no Master-detail or Parent-Child relationship.please see the example.
best,



User-added image
Ramu_SFDCRamu_SFDC
Hi Hector, I understand that it is a lookup relationship. I used the termonology 'Parent-Child' just to understand what object is referring to which object.

In the current relationship, there are two different scenarios

1) QA_Order_Control - Parent & Opportunity - Child  
2) Opportunity - Parent & QA_Order_Control - Child

The initial code you wrote works for the first relationship

To make the code work for the second relationship please try out the below code

trigger QARejectUpdatesOppStage1 on QA_Order_Control__c(before insert,before update){

List<Id> conIds = new List<Id>();
    List<Opportunity> newAssetlist =  new List<Opportunity>();
  
    for(QA_Order_Control__c cnt:Trigger.new){
  if(cnt.Reject_team__c=='SSS'){
   conIds.add(cnt.Opportunity__c);
  }
    }
if(conids.size()>0){ 
  List<Opportunity> parentAssets = [Select QA_Order_Control__c, StageName FROM Opportunity WHERE id IN:conIds];

  for(Opportunity a : parentAssets){
   a.StageName='10. Rejected by Operations SSS';
   newAssetlist.add(a);
  }

  if(newAssetlist.size() > 0){  
   try{
    update newAssetlist;
   }
   catch (System.Dmlexception e){
     system.debug (e);
   }
  }

}


Hector DiazHector Diaz
Hi Ramu
tks for your answer, your code works excellent.
let me try with the "test class" 

best,
Hector DiazHector Diaz

Hi Ramu
my test class, fail :(
this is the message:
"Error Message System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []"
can you help me, please



@isTest
private class AfterQAOCUpdateTest {

@isTest static void test_method_one() {
        QA_Order_Control__c q = new QA_Order_Control__c();
        update q;
       
        Opportunity o = new Opportunity();
        o.StageName='10. Rejected by Operations SSS';
        insert o;
       
        Id QAOCId = [SELECT Id, Name FROM QA_Order_Control__c WHERE Id=:d.Id].Name
       
        test.startTest();

        update o;
       
        Opportunity updatedOpp = [SELECT Id, StageName FROM Opportunity WHERE Id=:s.Id];
       
        test.stopTest();
        }
            }