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
abhaabha 

Relating different custom objects based on some specific field

In general database structure, we relate 2 tables by specifying some column as a foreign key.

In my application I have created 2 custom objects "Donor" and "DonLogin". Donor contains all basic information like 'donorID', name, address.. While DonLogin have login information i.e.. fields like username, password etc. I want to create 1-1 relationship between Donor-DonLogin. DonorID is AutoNumber.  Can I relate these 2 objects using DonorID?

I am creating object records through visualforce page and controller class. I have managed to create records for both objects through same controller. But I want to take some field of 'Donor'(especially DonorID) and want to insert its value in 'DonLogin'.  It is giving me error saying 'DonorID is not writable field'. What should be done so that DonorID will be editable field?  I want to refer  this system generated value in other custom objects as a reference key. How to do this?

Master-detail relationship type does not allow any specific field to select as reference key. How can I set some field as a connection between 2 objects?

SFDC Consulting 123SFDC Consulting 123

Hi,

 

In Salesforce.com the following are possible.

 

If you want to related Donor with DonLogin, create a field in DonLogin as a Lookup reference to Donor object. This will automatically does what you want. But this allows you to create more than one record in DonLogin for the same DonorId. Since you do not want that, control it using ur visual force page.

 

Best Wishes,

abhaabha

Thank you for your reply!!

I tried as per your solution.

But it is giving me error. I am posting here visualforce page and controller class code. Will you please help me to solve this?

Visualforce page 'DonorRegi1'

<apex:page controller="DonorRegiCon1" >
    <apex:form >
        <apex:pageBlock mode="edit">
            <apex:pageMessages />
            <apex:pageBlockSection >
                <apex:inputField value="{!Details.Name}"/><br/>
                <apex:inputField value="{!Details.Address__c}"/><br/>
                <apex:inputField value="{!Details.DOB__c}"/><br/>
                <apex:inputField value="{!Details.Gender__c}"/><br/>
                <apex:inputField value="{!Details.Height__c}"/><br/>
                <apex:inputField value="{!Details.Weight__c}"/><br/>                               
                <apex:inputField value="{!Details.Allergies__c}"/><br/>
                <apex:inputField value="{!Details.Health_Problems__c}"/><br/>    
                <apex:inputField value="{!Details.Contact_No__c}"/><br/>
                <br/>
                <apex:inputField value="{!Details.Bloodgroup__c}"/> <br/>
                </apex:pageBlockSection>
                <apex:pageBlockSection >
                <br/><br/>
              <b>Username:</b><br/>
        <apex:inputSecret id="username" value="{!username}" />
           <b>*Password:</b><br/>
              
        <apex:inputSecret id="password" value="{!password}" />
         <b>*Confirm Password:</b><br/>
        <apex:inputSecret id="cpassword" value="{!cpassword}" />
                
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Sign Up" title="Register" onclick="validate()" action="{!mysave}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
     <script type="text/javascript">
  function validate() 
   { 
    var y = document.getElementById("j_id0:j_id2:password");
    var a = document.getElementById("j_id0:j_id2:cpassword");
    var x = document.getElementById("j_id0:j_id2:username");    
    if (x.value.length == 0 && y.value.length == 0 ) { alert("Enter the Username and password \n");}
    
     else if (x.value.length == 0) { alert("Enter the Username\n");}
    
     else if (y.value.length == 0) { alert("Enter the Password\n");}
     else if (y.value != a.value) { alert("Password didn't match\n");return false; }
    
    else {
     return true;
    }
    }
  </script>
</apex:page>

 Controller class code 'DonorRegiCon1'

public class DonorRegiCon1 {

 public String username {get; set;  }
  public String password {get; set {password = value == null ? value : value.trim(); } }
   public String cpassword {get; set {cpassword = value == null ? value : value.trim(); } }
    public Donor__c d { get; set; }
    public DonLogin__c dl { get; set; }
    public DonorRegiCon1() {

        d = new Donor__c();
        System.Debug('constructor is running');
    }
        public Donor__c getDetails() {
        return d;
    }
    /*public DonLogin__c getDetails() {
        return dl;
    }*/
    public PageReference mysave() {
        try {
        d.Username__c=username;
        d.Password__c=password;
            insert(d);
        System.Debug('record insertion');
        dl=new DonLogin__c();
         dl.Username__c=username;
         dl.Password__c=password;
         dl.Donorobj__c=d.Name;
            insert(dl);
           
        } catch(System.DMLException e) {
            ApexPages.addMessages(e);
            return null;
        }
       PageReference pageRef = new PageReference('http://myappmbb-developer-edition.ap1.force.com/RegiSuccess');
      return pageRef;
    }
}

 It is showing following error:-

Visualforce Error

System.StringException: Invalid id: donlog10

Error is in expression '{!mysave}' in component <apex:page> in page donorregi1

 

Class.DonorRegiCon1.mysave: line 28, column 1

 

 It is giving error for   [ dl.Donorobj__c=d.Name; ] statement.

I tried [ dl.Donorobj__c=d; ], but it is not working.

Will you suggest some solution?