+ Start a Discussion
SeaTecSeaTec 

Linking custom field in Contracts object to custom object

Here is the scenario.  We have a field called Lease Number in the contract object.  I also have a custom object called buyout which i populate with apex data loader with fresh data on a monthly basis. The Buyout object also has a lease number field.  What needs to happen is that either by clicking on the lease number field in the contract object or by clicking a custom button or link on the contract object the corresponding record in the buyout object needs to open. Whats the easiest way of accomplishing this.  Not versed in developing s-controls.

 

Thank you

 

Anthony

Best Answer chosen by Admin (Salesforce Developers) 
ClintLeeClintLee

You don't need to populate the Name field.  Just remove it.

 

Buyout__c b = new Buyout__c();

All Answers

ClintLeeClintLee

You should be able to achieve this with these three components.  Create a custom controller apex class, a visualforce page, and a custom button on the Contract page.

 

The button links to the Visualforce page and passes in a parameter named leasenumber.  The page's controller uses the parameter to find the corresponding Buyout object.

 

 

Controller: ContractBuyout

 

public class ContractBuyout {

            public Buyout__c buyout  { get; set; }            

 

            pubilc ContractBuyout( ) {

                         buyout = [ select Id from Buyout__c where Lease_Number__c = :ApexPages.currentPage().getParameters().get( 'leasenumber' ) limit 1 ];

            }

 

            static testmethod void testOne() {

                       Buyout__c b = new Buyout__c( Name = 'Test' );

                       insert b;

 

                       PageReference pageRef = Page.ContractBuyout;

                       pageRef.getParameters().put( 'leasenumber', b.Id );

                       Test.setCurrentPage( pageRef );

                       ContractBuyout controller = new ContractBuyout();

            

                       System.assertEquals( b.Id, controller.buyout.Id );

            }

}

 

 

VisualForce Page: ContractBuyout.page

 

<apex:page controller="ContractBuyout">

    <apex:detail subject="{!buyout.Id}" relatedList="true" />

</apex:page>

 

Custom Button on Contract:

 

formula = /apex/ContractBuyout?leasenumber={!Buyout__c.Lease_Number__c}

 

Hope that helps!

 

~ Clint

SeaTecSeaTec

THis will help immensely. Having troubel finding where i can actually create an apex controller class though.

SeaTecSeaTec

Also,  what kind of Custom buttin does this need to be?

Java,  custom S  or  VIsual force page

 

THanks

SeaTecSeaTec

I'm in my sandbox to create the apex class under "Develop/apex classes and get this error when i try to save your code

 

Error: Compile Error: sObject type 'Buyout__c' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names. at line 9 column 37

 

 

Do i first have to create the object in the sandbox   in order to create the controller?

 

 

THanks

SeaTecSeaTec

Worked through the previous errors but now I'm getting this error when trying to save the class.  Error: Compile Error: Field is not writeable: Buyout__c.Name at line 17 column 60  where it says "  Buyout__c b = new Buyout__c( Name = 'Test' );

 

I have created the custom object named Buyout and added the field Lease_Number

ClintLeeClintLee

If you're using auto-numbering for the Name then you don't need that statement, Name = ''.  I just added it for the example since I didn't know your object or field names.

 

Make your button a Detail Page button, Content Source: URL.

 

~ Clint

SeaTecSeaTec

i use auot number and changed your Name -='Test'  to Name = ''  and still get the same error.

ClintLeeClintLee

You don't need to populate the Name field.  Just remove it.

 

Buyout__c b = new Buyout__c();

This was selected as the best answer
SeaTecSeaTec

I'll try that.

SeaTecSeaTec

THat seemed to have worked. Because I don't have the contract object in my sandbox i guess i have to move this new class and page to my production environment now to test it.  What's a good way for doing that?  THanks

ClintLeeClintLee

Are you referring to the standard Contract object?  That's a standard object so it should be available in the sandbox.

SeaTecSeaTec

I am looking around and cannot find the standard contract object.  Maybe HQ renamed it

SeaTecSeaTec

What's an easy way to transfer this class to my production environment?    Do I have to use the force?  ;)  I know. Bad joke.

ClintLeeClintLee

If you're using Eclipse, you can right-click the class and page and select Deploy to Server.  Then, enter your credentials to your Production account.

 

Otherwise, you can create a change set and add the items to it.  Then, deploy the change set to production.  If you haven't used change sets or you don't have a link set up between your production and sandbox accounts you'll probably want to read up on how to use them.  Look up "Change Sets" in the online help documentation.

 

~ Clint

SeaTecSeaTec

I'll have to go the change set route. set it up on the sandbox side but am getting an error This organization isn't authorized to upload change sets to other organizations. For authorization, contact the deployment connections administrators on the organizations where you want to upload changes

 

and am having a hard time figuring out what the problem is.

 

I am sys admin in the target production environment and have the right profile access.  

ClintLeeClintLee

You have to do set up on both sides, production and sandbox.

 

Did you read through the online help info?  Try watching this video - http://www.salesforce.com/_app/video/chatter/help/change_sets.jsp

SeaTecSeaTec

Yes.  got it figured out.  thanks

SeaTecSeaTec

if i wanted to place a dummy lease number value in the apex class code  where would i put it?

SeaTecSeaTec

Why woud i be getting this error when doign the change set to production?

ContractBuyout.testOne()Class9 Failure Message: "System.QueryException: List has no rows for assignment to SObject", Failure Stack Trace: "Class.ContractBuyout.<init>: line 9, column 1 Class.ContractBuyout.testOne: line 29, column 1"
ClintLeeClintLee

If you remove this part :ApexPages.currentPage().getParameters().get( 'leasenumber' ) and replace it with a lease number it'll find the Buyout object with that lease number.  However, you need to have an actual Buyout record with that lease number in it or the query will return null.

ClintLeeClintLee

Try replacing the test method with this one:

 

static testmethod void testOne() {

                       Buyout__c b = new Buyout__c( Lease_Number__c = '12345' );

                       insert b;

 

                       PageReference pageRef = Page.ContractBuyout;

                       pageRef.getParameters().put( 'leasenumber', b.Lease_Number__c );

                       Test.setCurrentPage( pageRef );

                       ContractBuyout controller = new ContractBuyout();

            

                       System.assertEquals( b.Id, controller.buyout.Id );

            }

SeaTecSeaTec

thank you.  Ill try that.

 

Also, can't figure which custom button type would accept   formula = /apex/ContractBuyout?leasenumber={!Buyout__c.Lease​_Number__c}  as a value

ClintLeeClintLee

Take a look at this for creating the button - http://screencast.com/t/po5fPbQUaw 

SeaTecSeaTec

Ran a Test with the code below and no errors.  the hard coded lease number is of course present in the buyout object

 

 

public class ContractBuyout {

 

            public Buyout__c buyout  { get; set; }           

 

 

 

            public ContractBuyout( ) {

 

                         buyout = [ select Id from Buyout__c where Lease_Number__c = :ApexPages.currentPage().getParameters().get( 'leasenumber' ) limit 1 ];

 

            }

 

 

 

            static testmethod void testOne() {

 

                       Buyout__c b = new Buyout__c( Lease_Number__c = 'H1245799' );

 

                       insert b;

 

 

 

                       PageReference pageRef = Page.ContractBuyout;

 

                       pageRef.getParameters().put( 'leasenumber', b.Lease_Number__c );

 

                       Test.setCurrentPage( pageRef );

 

                       ContractBuyout controller = new ContractBuyout();

 

           

 

                       System.assertEquals( b.Id, controller.buyout.Id );

 

            }

 

}

 

SeaTecSeaTec

ok.  created the button and populated one of the lease fields wiht an existing value in object buyout.  when i click the button i get this error

 

 

 

List has no rows for assignment to SObject

 

An unexpected error has occurred.  Your development organization has been notified.   

 

THis si the code im running in the apex object

 

public class ContractBuyout {

 

            public Buyout__c buyout  { get; set; }           

 

 

 

            public ContractBuyout( ) {

 

                         buyout = [ select Id from Buyout__c where Lease_Number__c = :ApexPages.currentPage().getParameters().get( 'leasenumber' ) limit 1 ];

 

            }

 

 

 

            static testmethod void testOne() {

 

                       Buyout__c b = new Buyout__c( Lease_Number__c = 'H1245799' );

 

                       insert b;

 

 

 

                       PageReference pageRef = Page.ContractBuyout;

 

                       pageRef.getParameters().put( 'leasenumber', b.Lease_Number__c );

 

                       Test.setCurrentPage( pageRef );

 

                       ContractBuyout controller = new ContractBuyout();

 

           

 

                       System.assertEquals( b.Id, controller.buyout.Id );

 

            }

 

}

 


ClintLeeClintLee

Try changing your formula to this - /apex/ContractBuyout?leasenumber={!Lease​_Number__c}

 

or

 

/apex/ContractBuyout?leasenumber={!Contract.Lease​_Number__c}

 

 

SeaTecSeaTec

won't let me do that. Syntax error.

 

 

This is the actual email i get regarding the error

 

Sandbox

 

Apex script unhandled exception by user/organization: 00550000000oGYP/00DP00000005qEP Source organization: 00D300000001Qaf (null) Visualforce Page: /apex/ContractBuyout

 

 

 

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

 

Class.ContractBuyout.<init>: line 9, column 1

SeaTecSeaTec

Got it working.  thank you very much for your help

SeaTecSeaTec

lol.  You are not goign to believe this. I was just informed that we are downgrading from enterpsie top professional. All thia work for nothing since Professional  does nto support apex classes.