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
Sascha LöfflerSascha Löffler 

Copy Relationship to another Parent

I need your help for the following requirement:

We want to copy contact relationship from a Custom Object to another record of that Object, using visualforce page.

Objects:
  • Group__c
  • Contacts
  • Group_Relationship__c (Junction Object between Group & Contacts)
We add a button for Group__c Record Detail Page (e.g. Cars) that will open a Visualforce Page. On that Page the User should be able to select another Group__c Record (e.g. bikes) (by lookup? or what will be best practice to search for another record from same object?) and all Contacts related to the selected Group__c Record (bikes) should be related to the source Group__c Record (Cars).

I didnt find anything to come out of that so i hope you can give me some hints.
srlawr uksrlawr uk
This does sound like a fairly complex bit of functionality that will require a Custom controller extension on your visualforce page.

It sounds like you are on the right track though.

It is not entirely simple to "make" a custom lookup on a visualforce page for an arbitrary record.. certainly not using the standard <apex:inputfield> tag anyway.. you might be able to do some jiggery pokery with the record in memory (ie. the Group that launched the page) to get a lookup field with a Value referenced to itself, or a new "shim" field on the Group that is itself a lookup to "Group__c" and then nick the value out of that when it is selected... or you will have to write your own quick interface to allow the user to populate a text value and then send that back to some SOQL to try to find a single match..

There are some neat examples of doing things like that all over the internet, including "search as you type" boxes with a bit of jQuery etc. - allowing you to make quite a nice UI if you want.

Once you have the new Group__c selected, you will need to fire an action back to your page.. this can be a straight up "Submit" button on your form, or you could use @RemoteActions to Ajax yourself a form submission. Simplest is to just submit the form and put an onSubmit back to a method in your Apex.

The target method will then look something like:
 
public PageReference migrateGroup() {

  // Load all the junction objects for the old group...

  // thisGroup is the instance variable you push the Standard controller.getRecord() to
  List<Group_Relationship__c> contactsUnderOld = [SELECT Id FROM 
  Group_Relationship__c WHERE Group__c = :thisGroup.id];

  // Update the Group__c field
  for(Group_Relationship__c thisRel : contactsUnderOld) {
      // newId is the ID you got from your UI lookup bit
      thisRel.Group__c = newId;
  }

  // Stick them back in the DB
  update contactsUnderOldId;

}

That should move all the Contact relationships via the junction object to the new group Id; which hopefully is what you are after?

Don't forget a unit test or two! Sorry I couldn't be more helpful on the UI aspect, it really depends on what you want to do there; and there are so many options it would take days to type up.