1

I have some apex validation where if a user forgets to enter something in a related list, the system will give them a pageMessage saying that they need to enter a row into that list. We want to create a link that will take them directly to the 'new' version of the page right in the pageMessage (i.e., clicking on the New button on top of the related list).

I thought this would work, but it doesn't:

Commission_Split__c commSplit = new Commission_Split__c(Opportunity__c = quoteRecord.zqu__Opportunity__c);
PageReference prNewCommSplit = new ApexPages.StandardController(commSplit).view();
...
// prNewCommSplit.getUrl(); returns a link back to the quoteRecord

Any ideas?

1

If you want to create a link in the Page message you will have to set escape = "false" in the page message tag

Then you will have to construct your page message (apexPages.addMessage(...)) you can add

<a href="{YOURURL}">Click Here to Open New Record</a>

Which should output the link in the page message

  • Just to add to this escape setting can be security risk so has to be carefully thought – Mohith Shrivastava Aug 13 '14 at 16:00
  • Not really as long as there is no user input involved. Now if the message is constructed from user inputted data then yes you would have to worry about Injection or XSS. I could be completely mistaken though but security review is ok with it as long as there is no user input.... – Eric Aug 13 '14 at 16:01
  • a java script written on browser can be used to hack this . – Mohith Shrivastava Aug 13 '14 at 16:11
  • I defer to you....not me expertise at all.I would like to know if there is any good way to prevent it though maybe time for a new question. Please look for it and post up...OP, heed Mohith's warning – Eric Aug 13 '14 at 16:14
  • Hi - yes, I know how to do that, but I needed the specific case of navigating to the 'new'/'create' page. But I did figure it out and will post the answer shortly. – willard Aug 13 '14 at 18:02
0

So the way to get the new/create url is as follows:

Get the key prefix of the object you want to create: String keyPrefix = MyCustomObject__c.sObjectType.getDescribe().getKeyPrefix();

Reconstruct the url to get to the new/create screen:

String errorMessage = 'Your error message here.  Here is link: <a href="/' + 
    keyPrefix + '/e?CF00NQ0000001K2Al=' + quoteRecord.zqu__Opportunity__r.Name + 
    '&CF00NQ0000001K2Al_lkid=' + quoteRecord.zqu__Opportunity__c + '&retURL=%2F' +
    quoteRecord.Id + '&saveURL=%2F' + quoteRecord.Id + '">my create link</a>';
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, errorMessage));`

The CF00NQ0000001K2Al is the id of the Opportunity field, which should be consistent between orgs.

  • Actually - looks like the field is is not consistent between orgs. I will update my answer when I find out how to do that as well. – willard Aug 13 '14 at 18:15
  • So I found out how to get field ids from this post, but that seems a very hackish way of doing things. If there is a better way to do this, I am all ears and will accept that as the answer. – willard Aug 13 '14 at 18:24
  • Why do you need to trap the field from the page, why not just get it from the controller? – Eric Aug 13 '14 at 18:32
  • @Eric, can you tell me how to do this via a PageReference? Remember, I need to go to the create/new page. I couldn't figure out how to do it via the PageReference. Also - it needs to be a url that I can stick into my error message. – willard Aug 13 '14 at 19:29

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.