You need to sign in to do that
Don't have an account?

Get data from parent object
Hi,
I'm totally newbie on Sales force, and I'm trying something that should be easy.
From a Case, I want to manage a new type of object, named SMS
I created the object and now I want to customize the page, with APEX
So, I created a class
global class NetsizeClass
{
private SMS__c currentSms;
public SMS__c getSms()
{
return currentSms;
}
public NetsizeClass(ApexPages.StandardController controller)
{
if (currentSms == null)
currentSms = new SMS__c();
string sms_id = System.currentPageReference().getParameters().get('id');
if (sms_id == null || sms_id == '')
{ // New SMS
currentSms.message__c = 'New SMS ';
}
else
currentSms= [select message__c, status__c, case__c, contact__c
from SMS__c where id = :sms_id];
}
public void SendSMS()
{
if (currentSms.id == null || currentSms.id == '')
// New SMS
else
// Existing SMS
upsert currentSms;
...
}
}
and a Page, that overload the 'New' button in my SMS object
<apex:page standardController="SMS__c" extensions="NetsizeClass"> <apex:sectionHeader title="New SMS" subtitle=""/> <apex:form> <apex:pageBlock title="SMS information"> <apex:pageBlockButtons > <apex:commandButton value="Save" action="{!save}"/> <apex:commandButton value="Send SMS" action="{!SendSMS}"/> </apex:pageBlockButtons> <TABLE> <TR> <TD><B>Message</B></TD> <TD><apex:inputField value="{!sms.Message__c}" id="smsMsg"/></TD> </TR> <TR> <TD><B>Contact</B></TD> <TD><apex:inputField value="{!sms.Contact__c}" id="smsContact"/></TD> </TR> <TR> <TD><B>Case</B></TD> <TD><apex:inputField value="{!sms.Case__c}" id="smsCase"/></TD> </TR> <TR> <TD><B>Case 2</B></TD> <TD><apex:inputField value="{!SMS__c.Case__c}" id="smsCase2"/></TD> </TR> </TABLE> <br/> </apex:pageBlock> </apex:form> </apex:page>
It works fine, when I am on a Case, I see the button 'New SMS' (I customized the layout), that open my custom APEX page.
But I just don't know how to retreive the parent Case information (I'm using the 2 fields Case for my tests):
- the field smsCase is empty, and it's normal as it is linked to {!sms.Case__c} and sms is created in the constructor. But I read in some forums that it's the only way to retreive later the updated values (I want to customize the Edit and View page)
- the field smsCase2 is filled with the id of my Case, but I don't know how to retreive this data from my page.
I also would like to retreive automatically the contact linked in the parent Case, to link it to the SMS. But I think it should be easy to do when I'll have the ID of my Case.
I'm sure that I'm missing something but I searched in so much forums that I'm starting to mix-up everything.
Any help will be welcome :)
Thanks
Hervé
Hey
This field,
<apex:inputField value="{!SMS__c.Case__c}" id="smsCase2"/>
Is using the standard controller's instance of the object. Whereas this field,
apex:inputField value="{!sms.Case__c}" id="smsCase"/>
Is using your extensions instance of the object. You need to link the two. This can be done more or less like this,
(sorry about the code getting squashed up. When I click 'Post' it scrunches it up.)
// New SMS
else
// Existing SMS
upsert currentSms;
... }
}
On your page, try to not mix {!SMS__c.field} and {!sms.field}. I know you're probably doing some troubleshooting, but just for future reference;) Otherwise saving can get tricky.
Cheers,
Wes
All Answers
Hey
This field,
<apex:inputField value="{!SMS__c.Case__c}" id="smsCase2"/>
Is using the standard controller's instance of the object. Whereas this field,
apex:inputField value="{!sms.Case__c}" id="smsCase"/>
Is using your extensions instance of the object. You need to link the two. This can be done more or less like this,
(sorry about the code getting squashed up. When I click 'Post' it scrunches it up.)
// New SMS
else
// Existing SMS
upsert currentSms;
... }
}
On your page, try to not mix {!SMS__c.field} and {!sms.field}. I know you're probably doing some troubleshooting, but just for future reference;) Otherwise saving can get tricky.
Cheers,
Wes
Wonderful, it's exactly what I was looking for ! :)
I didn't think to have a look to the 'controller' object. I'm going to check its property right now.
Don't worry, I'll remove the field linked to {!SMS__c.field}. I was using it just for my tests.
Thanks a lot for your quick support !! :D