locked
CRM 2011 on premise -Service Activity - set default SiteId.. RRS feed

  • Question

  • Hi there.  On the Service Activity form, I would like the SiteId field to be automatically populated with our Site ID (we only have one). At the moment, the user has to select this manually. I am aware you can do it on load form but I am unsure what the code would be. If someone could help me I would appreciate it as I am not that offay with coding.

    Thanks


    BM

    Wednesday, March 7, 2012 11:38 AM

Answers

  • Thanks for the help and input Kirsten, appreciate that. I eventually ended up acheiving this via onload of form as below. One can get the Site GUID from the SQL Management Studio, Tables, SiteBase.

    function service_activity_1()
    {
      if(crmForm.FormType ==1)
         {
    //Create an array to set as the DataValue for the lookup control.
     var lookupData = new Array();
     //Create an Object add to the array.
     var lookupItem= new Object();
     //Set the id, typename, and name properties to the object.
     lookupItem.id = '{ff7e3775-6217-e111-83b7-005056840008}';
     lookupItem.typename = 'siteid';
     lookupItem.name = 'type_here_your_site_name';
     // Add the object to the array.
     lookupData[0] = lookupItem;
     // Set the value of the lookup field to the value of the array.
     crmForm.all.siteid.DataValue = lookupData;
          }
    }


    BM

    • Marked as answer by BillyBoy9991 Friday, March 9, 2012 10:58 AM
    Friday, March 9, 2012 10:58 AM

All replies

  • Hi,

    If you have the id and name of the site, you can use this function:

    function setLookup(attributeName, recordId, recordType, recordName) {
        var attribute = Xrm.Page.getAttribute(attributeName);
        var newValue = new Array();
        var newLookup = new Object();
        newLookup.id = recordId;
        newLookup.entityType = recordType;
        recordName.name = recordName;
    
        newValue[0] = newLookup;
        attribute.setValue(newValue);
    }

    So you could call it using:

    setLookup("siteid","{xxxxx-xxxxxx-xxxxxxxx}","site","My Site");

    Kind regards

    Kirsten

    • Proposed as answer by D kay Wednesday, March 7, 2012 1:30 PM
    Wednesday, March 7, 2012 12:33 PM
  • Thanks for the help and input Kirsten, appreciate that. I eventually ended up acheiving this via onload of form as below. One can get the Site GUID from the SQL Management Studio, Tables, SiteBase.

    function service_activity_1()
    {
      if(crmForm.FormType ==1)
         {
    //Create an array to set as the DataValue for the lookup control.
     var lookupData = new Array();
     //Create an Object add to the array.
     var lookupItem= new Object();
     //Set the id, typename, and name properties to the object.
     lookupItem.id = '{ff7e3775-6217-e111-83b7-005056840008}';
     lookupItem.typename = 'siteid';
     lookupItem.name = 'type_here_your_site_name';
     // Add the object to the array.
     lookupData[0] = lookupItem;
     // Set the value of the lookup field to the value of the array.
     crmForm.all.siteid.DataValue = lookupData;
          }
    }


    BM

    • Marked as answer by BillyBoy9991 Friday, March 9, 2012 10:58 AM
    Friday, March 9, 2012 10:58 AM
  • Hi Billy,

    Are you using CRM 4.0 or CRM 2011? The code above you are using is using the old CRM 4.0 javascript object model. It is recommended to use the new JavaScript Object Model (Xrm.Page) for CRM 2011 to future-proof against new updates.

    For converting from CRM 4.0 javascript to CRM 2011 I can recommend Rhett Clinton's tool: http://crm2011scriptconvert.codeplex.com/

    Kind regards

    Kirsten Clapham

    Friday, March 9, 2012 11:05 AM
  • Thanks Kirsten! I am not really a Developer, thank you for your very helpful advice, I really appreciate it. A great tool tool! I have downloaded it and converted the old script. Interestingly, when the code fires, the lookup works fine but it does not display correctly in the field, its sort of half way and the graphic that is associated with the site icon is not fully displayed correctly. When the record is saved and reopened, its fine though. Any thoughts on that?

    Thanks


    BM

    Friday, March 9, 2012 11:22 AM
  • hi Kirsten

    i want ur help.....my posted my problem

    http://social.microsoft.com/Forums/en/crmdevelopment/thread/5bdfce4a-e1c5-4e95-8396-363b3cb33553

    please just check it out ....


    Ayaz

    Friday, March 9, 2012 11:25 AM