-2

I have created a Visualforce page to create multiple contacts from an opportunity and I am trying to populate the opportunity id as a lookup field on the created contact. I am using the the getRecord() method however when I try to add the variable to set the field value the contact record is created and the field is blank.

public class addApprenticeDetails_CX {

public String fName { get;set; }
public String lName { get;set; }
public String email { get;set; }
public String accName { get;set; }
public Id AccId { get;set;}
public List<Account> accList { get;set;} 

public Opportunity opp = new Opportunity();
public addApprenticeDetails_CX(ApexPages.StandardController controller){
    this.opp = (Opportunity)controller.getRecord();
    system.debug('Opportunity' + opp.id);
}

    public PageReference callLookup(){
        accList = [Select id,Name from Account];
        PageReference pr = new 
        PageReference('/apex/addApprenticeDetailsAccountLookup');
        pr.setRedirect(false);
        return pr;
    }

    public PageReference goBack(){
    accName = ApexPages.currentPage().getParameters().get('AccName');
    AccId = ApexPages.currentPage().getParameters().get('AccId');
    PageReference pr = new PageReference('/apex/addApprenticeDetails');
    pr.setRedirect(false);
    return pr;
    }

    public PageReference save(){
        if(lName == null || lName == ''){
        ApexPages.addmessage(new 
        ApexPages.message(ApexPages.severity.WARNING,
        'Last Name is Required!'));
        return null;
        }
        if(email == null || email == ''){
        ApexPages.addmessage(new 
        ApexPages.message(ApexPages.severity.WARNING,
        'Email is Required!'));
        return null;    
        }
        else{
            Contact con = new Contact();

                    con.FirstName = fName;
                    con.LastName = lName;
                    con.Email = email;
                    con.AccountId = AccId;
                    con.RecordTypeId = Schema.SObjectType.Contact.
                    getRecordTypeInfosByName().get('Corporate Contact')
                    .getRecordTypeId();
                    con.Third_Party_Opportunity__c = this.opp.id;
                insert con;


    PageReference pr = new PageReference('/apex/addApprenticeDetails');
    pr.setRedirect(true);
    return pr;

    }
    }
public list<Account> accs { get; set; }
public String searchString { get; set; }
public addApprenticeDetails_CX(){

}
public void search(){
    string searchQuery = 'select name, id from account where name like 
    \'%'+searchString+'%\'Limit 20';
    accs = Database.query(searchQuery);

}
public void remove(){
    accs.clear();
}
}
  • 1
    Is the page being used independently or embedded in an Opportunity page layout? Is the id= parameter present in the URL? – David Reed Feb 26 '18 at 14:49
  • It is embedded in the opportunity page layout in a new section – Hannah Feb 26 '18 at 14:51
  • 1
    I see some issues in your action methods (they're looking for parameters that won't be there), but I don't see the issue with your Opportunity id acquisition. Is that debug statement in your constructor showing a null value? – David Reed Feb 26 '18 at 15:23
  • This controller is used for more than one VF page. The other one uses the search and remove method to search a list of accounts to select as the accountid for the newly created contact. I am not getting the debug statement at all. – Hannah Feb 26 '18 at 16:37
  • I would suggest that you factor out "shared" methods into a service class and write one small controller for each Visualforce page. Debugging a shared controller gets really ugly really fast. – David Reed Feb 26 '18 at 16:41
1

I have removed the standard controller and only used my custom controller. To get the opportunity Id in my lookup field I have used con.Third_Party_Opportunity__c = ApexPages.currentPage().getParameters().get('Id'); and this has solved my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.