1

I'm currently making a custom editor in Visualforce that adds things to a related list. Things from this list are selected and then when the "add packages" button is click it should make the records, and redirect the page back to the Opportunity the user was in.

What happens is that it stays on the Visualforce page, but adds the records. No errors are displayed on the page but here are the errors I receive within the developer console:

Select_Package?retURL=%2F006L0000006EKr1&CF00NL0000003UiUg_lkid=006L0000006EKr1&sfdc.override=1&CF0…:1 
Refused to display 'https://cs8.salesforce.com/006L0000006EKr1' in a frame because it set 'X-
Frame-Options' to 'SAMEORIGIN'.

VFDevMode.js:21 Uncaught SecurityError: Sandbox access violation: Blocked a frame at 
"https://c.cs8.visual.force.com" from accessing a frame at 
"https://c.cs8.visual.force.com".  The frame being accessed is sandboxed and lacks the 
"allow-same-origin" flag.

I'm not sure what could be causing this, or how to fix it, so I will post the full visualforce and extension code.

Visualforce:

<apex:page standardController="Opportunity_Package__c" extensions="MetadataPackageEditor" sidebar="false" showHeader="false">
  <apex:outputPanel id="all">
  <apex:outputText >{!NOW()}</apex:outputText>
  </apex:outputPanel>
  <apex:form >

      <apex:outputField value="{!Opportunity_Package__c.Opp_Package_Rel__c}"/>

      <apex:pageBlock title="Select Package(s)">
      <apex:pageMessages />

          <apex:pageBlockTable value="{!packages}" var="o" id="table">
              <apex:column title="Select" headerValue="Select">
                  <apex:inputCheckbox value="{!o.selected}"/>
              </apex:column>
              <apex:column value="{!o.pack.Name}"/>
              <apex:column value="{!o.pack.Products__c}"/>
              <apex:column value="{!o.pack.PPU_Discount__c}" headerValue="Price per Unit Discount"/>
          </apex:pageBlockTable>
      <apex:commandButton value="Add Packages" action="{!processAndAddSelected}" rerender="all">
          <apex:param name="oppIdParam" value="{!Opportunity_Package__c.Opp_Package_Rel__c}" assignTo="{!oppId}"/>
      </apex:commandButton>
      <apex:commandButton action="{!cancel}" value="Cancel"/>
      </apex:pageBlock>



  </apex:form>

</apex:page>

Apex:

public class MetadataPackageEditor{
//------------------------------ Extension Code-----------------
private final Package__c pPackage;

private final Opportunity_Package__c oppPackage;

private final Opportunity opp;    
    // The extension constructor initializes the private member
    // variable acct by using the getRecord method from the standard
    // controller.

    Private ApexPages.StandardController controller;

    public MetadataPackageEditor(ApexPages.StandardController stdController) {

        sObjectType oType = stdController.getRecord().getsObjectType();

        this.controller = stdController;

        if (oType == Package__c.sObjectType){
            this.pPackage = (Package__c)stdController.getRecord();
        }

        else if (oType == Opportunity_Package__c.sObjectType) {
            this.oppPackage = (Opportunity_Package__c)stdController.getRecord();
        }

        else if (oType == Opportunity.sObjectType)
        {
            this.opp = (Opportunity)stdController.getRecord();
        }
    }
// Reference for page returns


public MetadataPackageEditor(ApexPages.StandardSetController stdController) {

        sObjectType oType = stdController.getRecord().getsObjectType();

        if (oType == Package__c.sObjectType){
            this.pPackage = (Package__c)stdController.getRecord();
        }

        else if (oType == Opportunity_Package__c.sObjectType) {
            this.oppPackage = (Opportunity_Package__c)stdController.getRecord();
        }

        else if (oType == Opportunity.sObjectType)
        {
            this.opp = (Opportunity)stdController.getRecord();
        }
    }    
//-------------------------------------End of Extension code------------------


public List<pPackage> packageList {get;set;}

public List<pPackage> getPackages() {
    if(packageList == null) {
        packageList = new List<pPackage>();
        for(Package__c p: [SELECT Id, Name, PPU_Discount__c, Expiration_Date__c, Products__c FROM Package__c]) {
            packageList.add(new pPackage(p));
        }
    }
    return packageList;
}

public List<Package__c> selectedPackages {get;set;}

public PageReference processSelectedPackages() {
    selectedPackages = new List<Package__c>();

    for(pPackage pPack: getPackages()) {
        if(pPack.selected == true){
            selectedPackages.add(pPack.pack);
        }
    }
    packageList=null;
    return null;
}

//Add all of the selected packages and return to the previous page
public String oppId {get;set;}

public PageReference processAndAddSelected(){
if(controller != null){
if(oppId != null){
    Opportunity opp = new Opportunity(id=oppId);
    processSelectedPackages();
if(selectedPackages != null && selectedPackages.size()>0){    
    for(Package__c pack: selectedPackages){
        Opportunity_Package__c oppPack = new Opportunity_Package__c();
        oppPack.Name = pack.Name;
        oppPack.Opp_Package_Rel__c = opp.Id;
        oppPack.Package_Opp_Rel__c = pack.Id;
        insert oppPack;
    }
   }

   else{
       throw new MetadataServiceExamplesException('Request failed due to selectedPackages being NULL.');
   }

    PageReference pRef = new PageReference('https://cs8.salesforce.com/'+oppId);
    pRef.setRedirect(true);

    return pRef;

    }
    else{
        throw new MetadataServiceExamplesException('Request failed due to oppId being NULL.');
        }
        }
    else{
        throw new MetadataServiceExamplesException('Request failed due to Controller being NULL.');
    }
}

public void pullStandardProducts () {

  MetadataService.MetadataPort service = createService();     
        MetadataService.CustomField customField = new MetadataService.CustomField();
        customField.fullName = 'Package__c.Products__c';
        customField.label = 'Products';
        customField.type_x = 'MultiselectPicklist';
        customField.visibleLines = 7;
        metadataservice.Picklist pt = new metadataservice.Picklist();
        pt.sorted= false;


    Id stdPricebook = [
        SELECT ID
        FROM Pricebook2
        WHERE IsStandard = true].Id;

    List<PricebookEntry> products = [
        SELECT Name
        FROM PricebookEntry
        WHERE Pricebook2Id = :stdPricebook];

    List<MetadataService.PicklistValue> valuesToAdd = new List<MetadataService.PicklistValue>();

    for (Integer j = 0; j < products.size(); j++){
        MetadataService.PicklistValue tempValue = new MetadataService.PicklistValue();
        tempValue.fullName = products.get(j).Name;
        tempValue.default_x = false;
        valuesToAdd.add(tempValue);
    }

    pt.picklistValues = new List<MetadataService.PicklistValue>();

    pt.picklistValues.addAll(valuesToAdd);

        customField.picklist = pt ;
        List<MetadataService.SaveResult> results =      
            service.updateMetadata(
                new MetadataService.Metadata[] { customField });
        handleSaveResults(results[0]);

}


public static MetadataService.MetadataPort createService()
    { 
        MetadataService.MetadataPort service = new MetadataService.MetadataPort();
        service.SessionHeader = new MetadataService.SessionHeader_element();
        service.SessionHeader.sessionId = UserInfo.getSessionId();
        return service;     
    }

public static void handleSaveResults(MetadataService.SaveResult saveResult)
    {
        // Nothing to see?
        if(saveResult==null || saveResult.success)
            return;
        // Construct error message and throw an exception
        if(saveResult.errors!=null) 
        {
            List<String> messages = new List<String>();
            messages.add(
                (saveResult.errors.size()==1 ? 'Error ' : 'Errors ') + 
                    'occured processing component ' + saveResult.fullName + '.');
            for(MetadataService.Error error : saveResult.errors)
                messages.add(
                    error.message + ' (' + error.statusCode + ').' + 
                    ( error.fields!=null && error.fields.size()>0 ? 
                        ' Fields ' + String.join(error.fields, ',') + '.' : '' ) );
            if(messages.size()>0)
                throw new MetadataServiceExamplesException(String.join(messages, ' '));
        }
        if(!saveResult.success)
            throw new MetadataServiceExamplesException('Request failed with no specified error.');
    }
    }

2 Answers 2

1

Is is possible that you are embedding this VF page in a standard page layout? That may be why you are getting the cross browser error. If you are, consider launching the page from a custom button on the source record, then redirecting back to the record when the Save completes. You shouldn't have to put the full URL into your pagereference, something like this should work fine:

pagereference pg = new pagereference('/'+oppid);
pg.setRedirect(true);
return pg;
5
  • This page is being redirected from the new button, from a related list. This page is not accessible anywhere else than from this related list, through the new button. And I've already tried that method and it provides the same error. Mar 5, 2015 at 21:23
  • Are you using Chrome? It could be because of the development footer, and Chrome interacting poorly. If you are using Chrome, try a different browser, and try it with the development footer turned off.
    – JimRae
    Mar 5, 2015 at 21:34
  • How would I turn the development footer off? Mar 5, 2015 at 21:35
  • From your user record, uncheck the Development Mode checkbox. Or, your can modify your url to include the core.apexpages.devmode.url=1 parameter
    – JimRae
    Mar 5, 2015 at 21:47
  • By god sir, you're a genius. Mar 5, 2015 at 21:56
0

Two things i would request you to do.

1) Remove the hardcoded instance URL and use a relative URL

PageReference pRef = new PageReference('https://cs8.salesforce.com/'+oppId); Change to PageReference pRef = new PageReference('/'+oppId);

2) In case the above doesnt work, remove redirect and return Page reference directly using the relative path.

2
  • I had already tried that before with the same results Mar 5, 2015 at 21:10
  • when I hover over the button in the left corner of your screen it will display the url path. if it is displaying visualforce. Then change the path in your pagereference to '../'+oppId
    – Suresh
    Mar 5, 2015 at 21:15

You must log in to answer this question.

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