1

I have a custom Visualforce controller that applies to a custom object (SF__c). This object has a master-detail to the Opportunity object. My controller pre-populates the SF__c object with the Opportunity ID and the Owner ID upon creation. I would also like to populate a Contact__c field on the SF__c object with a Contact Role on the Opportunity that has a Role of "Signer". Does anyone know how I can do this in my controller?

public class VF_SFController{

public List<SF__c> ESF {get; set;}

    private final Opportunity opp;
    public VF_SFController(ApexPages.StandardController myController){
        SF = new List<SF__c>();
        myController.addFields(new List<String>{'Id', 'OwnerId', 'Owner.Phone'});
        opp=(Opportunity)myController.getrecord();
    }

    public SF__c SF2 = new SF__c();
        public void SF(){

            SF2.Opportunity__c = opp.Id;
            SF2.Rep__c = opp.OwnerId;
            SF.add(SF2);
        }


    public PageReference save() {

        insert SF;

       PageReference RetPage = new PageReference('/apex/VFSFView?id=' + SF[0].id);
        RetPage.setRedirect(true);
        return RetPage; 
    }

}
  • 1
    why can't you just query for the OCRs in the constructor? You know theopp.id – cropredy Nov 6 '15 at 17:12
  • I'm not sure how to do that. – J. Neilan Nov 6 '15 at 17:21
1

Assuming that

public void SF(){

        SF2.Opportunity__c = opp.Id;
        SF2.Rep__c = opp.OwnerId;
        SF.add(SF2);
    }

is your page action method, you could extend it to:

public void SF(){

        SF2.Opportunity__c = opp.Id;
        SF2.Rep__c         = opp.OwnerId;

        Opportunity o = select id (select id, contactId from OpportunityContactRoles
                              where role = 'Signer' order by lastModifiedDate desc limit 1)
                           from Opportunity where id = :opp.id];
        SF2.contactId = o.opportunityContactRoles.size() != 0 
                        ? o.opportunityContactRoles[0].contactId  
                        : null 
        SF.add(SF2);
    }

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.