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
aKallNVaKallNV 

Custom Button not passing ID

hi,

 

I have a custom button that I use to pass two id's to a controller. The button allows me to launch a VF page from a related list. This button was working fine, but has suddenly started throwing an invalid id error. My first assumption was that it had to do with the introduction of a new record type, which was causing an error in my controller somewhere, but I quickly noticed that the button was setting up the URL incorrectly. So, the button is meant to pass two ids one for custom object Goal__c and another for Account, and their query string variables are gid and aid, respectively. I noticed that when I click the button and get the error message that aid does not have the account's id assigned, and the error message points me to first line of code that works with account id. To further confuse things, I have buttons that work in a very similar matter, but they are launched from a different object, but still rely on account id in the same way...these still work fine. So, I'm curious to know if anybody has any ideas on why this might be failing.

 

Here is url that the button sets up: /apex/narrativeWizard3?gID={!Goal__c.Id}&aID={!Account.Id}

Best Answer chosen by Admin (Salesforce Developers) 
wzhaowzhao

1. Which page is the cutom button on? Is it on Account detail page, Goal__c detail page or some other page?

 

2. What is the relationship between Goal__c and Account (lookup? master-detail?) When you contructing a query string like this

 

/apex/narrativeWizard3?gID={!Goal__c.Id}&aID={!Account.Id}

 

Which Goal__c and Account are you expecting to get returned? You mentioned it is a related list of Accounts. So how would the page know which Account to return?

 

3. What was the error message you see when you try the following?

 

/apex/narrativeWizard3?gID={!Goal__c.Id}&aID={!Goal__c.AccountId__c}



 



All Answers

Chamil MadusankaChamil Madusanka

Hi,

 

Please post your code.

aKallNVaKallNV
/*
*  Narrative Wizard - this class is called from the New Log Entry button in DeployedActionItems__c, Goal__c, Contacts and Cases object
*/
public with sharing class narrativeWizard_Controller3{  
  
    public SchoolWorkPlanLog__c swpn { get; set; } //object to be shown and edited from the page
    
    public String selectedUnit { get; set; } //the selected DeployedActionItem Unit
    
    public Set<String> caseRTOptions = new Set<String>();//for case record type options
    
    public String selectedCaseRT { get; set; } //the selected Case record type
    
    public String electedGoalUnit { get; set; } //The selected Goal Unit for Query
    
    public boolean showAll { get; set; } //controls wether or not only priority Action Items or all Action items are shown.
    
    public String swpnAccount { get; set; } //stores the Name of the Account from which the Log was initiated.
    
    public narrativeWizard_Functionality controller { get; set; } //contains all functionality to narrative log entry 
  
  
    //CONSTRUCTOR
    public narrativeWizard_Controller3(ApexPages.StandardController swpnController){
      controller = new narrativeWizard_Functionality();
      controller.GoalId = ApexPages.currentPage().getParameters().get('gID');
      controller.ContactId = ApexPages.currentPage().getParameters().get('cID');
      controller.CaseId = ApexPages.currentPage().getParameters().get('cseID');
      controller.AItemId = ApexPages.currentPage().getParameters().get('actID');
        controller.accountId = ApexPages.currentPage().getParameters().get('aID');
        selectedUnit = 'All';
        swpn = (SchoolWorkPlanLog__c)swpnController.getRecord();
        swpn.Account__c = ApexPages.currentPage().getParameters().get('aID');
        showAll = false;
        makesStrategyWrappers();
        makesGoalWrappers();
        makesContactWrappers();
        makesCaseWrappers();
        swpnAccount = controller.getAccountName();
    }
    
    //makes Action Item Wrappers
    public void makesStrategyWrappers(){
      String query = controller.getStrategyQuery(selectedUnit);
        //makesStrategyWrappers stores values in variables in narrativeWizard_Functionality
        controller.makesStrategyWrappers(query, 3, controller.AItemId , this.showAll);
    }
           
    //makes Goal Wrappers, which are the goals that are displayed on Goals tab in VF page.
    public void makesGoalWrappers(){
        if(selectedGoalUnit == null){
           selectedGoalUnit = 'All';
        }
        String query = controller.getGoalQuery(selectedGoalUnit);
        controller.makesGoalWrappers(query, 3, controller.GoalId);
    }
     
    //makes Contact Wrappers
    public void makesContactWrappers(){
      controller.makesContactWrappers(3, controller.ContactId);
    }
     
    //makes Case Wrappers
    public void makesCaseWrappers(){
        //String query = controller.getCaseQuery(selectedCaseRT);
        controller.makesCaseWrappers(3, controller.CaseId);
    }
    
    //Method called by Save Button
    public PageReference processSelected(){  
        SchoolWorkPlanLog__c newLog = new SchoolWorkPlanLog__c(
                                      Account__c = controller.accountId,
                                      Subject__c = this.swpn.Subject__c,
                                      Narrative__c = this.swpn.Narrative__c,
                                      //PreEngagementPlan__c = this.swpn.PreEngagementPlan__c,
                                      InteractionDate__c = this.swpn.InteractionDate__c,
                                      CategoryofWork__c = this.swpn.CategoryofWork__c,                
                                      HoursSpent__c = this.swpn.HoursSpent__c,
                                      //NextStepDate__c = this.swpn.NextStepDate__c,
                                      //NextStepPlan__c = this.swpn.NextStepPlan__c,
                                      Category__c = this.swpn.Category__c,
                                      OffPlan__c = this.swpn.OffPlan__c,
                                      Department__c = this.swpn.Department__c,
                                      Unit__c = this.swpn.Unit__c);
        
        SchoolWorkPlanLog__c updatedlog;
        //SchoolWorkPlanLog__c newLog = this.swpn;
        
        try{   
          insert newLog;
          updatedlog = [Select Unit__c, HoursSpent__c, CategoryofWork__c, OffPlan__c from SchoolWorkPlanLog__c where Id =: newLog.Id LIMIT 1];
          if(updatedlog.Unit__c == 'Cross Unit: Coach'){
          if(updatedlog.HoursSpent__c == null){
            delete newLog;
            ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Hours spent: you must enter a value.');
          ApexPages.addMessage(errMsg);
          return refreshPage();
          }
          if(updatedlog.CategoryofWork__c == null){
            delete newLog;
            ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Category of Work: you must enter a value.');
          ApexPages.addMessage(errMsg);
          return refreshPage();
          }
        }
          
        }catch(Exception e){
          ApexPages.Message errMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'The hours spent can not be negative.');
      ApexPages.addMessage(errMsg);
          return refreshPage();
        }
        
        Pagereference newpage;
        newpage = controller.createActionItems(3, newLog, updatedlog);
        if(newpage != null){
          return newpage;
        }
        
        newpage = controller.createGoals(3, newLog.Id);
        if(newpage != null){
          return newpage;
        }
        
        newpage = controller.createContacts(3, newLog.Id);
        if(newpage != null){
          return newpage;
        }
        
        newpage = controller.createCases(3, newLog, updatedlog);
        if(newpage != null){
          return newpage;
        }
        
        Pagereference acctPage = page.MyPortfolio;
        acctPage.getParameters().put('sID', swpnAccount);
        return acctPage;
    }
    
    //the Method called by the Cancel Button.
    public PageReference cancelButton(){
        Pagereference acctPage = page.MyPortfolio;
       acctPage.getParameters().put('sID', swpnAccount);  
        return acctPage;
    }
    
    //this function refresh the page with the current Object and the accountId
    public Pagereference refreshPage(){ 
        return controller.refreshPage(3);
    }
    
    //sets up Strategy unit filter
    public List<SelectOption> getUnits(){
        return controller.getUnits(controller.getStratyUnits());
    }
    
    //sets up Case Record Type filter
    public List<SelectOption> getCaseRTs(){
      return controller.getCaseRTs(this.caseRTOptions);
    }
    
    //sets up Goal's Unit Filter
    public List<SelectOption> getGoalUnits(){
        return controller.getGoalUnits(controller.getAllGoals());
    }
    
    //DISPLAY CONTROLS                
    public Boolean showAIandGoal {
        get{
        if(this.swpn.OffPlan__c == False){ return true; }else{ return false; }
        }
    }
    
}

  The error is 'String Exception: Invalid Id' on line 33, which is the following line in the constructor: swpn.Account__c = ApexPages.currentPage().getParameters().get('aID');

Kris GrayKris Gray

Are you able to get the URL it is using to request your new page?

 

 

aKallNVaKallNV

That is what the problem seems to be...the URL comes out wrong. The button is configured to feed the Goal Id and the Account Id as such:  /apex/narrativeWizard3?gID={!Goal__c.Id}&aID={!Account.Id}

 

However, on the error page the URL says  /apex/narrativeWizard3?gID=a1D40000000I9ns&aID=

 

And again, what is weird is that this use to work, and I have other buttons like it that still work. For example, I have the same button but for the Contact object rather than Goal__c, which is set up like so: /apex/narrativeWizard3?cID={!Contact.Id}&aID={!Account.Id}

wzhaowzhao

It is really hard to know what is going on without poking around the actual page.

 

My wild guess is that Account.id might not even be there for you to pass it into the query string in the first place.Can you try to change the content source to be "Onclick Javascript" and replace the content to:

 

alert( {!Account.Id});

 

you should be able to see a valid id in the alert box once you invoke the button.If not, then that means the way you construct the query string is not correct. On the other hand,

alert( {!Goal__c.Id}); should definitely display an id in your case.

aKallNVaKallNV

Thanks for the idea. I know nothing about javasript...just some Apex and VF so  i wouldn't have though of that. 

 

Anyway, I did it, and clicking the button simply presents me with the word 'undefined' in the alert pop-up.

 

So, it seems that our suspicion was correct...the Account ID is not being found. Any ideas on why?

 

 

wzhaowzhao

If that is the case, are you sure this ever worked before? I am guessing you are on the Goal detail page and you are trying to find the Account id associated with that Goal. I think the correct sytax should be:

 

/apex/narrativeWizard3?gID={!Goal__c.Id}&aID={!Goal__c.AccountId__c}



Let me know if this works for you.

aKallNVaKallNV

Yes, it worked when we initially deployed everything, and there are other buttons that work in the same way but from a different object. For example, one from the Contact object, and it finds the Account ID fine. The only thing I can remember doing before it broke was introducing a new record type to the Goal Object, but going back to one record type doesn't make it work again, and can't think of why that would effect anything.

 

It won't let me use the syntax that you suggested. The syntax I have seems to be the only way, and it is wokring for those other buttons that I mentioned.

wzhaowzhao

1. Which page is the cutom button on? Is it on Account detail page, Goal__c detail page or some other page?

 

2. What is the relationship between Goal__c and Account (lookup? master-detail?) When you contructing a query string like this

 

/apex/narrativeWizard3?gID={!Goal__c.Id}&aID={!Account.Id}

 

Which Goal__c and Account are you expecting to get returned? You mentioned it is a related list of Accounts. So how would the page know which Account to return?

 

3. What was the error message you see when you try the following?

 

/apex/narrativeWizard3?gID={!Goal__c.Id}&aID={!Goal__c.AccountId__c}



 



This was selected as the best answer
aKallNVaKallNV

Just got back around to this...it's been a busy week because we just turned chatter on.

 

anyway, you're right. the syntax is {!Goal__c.AccountId__c} and the button works as expected now.

 

I think I was getting an error when I tried {!Goal__r.AccountId__c}

 

 

Thanks!