Link to home
Start Free TrialLog in
Avatar of 2_under_par
2_under_parFlag for United States of America

asked on

Auto populate Contract Template (lookup field) on FormLoad

When working with the Contract entity, the CONTRACT TEMPLATE field is required.  Since we're not using that field, I'd like to have the form automatically populate it with "Service".  (the only value available)

I tried the following 2 pieces of code and neither worked.  Any Suggestions?

if (crmForm.all.contracttemplateid.SelectedText == null)
    {
     crmForm.all.contracttemplateid.SelectedText == "Service";
    }

if (crmForm.all.contracttemplateid.DataValue == null)
    {
     crmForm.all.contracttemplateid.DataValue == "Service";
    }
Avatar of Jeff Wight
Jeff Wight
Flag of United States of America image

How are you creating a contract record without choosing the type first?  I thought that was a mandatory part of creating a contract.  Was data imported into this entity?


The contract type is a related entity.  Adding a value to a lookup field can be done with javascript like this:

var contractTemplate = new Array(new Object());
      contractTemplate[0].id = "9E1B393B-AAD6-DD11-8632-005056867069";
      contractTemplate[0].name = "SER";
      contractTemplate[0].type = 2011;
      contractTemplate[0].typename = "contracttemplate";
                                    
crmForm.all.contracttemplateid.AddItems(lookupCampaignID);

In order to use this, you'll have to get the guid for the "Service" contract template (if you open the template itself and then click F11, the value inside the {} in the page address is the GUID - "http://crm/tools/contracttypemanager/edit.aspx?id={54D8EE69-9B4F-4966-BB2C-581E3453DDE7}" has a guid of 54D8EE69-9B4F-4966-BB2C-581E3453DDE7).  Put this value, minus the brackets, into the snippet above and it should put the Service contract value into the ContractId field.  You'll still want to check and see if there is a value there already first.

Avatar of 2_under_par

ASKER

"How are you creating a contract record without choosing the type first?"  Good Question....  

Within the Opportunity Entity, I created a 1:N Relationship between Opportunities & Contracts.  Then, I linked some custom fields that we are using in both Entities.  So, within an open Opportunity, you can click on the Linked Contract Menu on the left.  When doing so, you may create a new Contract and all of the Mapped custom fields are Imported into the new Contract.  This process bypasses the screen to select a Contract Template, but I can't autopopulate it.  

I was able to get the GUID of the Contract Template by opening it up & doing a CTRL - N.  I'm not good with Arrays, though... and that last line threw me a bit.  Forgive me, but this is what I've got so far...

if (crmForm.all.contracttemplateid.SelectedText == null)
    {
     var contractTemplate = new Array(new Object());
      contractTemplate[0].id = "77ECDFDA-3E70-429D-B174-1352B981C5F2";
      contractTemplate[0].name = "SER";
      contractTemplate[0].type = 2011;
      contractTemplate[0].typename = "contracttemplate";
                                   
      crmForm.all.contracttemplateid.AddItems(lookupCampaignID);
    }

I'm assuming there's something obvious that I need to replace in the code above?
ASKER CERTIFIED SOLUTION
Avatar of Jeff Wight
Jeff Wight
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Any luck getting this to work?
Thanks jd_wright, that worked great!  I had an issue with another line of code that kept this from working.  After removing the bad code, this worked pefectly fine.  

I did change SER to Service in my final code, but your code worked also.  So, I'm giving you all the points.  

if (crmForm.all.contracttemplateid.SelectedText == null)
    {
     var contractTemplate = new Array(new Object());
      contractTemplate[0].id = "77ECDFDA-3E70-429D-B174-1352B981C5F2";
      contractTemplate[0].name = "Service";
      contractTemplate[0].type = 2011;
      contractTemplate[0].typename = "ContractTemplate";
                                   
      crmForm.all.contracttemplateid.AddItems(contractTemplate);
    }
Autopopulate Contract Template