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
Kerry ProkselKerry Proksel 

Populate custom lookup field from custom URL button with a visualforce page

I created a custom button on the opportunity that goes to a visualforce page. That visualforce page creates a specifc case type and allows attachments. My goal is to populate the "Opportunity__c" lookup field on the Case with the opportunity Id. I have tried editing the button so the URL is this:

/apex/InfoSecCase?00N0v000002BPBH={!Opportunity.Id}

When clicking the button, I can see the parameter is passed through: 
User-added image

But when I click Save, the field isn't populated. Could someone please help point me in the right direction?

Thank you,
Best Answer chosen by Kerry Proksel
PawanKumarPawanKumar
Please try below controller.

public class CaseAttachment {
    public case objcase{get;set;}
    public Attachment myAttachment{get;set;}
    public string fileName{get;set;}
    public Blob fileBody{get;set;}
    Opportunity opp;
    
    public CaseAttachment(Apexpages.standardcontroller controller)
    {
        String optyID = ApexPages.currentPage().getParameters().get('OptyId');
        objcase = new case();
        objcase.Opportunity__c = optyID;
        myAttachment =new Attachment();
    }
    public pagereference save()
    {
        insert objcase;
        System.debug('@@@@@fileBody'+fileBody);     
        myAttachment  = new Attachment();
              Integer i=0;
              myAttachment .clear();
              myAttachment.Body = fileBody;
              myAttachment.Name = fileName; 
              myAttachment.ParentId = objcase.id;             
              insert myAttachment;                 
        pagereference pr = new pagereference('/'+objcase.id);                           
        return pr;
    }


}

Please change URl as below.
/apex/InfoSecCase?OptyId={!Opportunity.Id}

 

All Answers

PawanKumarPawanKumar
can you please share you VF and Controller code? then we suggest you better
Kerry ProkselKerry Proksel
Absolutely! 

Controller Extension:
 
public class CaseAttachment {
    public case objcase{get;set;}
    public Attachment myAttachment{get;set;}
    public string fileName{get;set;}
    public Blob fileBody{get;set;}
    Opportunity opp;
    
    public CaseAttachment(Apexpages.standardcontroller controller)
    {
        objcase = new case();
        myAttachment =new Attachment();
    }
    public pagereference save()
    {
        insert objcase;
        System.debug('@@@@@fileBody'+fileBody);     
        myAttachment  = new Attachment();
              Integer i=0;
              myAttachment .clear();
              myAttachment.Body = fileBody;
              myAttachment.Name = fileName; 
              myAttachment.ParentId = objcase.id;             
              insert myAttachment;                 
        pagereference pr = new pagereference('/'+objcase.id);                           
        return pr;
    }


}
Visualforce Page:
 
<apex:page standardController="case" extensions="CaseAttachment" lightningStylesheets="true">
    <apex:form id="frm">
        <apex:pageBlock title="Case Edit">
            <apex:pageBlockSection title="Case Information" collapsible="false">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Record Type"/>     
                    <apex:inputfield value="{!objcase.RecordTypeID}"/>
                </apex:pageBlockSectionItem>
                <apex:inputField value="{!objcase.Opportunity__c}"/>
                <apex:inputField value="{!objcase.Status}"/>
                <apex:inputField value="{!objcase.Service__c}"/>
                <apex:inputField value="{!objcase.Mobile__c}"/> 
                <apex:inputField value="{!objcase.Subject}"/>
                <apex:inputField value="{!objcase.Description}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Please Note: The SLA for filling out Questionnaires is currently 3 days" collapsible="false">
                <apex:inputField value="{!objcase.Requested_Completion_Date__c}"/>  
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}"/>
            </apex:pageBlockButtons>       
            <apex:pageBlockSection title="Uploading the Attachment" collapsible="false" dir="LTR" columns="1">
                <div id="upload" class="upload">                                   
                    <apex:inputFile id="fileToUpload" value="{!fileBody}" filename="{!fileName}" styleClass="input-file"/>                            
                </div> 
            </apex:pageBlockSection>           
        </apex:pageBlock>        
    </apex:form>
</apex:page>

I tried to initially do this by adding this to the controller:
public Opportunity getOpp() {
        
        opp = [SELECT id FROM Opportunity WHERE id =: ApexPages.currentPage().getParameters().get('id')];
        return opp;
    }

But I had issues setting the variable as the Opportunity__c field in the visualforce page. 

Thank you!
PawanKumarPawanKumar
Please try below controller.

public class CaseAttachment {
    public case objcase{get;set;}
    public Attachment myAttachment{get;set;}
    public string fileName{get;set;}
    public Blob fileBody{get;set;}
    Opportunity opp;
    
    public CaseAttachment(Apexpages.standardcontroller controller)
    {
        String optyID = ApexPages.currentPage().getParameters().get('OptyId');
        objcase = new case();
        objcase.Opportunity__c = optyID;
        myAttachment =new Attachment();
    }
    public pagereference save()
    {
        insert objcase;
        System.debug('@@@@@fileBody'+fileBody);     
        myAttachment  = new Attachment();
              Integer i=0;
              myAttachment .clear();
              myAttachment.Body = fileBody;
              myAttachment.Name = fileName; 
              myAttachment.ParentId = objcase.id;             
              insert myAttachment;                 
        pagereference pr = new pagereference('/'+objcase.id);                           
        return pr;
    }


}

Please change URl as below.
/apex/InfoSecCase?OptyId={!Opportunity.Id}

 
This was selected as the best answer
Kerry ProkselKerry Proksel
That worked! Thank you so so much!!!