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
Aditya Rayavarapu 5Aditya Rayavarapu 5 

Command button action to reference inputfield

Hello, 
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 ];  
            }
              else
            {this.c= new Contact  ();} 

 acc2 = ApexPages.currentPage().getParameters().get('LastName'); 
           if(acc2 != null)        
            {
                this.cont= [   SELECT  LastName
                              FROM    Contact
                              WHERE   LastName = : acc2 ];  
            }
               else
            {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
BalajiRanganathanBalajiRanganathan
it should be

new contact(Master_Id__c=acc3)
Aditya Rayavarapu 5Aditya Rayavarapu 5
Hello,
I have tried that, but it does not work
Error message: 
System.DmlException: Insert failed. First exception on row 0; first error: MISSING_ARGUMENT, Master_Id__c not specified: []

I have also tried
='!acc3'
=get.acc3
=!acc3
=get<acc3>
=get<String>{acc3}

and similar syntaxes, but none of them work
BalajiRanganathanBalajiRanganathan
this is the corret syntax. 
this.cont=  new Contact(Master_Id__c=acc3)

but verify what you are getting the value for acc3. system.debug('acc3 :' + acc3).
BalajiRanganathanBalajiRanganathan
you have to do it in two setps.
Contact c1 = new Contact(Master_Id__c=acc3);
insert c1;

this.cont = new Contact(ReportTo = c1.id);
insert this.cont;
 
Aditya Rayavarapu 5Aditya Rayavarapu 5
Hi, 
I have tried that as well, It does not work.
Salesforce only accepts standard lookups to insert parent and child in the same action, and only specific syntax
Aditya Rayavarapu 5Aditya Rayavarapu 5
Hi, I just figured it out, I needed to have this.cont = in the public PageReference save() { instead of under public controller(){
So the final code was:
  acc2 = ApexPages.currentPage().getParameters().get('LastName'); 
           if(acc2 != null)        
            {
                this.cont= [   SELECT  LastName
                              FROM    Contact
                              WHERE   LastName = : acc2 ];  
            }
            
             else
            {this.cont=  new Contact()  ; }
            
            
            
            }
            
    public PageReference save() {
   
insert c;
this.cont= New Contact(ReportsTo= new Contact(Master_Id__c = c.Master_Id__c),LastName=cont.LastName);
insert cont;


Thanks a lot for your help Balaji!!