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
venkateshvenkatesh 

Associating one object with another object using Master-detail lookup

Hi,

 

I will have to write the Apex class for associating master object with the chile object. How I can achieve this scenario.

 

Please help me........

 

Thanks in advance.

 

 :smileymad: :smileymad:

Message Edited by venkatesh on 03-06-2009 06:43 AM
WhyserWhyser

// Obtain the child object you wish to update
ChildObject__c childObject = [select fields from ChildObject__c where Id = childObjectId];

 

// Update the childobject with the Masterobject id

childObject.MasterObject__c = MasterObjectId;
update childObject;

 

I know this code is extremely general, but we don't have enough information to answer your question properly. We need to know the use case, how it will be called, when will this be used, etc?

venkateshvenkatesh

Hi Whyser,

 

Thanks for your quick reply. You are correct. I did not provide the details. Below I have mentioned my scenario to achieve the child object update.

 1) After calling Apex Web service, data will be coming from external service. These data may be related to Accounts, Sales Order and Invoice related details.

2)  I have the code for updating the Accounts object. Now I need to update the child objects such as Sales Order and Invoice objects related to perticular Accounts records using Master-detail relashionship.

3) For master-detail relationship, I have created one junction object using Salesforce UI setup and related the master object with the child object.

4)My requirement now is to update the child object related to master object as said like above. I don't have planning to update the master fields in child object as per your above suggestion. I should have to directly update the WebService data to the perticular child objects which are related to master object while saving the data to Master object.

 

Please!!!! Please!!!! suggest/help me to fulfill the same.

 

 Thanks in advance.

 

:smileymad: :smileymad:

 

Please help me, if anyone knows this issue..................

Message Edited by venkatesh on 03-08-2009 09:49 PM
Message Edited by venkatesh on 03-09-2009 01:53 AM
Message Edited by venkatesh on 03-09-2009 06:20 AM
venkateshvenkatesh

Hi Anybody,

 

    I did not get any answer for the above posted scenario. I will be looking for any idea or sample code to fulfill the same. I am  in hurdle to overcome the scenario. I am ready to provide more details, If anyone wants more details.

Please suggest me any one.........!!!!!

 :smileymad:  :smileymad:  :smileymad:

WhyserWhyser
so your issue isn't really about associating an object with another using master-detail, it's about updating a child object after updating the master one.

I'm still not sure of the specifics of what you need, I'll probably need to see what you have so far, but I'll take a stab at the dark here...

First you'll want to query for the child object or object(s) that you want to update. Since you already know the Account object, and the Account object is the master, then you will use the Account Id to retrieve the child objects like so:

// Let's say that your Account object variable was called acct: List< childObject__c > childObjectList = [select fields from childObject__c where Account__c = :acct.Id]; // Now childObjectList will contain all the objects that you're interested in updating. Iterate through the list and perform the updates you want for ( childObject__c childObject : childObjectList ) { // Change whatever fields you want childObject.customField1__c = value; childObject.customField2__c = value; } //Now update the childObjectList that you just made changes to update childObjectList;

 

I hope that answers your question?
venkateshvenkatesh

Hi Whyser,

 

I did not got your above logic. As per my knowledge, this simple update procedure logic is not working. I have previously tried this type of coding.

 

I have the Apex Web Service calling class to access the Account(Parent Object) records. In Web service calling class, I will be calling different class to update the Account object.  Finally I will call this web service class in one of the object's trigger.

 

Now I wanted to update the child object(Costom Object) which is related to parent object while inserting the parent object through Web Service call. For this, I have to write seperate Apex class to update the child object which is related to parent.

 

I made the relation between two objects using master detail relationship(creating another junction object) in my Salesforce Account object UI page. So when I update the child object, those records should display in parent object as a seperate block.

 

 

Thanks for your reply. Please give any kind of solution. If you want, I will give you the code which I have written.

 

Thanks in advance.

:smileymad: :smileymad:

 

 

Message Edited by venkatesh on 03-11-2009 07:37 AM
WhyserWhyser

I'm not sure why the sample code above would not work for what you need.

My code does exactly what you're asking for, to update child object that is related to a parent object.

 

I didn't enclose my code in a method or class or trigger or whatnot, because I assumed you know how to do this yourself.

And I did not show how you pass in the Account object's ID to query and retrieve the child objects because I also assume that you know what it is.

 

What do you not understand in my sample code? Maybe you should show us what you have.

 

 

venkateshvenkatesh

Hi Whyser,

 

 I have written new trigger for that to achieve. But I will not be able to achieve expected result. Please look into the code. If I missed anything please let me know.

 

trigger UpdateSalesOrderfromAccount on Account (after insert) {

for (Account acc : Trigger.New) {
WebServiceCall.callservice(acc.Id, acc.Name);
}

//before update
List<Id> idsToQuery = new List<Id>{};
for(Account acc : Trigger.new) {
idsToQuery.add(acc.id);
}
//Now query all related Sales Order
Sales_Order__c[] sales = [select id, Name, DESCRIPTION__c from Sales_Order__c where id IN :idsToQuery];

//Loop through Sales Order
for(Sales_Order__c s: sales){

//Using the trigger.newMap, get the related Sales Oredr record to the specific Account record
Account a = Trigger.newMap.get(s.Id);

//Set the Last_Case__c field here....
s.DESCRIPTION__c = a.For_Testing__c;
}


//Update all records in a single statement
update sales;

}

 

 

Also suggest me how I can convert this trigger to Apex method. Because I am planning to call this method when inserting Web service records to an object. If I write the Apex method, I don't know how I can  pass in the newly created Account object's ID to Sales Order query as I can pass the new Id in trigger when trigger takes place.

 

Please do the needful. This is the very important requirement for me. I hope you can resolve my problem.

 

Waiting for your kind suggestion.

 

Thanks a lot in advance.

 

:smileymad::smileysad: 

Message Edited by venkatesh on 03-12-2009 07:42 AM
WhyserWhyser

You look like you're pretty close, I would make these changes.

The only other problem I see is how you declared your trigger.

 

You should be expanding it to have it run on more than just after insert. Looking at your code, you cannot have any Sales_Order__c objects associated to an Account if it's newly created, you probably want to run the second half of the code if your account is doing an update.

 


venkatesh wrote:

Hi Whyser,

 

 I have written new trigger for that to achieve. But I will not be able to achieve expected result. Please look into the code. If I missed anything please let me know.

 

trigger UpdateSalesOrderfromAccount on Account (after insert, after update) {

for (Account acc : Trigger.New) {
WebServiceCall.callservice(acc.Id, acc.Name);
}

// Don't need to store your AccountIds in an array, Trigger.newMap.keySet() contains all the AccountIds
if ( Trigger.isUpdate ) {
//Now query all related Sales Order
Sales_Order__c[] sales = [select id, Name, DESCRIPTION__c from Sales_Order__c where id IN :Trigger.newMap.keySet()];

//Loop through Sales Order
for(Sales_Order__c s: sales){

//Using the trigger.newMap, get the related Sales Oredr record to the specific Account record
// You need to pass in the AccountID into the get call rather than the Sales_Order__c's ID
Account a = Trigger.newMap.get(s.Account__c);

//Set the Last_Case__c field here....
s.DESCRIPTION__c = a.For_Testing__c;
}


//Update all records in a single statement
update sales;
}
}

 

 

Also suggest me how I can convert this trigger to Apex method. Because I am planning to call this method when inserting Web service records to an object. If I write the Apex method, I don't know how I can  pass in the newly created Account object's ID to Sales Order query as I can pass the new Id in trigger when trigger takes place.

 

Please do the needful. This is the very important requirement for me. I hope you can resolve my problem.

 

Waiting for your kind suggestion.

 

Thanks a lot in advance.

 

:smileymad::smileysad: 

Message Edited by venkatesh on 03-12-2009 07:42 AM

 

venkateshvenkatesh

HI,

 

      Yes, you are right. I am putting my webservice call in scheduler and then I will have to run only second part part of the above code. But I am getting the error.... i.e for the below line:

 

// You need to pass in the AccountID into the get call rather than the Sales_Order__c's ID
Account a = Trigger.newMap.get(s.Account__c);

I am getting this  Error: Compile Error: Invalid field Account__c for SObject Sales_Order__c at line

  

 

 But as per your suggestion, How I can put the AccoutId into get call rather than Sales_Order__c's Id? Here Account is default object. I will not be able to run the code even if I mention s.Account or s.Account__r.Name or s.Account.Name also. If I put the s.Name related to Account in that place, code is working fine. But I am not getting any expected result.

 

Could I know whether this code(above) will be able to create the Child object related to Parent when I upsert the web service data to parent object.

 

I also tried below method to update the child record related to parent. But still I am unable to get the expected result. My expected results are:

1) Code has to create a new Sales Order (child) record.

2) This newly created child record has to associate/update in parent record.

3) When I click on the single Account(parent) record in the Account list view, detail view should contain the child records which are related to parent and should display under seperate block(When I click the Id/Name of this child record, the link shoud navigate to perticular child record view page).

 

trigger UpdateSalesOrderfromAccount on Account (before update) {

// The map allows us to keep track of the accounts that have
// new addresses

Map<Id, Account> acctsUpdate = new Map<Id, Account>();

// Trigger.new is a list of the Accounts that will be updated
// This loop iterates over the list, and adds any that have new
// addresses to the acctsUpdate map.

for (Integer i = 0; i < Trigger.new.size(); i++) {
if (Trigger.old[i].Description != Trigger.new[i].Description) {
acctsUpdate.put(Trigger.old[i].id, Trigger.new[i]);
}
}

List<Sales_Order__c> updatedSales = new List<Sales_Order__c>();

//Here we can see two syntatic features of Apex:
// 1) iterating over an embedded SOQL query
// 2) binding an array directly to a SOQL query with 'in'

for (Sales_Order__c s : [SELECT id, DESCRIPTION__c FROM Sales_Order__c WHERE id IN :acctsUpdate.keySet()]) {

Account parentAccount = acctsUpdate.get(s.id);
s.DESCRIPTION__c = parentAccount.Description;

// Rather than insert the Sales_Order__c individually, add the
// Sales_Order__c to a list and bulk insert it. This makes the
// trigger run faster and allows us to avoid hitting the
// governor limit on DML statements

updatedSales.add(s);
}

update updatedSales;

}

 

 

Please let me know your suggestions.

 

Thanks for your all comments.

 

:smileymad: :smileymad: :smileysad::smileysad:

Message Edited by venkatesh on 03-12-2009 10:28 PM
Message Edited by venkatesh on 03-13-2009 05:18 AM
Message Edited by venkatesh on 03-13-2009 05:22 AM
Message Edited by venkatesh on 03-13-2009 05:29 AM
WhyserWhyser

The reason why you're getting the error is because I have no idea how you set up your Sales_Order__c object.

You can create your master-detail relationship, but it's up to you to determine what the name of that field is going to be called, I justed used Account__c as an example since that is what I normally use when I make this kind of relationship in my custom objects.

 

You have to go into your own Sales_Order__c object and determine what the API field name is for your master-detail relationship with Account. It doesn't make sense to just copy my code and expect some sort of miracle since I don't know how you set everytihng up.

 

Now you're confusing me even more. Your requirements are not very clear. I thought originally you wanted to associate the Sales_Order__c to the Account, and then I thought you wanted to update the Sales_Order__c that is ALREADY related to the Account, and now I'm reading that you want the code to CREATE the Sales_Order__c objects so that they relate to the Account. Which is it? Am I associating? creating? updating? or creating and associating?

 

If we're creating the Sales_Order__c objects and THEN associating them to the Account, then we're doing this COMPLETELY wrong.

 

You need to do a couple of things before we can continue:

1) Tell me exactly what you want to happen as per my questions above. Creating and Associating? Or just associating? Or updating existing Sales_Order__c objects?

2) You need to show us what your Sales_Order__c object looks like so that we can see how it can be linked to an Account. We need to see what the name of the field is used to create that master detail relationship.

venkateshvenkatesh

Hi,

 

      As per your question, first I will have to asscociate two objects and then I will have to update/insert master related child records to child object which are coming from Web Service call.  Here I will explain about my work related to master-detail relationship with the Salesforce screenshots and what I did upto this. Please have a look into the details. Then you will be able to realize my problem.

1) I have created new Sales_Order__c(custom object).

2) This object contains Name field as the required filed(ofcourse this field is Auto Number).

3) I have created new custom object(called AccountOrderAssociation__c as the API name) to relate the Account and Sales Oreder objects. This object contains two master-detail relationship custom fields. Here I have ralated one custom field with Account(field API name is Accounts__c)and another with Sales Order(field API name is SalesOrder__c).

4) After above setup, I went to page layout setup of the Account object. Because my intention is to associate the Sales Order(child object) with Account(parent object) as like in link1 and link2.

5) I have selected Sales Order fields such as Name(Required field for Sales Order), Document Number(custom field for Sales Order) from the Related List Properties-Sales Order Association popup window.This popup is available when we click Properties icon in the Sales Order associated block in Account page layout.

When I go to view in-detail of Account records in Account object, these Sales Order field details are displaying in seperate block.

6) This will finish up my requirement of relating between two default and custom objects.

7) Now my requirement is to code level inserting/updating/associating the records to Account and Sales Order objects.

8) To achieve the above scenario at present, I made web service call to external database. I am calling this web service Apex class within trigger of my sheduler(This is one of the seperate custom object) from which I can schedule to update the Account records at the specified intervals.

9) Now I can successfully insert the records to Account object using Apex upsert call.

10) As same like Account object, I will be getting records for Sales Order custom object related to Account records also(through web service call) from my external database.

11)  I should have to insert/update(upsert API call) these Sales Order records to Sales Order object.

12) At present situation I don't have Apex class to call the sales order records from my databse through web service call.

13) So I tried for, when account records are  upserted to Account object through web service call, it should create new Sales Order record or upsert to the same record in my Sales_Order__c object by taking one of the field values from the Account record. As same as below statement written in bold text:

 

//Loop through Sales Order
for(Sales_Order__c s: sales){

//Using the trigger.newMap, get the related Sales Oredr record to the specific Account record
// You need to pass in the AccountID into the get call rather than the Sales_Order__c's ID
Account a = Trigger.newMap.get(s.Name);

//Set the Sales Order related to Account field here....
s.DESCRIPTION__c = a.For_Testing__c;
}

14) To achieve above scenario, I have written the trigger for Account object. As per my knowledge, when Account trigger occures(from web service call), these update/insert or upsert to Sales Order whatever operation should happen.

This much informations I have with me. If you want to know more details, please let me know. I can't attach the screenshot to this forum message. As per my knowledge from this much informations, you will be able to provide good suggestion. No where I copied and pasted your code. I guess and tried all the possiblities from your suggestion.

 

Thanks for your kind reply, help and interest in problem solving.

 

Please help me!!! Waiting for your reply.............

 

I don't have other way to achieve this requirement. Please help me.... :smileymad::smileysad:


I am waiting for any kind of suggestion from the forum. Please help me to achieve the above requirement........

I hope for good suggestion from any one of you from Forum. Please help me!!!!!!!!!!!!!!
Message Edited by venkatesh on 03-17-2009 06:42 AM
Message Edited by venkatesh on 03-17-2009 08:56 PM
Message Edited by venkatesh on 03-18-2009 03:57 AM
Message Edited by venkatesh on 03-18-2009 04:45 AM
venkateshvenkatesh

Please reply for the above said scenario, if anyone knows Salesforce Apex programming. Unfortunately I did not  achive the association between the parent and child object.

 

Please help me!!!!!!!!!

 

Thanks in advance to every one.

merumeru

Hi venkatesh,

 

Were you able to achieve this association between child and parent record.Please reply as I am facing the same problem.

any help would be appreciated.

 

regards,