1

Everything work fines if I use a static resource instead of using a method in it, such as

apex:image value="{!$Resource.Testprod1} 

where testprod1 is a static resource.

    apex:image value="{!URLFOR($Resource[prodpic])} 

where prodpic is the method name.

I want to use method because image is not same for all, it needs to be different per product. method brings in the image in relevance.

The error i get is

Invalid target parameter for function URLFOR Error is in expression '{!URLFOR($Resource[prodpic])}' in component in page testcheckpage

below is the code

VF page

<apex:page controller="productslistpage" showHeader="false">

 <Apex:form >

  <apex:pageblock >  
<apex:pageBlockSection columns="3">
            <apex:repeat value="{!prodlist}" var="prod">
                <apex:pageBlockSectionItem >

             <apex:commandLink action="{!prod1button}">

             <apex:param name="prodid"
                         value="{!prod.Productid__c}"
                         assignTo="{!prodid}"/>



            <apex:image value="{!URLFOR($Resource[prodpic])}"/>

            <br/>
            <h3>{!prod.name}(Gray)</h3> 
            </apex:commandlink>
            <p>{!prod.Price__c}</p>
  </apex:pageblocksectionitem>
          </apex:repeat>

</apex:pageblocksection>

 </apex:pageblock> 

  </apex:form>
</apex:page>

Class

public Class productslistpage{

    public list<inventory__c> prodlist{get; set;} 
    public string PID{get; set;}
    public string name{get; set;}
    public string usermode{get; set;}
    public String prodid {get; set;}
    public String prodpic {get; set;}
    public String prodpage {get; set;}  
    integer f;
    PageReference pg;

public productslistpage(){
    prodlist = new list<inventory__c>();
    this.name = ApexPages.currentPage().getParameters().get('Name');
    this.usermode = ApexPages.currentPage().getParameters().get('usermode');
    prodlist = [Select prodpic__c,ButtonName__c,Name, Price__c, Stocks__c,productid__c from Inventory__c];
}     


public String getprodpic(){

         for(inventory__c product : prodlist)  
      {
          if (product.productid__c == prodid){
              prodpic =  product.prodpic__c;

       }
    }
    return prodpic;
}


public pagereference prod1button(){

      f=0;
      for(inventory__c product : prodlist)  
      {
          if (product.productid__c == prodid){
              prodpage =  '/apex/' + product.buttonname__c;
                f=1;

          }
     } 


       if(f==1)
       pg = new PageReference(prodpage);  
       else
       pg = new PageReference('/apex/testprodpage');          
      return pg;
     }
}
0

You're getting this error because getprodpic() returned null. apex:param only works when the apex:commandButton is clicked, not during rendering. Use a wrapper class, or use the field directly. Here's a suggested minimal implementation change from your code.

Note: this has not been tested, and may require fine-tuning.

public class Wrapper {
    public Wrapper(Inventory__c record) {
        prod = record;
    }
    public Inventory__c prod { get; set; }
    public String getProdPic() {
        return prod.ProdPic__c == null? 'defaultprodimage': prod.ProdPic__c;
    }
    public PageReference prod1Button() {
        return new PageReference('/apex/'+prod.buttonname__c);
    }
}

public Wrapper[] getProdList() {
    Wrapper[] results = new Wrapper[0];
    for(Inventory__c record: prodList) {
        results.add(new Wrapper(record));
    }
    return results;
}

<apex:pageBlockSection columns="3">
            <apex:repeat value="{!prodlist}" var="item">
                <apex:pageBlockSectionItem >

             <apex:commandLink action="{!item.prod1button}">
            <apex:image value="{!URLFOR($Resource[item.prodpic])}"/>

            <br/>
            <h3>{!item.prod.name}(Gray)</h3> 
            </apex:commandlink>
            <p>{!item.prod.Price__c}</p>
  </apex:pageblocksectionitem>
          </apex:repeat>

</apex:pageblocksection>

  • Thanks you Sfdc for the answer, sorry I wasn't well all this time, let me check on this and get back to you. – Niknit Aug 22 '17 at 12:01

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.