+ Start a Discussion
Project2Project2 

APEX for junction object

Hi,

 

I have three custom objects, one of them 'B' is a junction object (Master data relationship with A and C). I want to update the status on a filed on object C; Execute Trigger when the status of object A is changed.

 

This is what i have and it has issues, can you please guide me where all i am wrong 

 

 

trigger UpdateCaseStausonobjC on objectA__c (after update) {
   List<objectA__c> caseitems =
        [SELECT ID, caselineitems__c, status__c FROM objectA__c 
            WHERE Id IN:Trigger.new
            FOR UPDATE];
    for (objectA__c op:caseitems){
            List <objectB__c> documentitems =
            [SELECT ID, name from objectB__c
                WHERE ID IN op.caseitems__C];    /pass objectA caseitmes(Master detail)   
            for (objectB__c li:documentitems){
                Document_ID_r.Reference_Key__c = op.status__c;  
/Document_Id_r is relationship between B anc C, set object C=Object A status
            }
        }
}

 

gm_sfdc_powerdegm_sfdc_powerde

To do this, you have to explicitly select rows from object C and use update command to update the values.  A query like the one below should return the target rows from target C (no need to separately query on object  B)

 

 

Select Id from objectC where Id in (Select ObjectCLookup from ObjectB where ObjectALookup = op)

 

After you do this, you can update object C using "update" command.  Hope this helps.

 

Project2Project2

Thank you

 

I get an " unexpected token: 'OP' " error.

Ritesh AswaneyRitesh Aswaney

 

trigger UpdateCaseStausonobjC on objectA__c (after update) {
   
List<Object__c> objstoUpdate = new LIst<Object__c>{};
    for (objectC__c op:  [SELECT ID, name, (Select objectALookup from objectBRelationship)  from objectC__c WHERE ID IN (Select ObjectCLookup from ObjectB__c where ObjjectALookup IN :trigger.newMap.keySet() )]   )
{
             op.Reference_Key__c = op.ObjectBRelationship.objectA__r.status__c;  
objsToUpdate.add(op);
            }
     
if(objsToUpdate != null && !objsToUpdate.isEmpty())
Database.update(objsToUpdate);
}

 

Project2Project2
Thanks.
i need to learn here.
Let's say the Master-detail realtionship field label for is LabelB2A in object B for object A. 
Similarly field LabelB2C for Object C.
so using your logic the stament should be
SELECT ID, name, (Select LabelB2A_c from LabelB2A_c__r)  from objectC__c WHERE ID IN (Select LabelB2C_c from ObjectB__c where LabelB2A_c IN :trigger.newMap.keySet() )]
Ritesh AswaneyRitesh Aswaney

It would be LabelB2A__r, you dont need the _c, unless its part of the name.

SELECT ID, name, (Select LabelB2A_c from LabelB2A__r)  from objectC__c WHERE ID IN (Select LabelB2C_c from ObjectB__c where LabelB2A_c IN :trigger.newMap.keySet() )]

 

for eg, with Account - Contacts, the query will read

Select Id, Name, (Select Id, Name from Contacts) from Account where Id IN (Select AccountId from Contact where Id in :trigger.newMap.keySet())

 

Project2Project2

I get an error "Error: Compile Error: Invalid foreign key relationship: ObjectC__c.ObjectBtoA__r at line 6 column 49"

 

for line

op.Reference_Key__c = op.ObjectBtoA__r.status_c

 

Status_c is a Picklist

Thanks

Ritesh AswaneyRitesh Aswaney

Can you post your actual code ?

Project2Project2

This is the code i currently have.

 

trigger UpdateCaseStausonDoc on Dispute_New__c (after update) {
   list <Dispute_New__c> openlineitems =
        [SELECT ID, Status__c FROM Dispute_New__c 
            WHERE Id IN:Trigger.new
            FOR UPDATE];
   For (Dispute_New__c op:openlineitems){
          list <Document_ID__c> lineitems =
                 [Select Id, Reference_Key2__c from Document_ID__c 
                    where Id IN (Select Document_ID__c from Dispute_Line_Items__c 
                    where Dispute2__c IN:Trigger.new)];
          for (Document_ID__c li:SAPlineitems){
                 li.Reference_Key2__c = op.status__c;
                 lineitems.add(li);
                        }
                    }
           }

Ritesh AswaneyRitesh Aswaney

Assuming

Dispute_New__c -----<Dispute_Line_Items__c > ----- Document_ID__c 
.
.
.
trigger UpdateCaseStausonobjC on Dispute_New__c (after update) {
   
List<Document_ID__c> objstoUpdate = new LIst<Document_ID__c>{};
for (Document_ID__c op:  [SELECT ID, name, Reference_Key2__c , (SELECT ID, Dispute_New__r.Status__c FROM  Dispute_Line_Items__c )  from Document_ID__c WHERE ID IN (Select Document_ID__c from Dispute_Line_Items__c where Dispute2__c IN :trigger.newMap.keySet() )]   )
{
             op.Reference_Key__c = op.Dispute_Line_Items__r.Dispute_New__r.status__c;  
 objsToUpdate.add(op);
            }
     
if(objsToUpdate != null && !objsToUpdate.isEmpty())
Database.update(objsToUpdate);
}
Project2Project2

I now get error message

Error: Compile Error: Didn't understand relationship 'Dispute_Line_Items__c' in FROM part of query call. 

 at line 4 column 5

Ritesh AswaneyRitesh Aswaney

It should be __r , not __c, apologies

 

for (Document_ID__c op:  [SELECT ID, name, Reference_Key2__c , (SELECT ID, Dispute_New__r.Status__c FROM  Dispute_Line_Items__R )  from Document_ID__c WHERE ID IN (Select Document_ID__c from Dispute_Line_Items__c where Dispute2__c IN :trigger.newMap.keySet() )]   )

Project2Project2

Ritesh, Not sure why your code d'nt work. Thank you you help.

 

My earlier code with a tweak (the update method tip form your code) works now. Not sure if this is most efficient way, but it works.

 

Trigger UpdateCaseStausonDoc on Dispute_New__c (after update) {
   list <Dispute_New__c> openlineitems =
        [SELECT ID, Status__c FROM Dispute_New__c 
            WHERE Id IN:Trigger.new
           ];
   For (Dispute_New__c op:openlineitems){
          list <Document_ID__c> lineitems =
                 [Select Id, Reference_Key2__c from Document_ID__c 
                    where Id IN (Select Document_ID__c from Dispute_Line_Items__c 
                    where Dispute2__c IN:Trigger.new)];
          for (Document_ID__c li:lineitems){
                 li.Reference_Key2__c = op.status__c;
                 Database.update(li);
                        }
                    }
           }