+ Start a Discussion
venkateshvenkatesh 

Help required to associate.

Hi,

 

     I have written the trigger code to associate parent object with child object. This has been achieved by directly assigning a Id's to each master-detail fields of junction object. I have pasted my code.

 

trigger createAssociationWithAccounts on Sales_Order__c (before update, before insert) { Map<Id, Sales_Order__c> acctsUpdate = new Map<Id, Sales_Order__c>(); for (Integer i = 0; i < Trigger.new.size(); i++) { acctsUpdate.put(Trigger.old[i].id, Trigger.new[i]); } for(Integer j = 0; j < acctsUpdate.size() ; j++){ AccountOrderAssociation__c accountOrderAssociation = new AccountOrderAssociation__c(); accountOrderAssociation.SalesOrder__c = 'a018000000LprTD'; accountOrderAssociation.Accounts__c = '0018000000PBqSq'; insert accountOrderAssociation; } }

 

In above code, Sales_Order__c is a child object and Account is a parent object. I have associated these two objects using junction object (AccountOrderAssociation__c). In junction object, I have created two master-detail fields like Accounts__c and SalesOrder__c. I have manually assign the Sales_Order__c's Id to Account and vice-versa. After inserting the junction object I will be able to associate between two objects.

 

Please let me know how I can put the Sales_Order__c's Id to Account and Account Id's to Sales_Order__c dynamically using Map.

 

Thanks in advance.