You need to sign in to do that
Don't have an account?
Project2
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
}
}
}
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)
After you do this, you can update object C using "update" command. Hope this helps.
Thank you
I get an " unexpected token: 'OP' " error.
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())
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
Can you post your actual code ?
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);
}
}
}
Assuming
I now get error message
at line 4 column 5
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() )] )
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);
}
}
}