+ Start a Discussion

Update joined objects

I have custom objects (called CProducts__c)  which are linked to Opportunities via a reference field in CProducts__c called Opportunity__c.

Opportunities as well as CProducts__c have custom fields, both called DOCNO__c.

Now, the DOCNOs in CProducts__c are out of synch with the DOCNOs in Opportunities which they should not.

I need a query to write the DOCNOs from Opportunities in the dependent CProducts__c. I would like to have a update query which I could execute in the SF IDE or so.

Any hint, how to do that?

Thanks for your help.
Why not just a cross object formula field?
Thank you TheNrd,

could be a solution, but I am not allowed to alter the fields for my customer (he is like that). And it's a one-shot thing.

So, what would be the equivalent for this SQL statement? Maybe just from the SFDC IDE?

update CProducts__c c left join Opportunities o on c.Opportunity__c = o.Id set c.DOCNO__c = o.DOCNO__c

Just as simple as that?
Here is the code but I would highly highly recommend using a cross object formula field. There is really no reason to use Apex if something can be done with out of the box functionality.

Set<Id> oppIds = new Set<Id>();
Map<Id,String> oppMap = new Map<Id,String>()
List<CProducts__c> CPsToUpdate = new List<CProducts__c>();

for(Opportunity opp : trigger.new){
 oppMap.put(opp.Id, opp.DOCNO__c);

for(CProducts__c cp : [select Opportunity__c, DOCNO__c from CProducts__c where Opportunity__c IN :oppIds]){
 cp.DOCNO__c = oppMap.get(cp.Opportunity__c);

if(CPsToUpdate.size() > 0){
 update CPsToUpdate;


A Dev in NZA Dev in NZ
I am trying to do a similar thing.
Can you please help.
1.   Reason for me doing this is I have reached the limit of 5 cross objects within formulas. SFDC support suggest I use S-Control.
2.   For example I wish to retrieve fields within an "Account" that are related by a "Account Name" lookup to a "Contract", both standard objects. And then place those retrieved fields into my "Contract".
3.   I am current trying to do this with an S-Control that executes when I hit the "EDIT" button on any "Contract Detail page".
4.   I am also trying to bring through multiple "Contact" fields, "Opportunity" fields, and "User" fields. Hence reaching my limit.
5.   I would love to be update these dependant Contract Fields on selecting the lookup parent field, but I am happy with executing on "EDIT"
Q1:   Where would the code you gave to "kkaal" above be entered into SFDC?
Q2:   Should I be doing a similar thing?
Q3: This is the type of code I am trying but it fails within S-Control
result = sforce.connection.retrieve("Name,Phone", "Account", [result[0].id]);
  result = sforce.connection.update([contract]);

  if (result[0].getBoolean("success")) {
    log("account with id " + result[0].id + " updated");
  } else {
    log("failed to update account " + result[0]);