+ Start a Discussion
SjaleelSjaleel 

How to get an Obect ID

Dear developers,

 

How can an object Id be accesse? is there a method that sets the ID of an object? I want to create VF page that would display a deatiled info page of my object, but I dont know how to pass or set the object id, other than copying and pasting from the URL!

Any help and suggestions would be appreciated.

 

Thanks in advance

bob_buzzardbob_buzzard

It all depends on how you want the user to navigate to your page.

 

For example, you can define a button for a standard/custom object that opens a visualforce page - in this case the id will automatically be added by the system.

 

Can you tell us a bit more about your page flow?

SjaleelSjaleel

Thanks for the response...here's my workflow

 

I have got an aircraft tab just like an accounts, but unlike people account its related to the aircraft. Am trying to create pdf detail doucment for the aircraft instance, which can be later on emailed as an attachment, the document should be generated when the button is clicked on but I just cant figure how the ID will be passed on or set, when my document is generated there is no info and and is pointing to null!

 

Hope this gives a better view for what am trying and want to...

 

thanks again in advance!

SjaleelSjaleel
How will the ID pass within the button?.... Sorry if my questions are silly or stupid, its just am a newbie to this VF thing! Thanks in advance
bob_buzzardbob_buzzard

Assuming this is a custom button on a standard/custom sobject, if you choose the button target as a visualforce page, Salesforce will automatically add the id.  It will also only let you select VF pages that use the standard controller for the standard/custom sobject type.

 

This sounds like the route to go based on your use case.

SjaleelSjaleel

public with sharing class MRInvoiceController {

private final Utilization_Report__c UR;

public ID UR_Id {get;set;}

public String email {get;set;}

 

public MRInvoiceController() {

 

UR = [select Aircraft__c, Date_of_MR_Payment_Due__c, 

Month_Ending__c, Invoice_Date__c, ,

Total_Maintenance_Reserve__c 

from Utilization_Report__c where id = :ApexPages.currentPage().getParameters().get('id')];

 

}

 

public Utilization_Report__c  getUR() {

return UR;

}

public PageReference sendPdf() {

PageReference pdf = Page.InvoiceGenerator;

pdf.getParameters().put('id',UR_Id);

  Blob body;

body = pdf.getContent();

Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();

attach.setContentType('application/pdf');

attach.setFileName('MRInvoice.pdf');

attach.setInline(false);

attach.Body = body; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

mail.setUseSignature(false);

mail.setToAddresses(new String[] { email });

mail.setSubject('Maintenance Reserve Invoice');

mail.setHtmlBody('Here is the email you requested! Check the attachment!');

mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with Invoice sent to '+email)); return null; } }

 

This is my controller code but am getting this error

 

System.QueryException: List has no rows for assignment to SObject

 

just cant find what the error is, but now am getting this message as an error!

bob_buzzardbob_buzzard

The only candidate for this is the select statement in the constructor I'd say, which matches up with the URL id parameter being null.

 

How does the user access this visualforce page?  Is it through a button added to the page layout or some other mechanism?

SjaleelSjaleel

yes its through a button the user get to access the vf page.....

 

so what do I need to do?

 

thanks in advance

SjaleelSjaleel

:ApexPages.currentPage().getParameters().get('id')....wouldn't this give the Object ID if not then how would we get it?

 

I just cant figure how its going to set the ID!

bob_buzzardbob_buzzard
How did you define the button? Is it a custom button on the object or something else?

The mechanism you are using is correct, as long as the id is added to the URL.
SjaleelSjaleel

Its a custom button on the object that calls the VF page where the user enters the email address need to send,

 

InvoiceGenerataor is the pdf file that needs to be sent, but its not getting the id of the aircraft

bob_buzzardbob_buzzard

And what does this button do?  I.e. when you created it, what behaviour did you specify - open URL, javascript, VF page etc?

SjaleelSjaleel

The button's behaviour is speciifed as an URL " /apex/MaintenanceReserveInvoice" ......when questioned it I sat and reviewed it and I felt there was something wrong

 

so I appended the url with this /apex/MaintenanceReserveInvoice?id={!Utilization_Report__c.Id} and it gave me the result in the PDF....

 

thanks alot for your help and questioning me helped to think further and to get to know where and what to look for

^__^......

 

Regards

Saj

bob_buzzardbob_buzzard

Glad to see you got there.