You need to sign in to do that
Don't have an account?
Hector 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
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);
}
}
}
this is the case:
I have 2 Objects
- Custom Object, called "QA_Order_Control__c"
- Opportunity Object
- "Reject_team__c" inside Custom Object
- "StageName" inside Opportunity
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);
}
}
}
Hope this helps !!
It doesn´t work, what can I do?
Best,
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.
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,
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.
it's a Lookup relationship, no Master-detail or Parent-Child relationship.please see the example.
best,
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);
}
}
}
}
tks for your answer, your code works excellent.
let me try with the "test class"
best,
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();
}
}