In my controller, I have:

public Contact c {get; set;}
public Contact cont {get; set;}
Public String acc3{get;set;}
Public String acc2{get;set;}

public controller(){

 acc3 = ApexPages.currentPage().getParameters().get('Master_Id__c'); 
           if(acc3 != null)        
                this.c= [   SELECT  Master_Id__c
                              FROM    Contact
                              WHERE   Master_Id__c = : acc3 ];  
            {this.c= new Contact  ();} 

 acc2 = ApexPages.currentPage().getParameters().get('LastName'); 
           if(acc2 != null)        
                this.cont= [   SELECT  LastName
                              FROM    Contact
                              WHERE   LastName = : acc2 ];  
            {this.cont=  new Contact(ReportsTo=new contact(Master_Id__c='xxxx')); } 

For string acc2, in the else statement, I have hardcoded the value to 'xxxx'. However, I want that value to be the same as the string in acc3 (which is an input field in my VF page). So it would look something like (Master_Id__c='!acc3')); However, I can't seem to get the syntax for it correctly. Any suggestions on how to correctly reference the string acc3? Thanks

2 Answers 2


As this is just Apex code, you can reference the string directly:

{this.cont=  new Contact(ReportsTo=new contact(Master_Id__c=acc3)); } 

As you create the new instance of Contact, you are assigning the string variable acc3 to the Contact object's Master_Id__c field.

The {! } syntax is only used in Visualforce pages not Apex classes.


That usually works, but for some reason it did not work in this case. (This code was to insert a parent and child at the same time. A parent contact with a child as well).

What finally worked was when I moved the this.cont into the Save function and changed it to

{this.cont=  new Contact(ReportsTo=new contact(Master_Id__c=c.Master_Id__c)); } 

before insert

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .