locked
PickList Default New Opportunity RRS feed

  • Question

  • Hi,

    I would like to know if I can select a default value for a picklist when my user opens a new opportunity.

    I have a Picklist Country (this is a list of countries) and  I created 2 teams UK and FR, I would like that : when an user of the UK team opens a new opportunity windows that the country picklist is selected to United Kingdom and when an user of FR creates a new opportunity that France is the default value.

    I created my javascript on the onLoad on the Opportunity form but is not working, I think is not the same function that is load when it is a brand new opportunity.

    Thanks

    David

    Thursday, November 18, 2010 10:05 PM

Answers

  • There is only one OnLoad event for each entity, David.  If it appears as if your code is not running at all, you should ensure that you've:

    1. Checked the 'Enable Script' checkbox
    2. Published the entity

    Then, debug the code:

    1. Open the IE script debugger (F12)
    2. Go to the Script tab
    3. Click on "Start Debugging"
    4. You can trigger debugging by either setting a breakpoint OR putting the keyword "debugger" in your script
    debugger;
    

    --pogo (pat)
    Thursday, November 18, 2010 10:51 PM
  • Try changing the applicable section to:

    if(inTeam) {
     if(typeName == "FR") {
      if(crmForm.all.new_countrylist.DataValue == null) {
       crmForm.all.new_countrylist.DataValue =40;
      }
     }
    
     if(typeName == "UK") {
      if(crmForm.all.new_countrylist.DataValue == null) {
       crmForm.all.new_countrylist.DataValue =74;
      }
     }
    }
    
    

    --pogo (pat)
    Friday, November 19, 2010 2:47 AM

All replies

  • There is only one OnLoad event for each entity, David.  If it appears as if your code is not running at all, you should ensure that you've:

    1. Checked the 'Enable Script' checkbox
    2. Published the entity

    Then, debug the code:

    1. Open the IE script debugger (F12)
    2. Go to the Script tab
    3. Click on "Start Debugging"
    4. You can trigger debugging by either setting a breakpoint OR putting the keyword "debugger" in your script
    debugger;
    

    --pogo (pat)
    Thursday, November 18, 2010 10:51 PM
  • sidavid:

    Please read the guidelines we have posted regarding inquiries that involve malfunctioning code.  Additional information is required to fully assess the situation you're encountering.

    When it comes to the behavior you desire, I believe a quick pseudo-code approach would be as follows:

    • Query the Owner's relationship to teams
    • Analyze the team return for association to a Picklist value
    • Set the Picklist value

    I believe you intend the behavior to operate ONLY during the following conditions:

    • On the "Create" mode of the Opportunity form
    • When the "Owner" field changes
    As far as why your code doesn't work, you'll need to post it here using the guidelines I identified above.
    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com
    Thursday, November 18, 2010 11:45 PM
    Moderator
  • Hi,

    My code is working when an user clicks on an existing opportunity (the onLoad function is enabled). I would like that the code runs when he clicks on a new opportunity.

    Thanks

    David

    Friday, November 19, 2010 12:33 AM
  • Can you post the code here?  My initial impression is that one of two methods could be preventing the functionality:

    1. An "if" statement that conditionally calls the code based on the crmForm.FormType
    2. An ancillary call to the Web-Services (perhaps with RetrieveMultiple, or FetchXML) that links through the Opportunity record
    The first is easy to fix.  The second may be a little more difficult, but not impossible.  The reason the second method would fail is because the Opportunity record doesn't actually exist until it's been "saved" back to the database.
    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com
    Friday, November 19, 2010 12:41 AM
    Moderator
  • Thanks in advance. This is the code in the onLoad function of my opportunity form:

    //new_countrylist is a picklist with countries

    var team1 = getTeam();
    var inTeam = false;
    for(var y=0; y<team1.length; y++)
    {
        switch(team1[y])
        {
            case "FR teams" : typeName = "FR"; inTeam = true; break;
            case "UK teams" : typeName = "UK"; inTeam = true; break;
            case "0" : return;
        }
    }

    if(inTeam)
    {
    if(typeName == "FR")
    {
    if(crmForm.all.new_countrylist.IsNull)
    {
    crmForm.all.new_countrylist.DefaultValue =40;
    }
    }
    if(typeName == "UK")
    {
    if(crmForm.all.new_countrylist.IsNull)
    {
    crmForm.all.new_countrylist.DefaultValue =74;
    }
    }
    }

    function getTeam(){
    return all the teams that the current user is member.
    return result;
    }

    Like I said the code works on existing opportunities

    Friday, November 19, 2010 1:01 AM
  • Have you ascertained that "getTeam()" is both being executed and returning an appropriate value?  OnLoad doesn't conditionally execute, if it's enabled, it runs regardless of the state or stage of the form.  If a simple alert() statement in the OnLoad code produces an alert dialog, then the code is running.
    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com
    Friday, November 19, 2010 1:28 AM
    Moderator
  • David, This is the code of getteam:

    function getTeam(){
    var xml = "" +
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
    GenerateAuthenticationHeader() +
    " <soap:Body>" +
    " <RetrieveMultiple xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
    " <query xmlns:q1=\"http://schemas.microsoft.com/crm/2006/Query\" xsi:type=\"q1:QueryExpression\">" +
    " <q1:EntityName>team</q1:EntityName>" +
    " <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
    " <q1:Attributes>" +
    " <q1:Attribute>name</q1:Attribute>" +
    " </q1:Attributes>" +
    " </q1:ColumnSet>" +
    " <q1:Distinct>false</q1:Distinct>" +
    " <q1:LinkEntities>" +
    " <q1:LinkEntity>" +
    " <q1:LinkFromAttributeName>teamid</q1:LinkFromAttributeName>" +
    " <q1:LinkFromEntityName>team</q1:LinkFromEntityName>" +
    " <q1:LinkToEntityName>teammembership</q1:LinkToEntityName>" +
    " <q1:LinkToAttributeName>teamid</q1:LinkToAttributeName>" +
    " <q1:JoinOperator>Inner</q1:JoinOperator>" +
    " <q1:LinkCriteria>" +
    " <q1:FilterOperator>And</q1:FilterOperator>" +
    " <q1:Conditions>" +
    " <q1:Condition>" +
    " <q1:AttributeName>systemuserid</q1:AttributeName>" +
    " <q1:Operator>EqualUserId</q1:Operator>" +
    " </q1:Condition>" +
    " </q1:Conditions>" +
    " </q1:LinkCriteria>" +
    " </q1:LinkEntity>" +
    " </q1:LinkEntities>" +
    " </query>" +
    " </RetrieveMultiple>" +
    " </soap:Body>" +
    "</soap:Envelope>" +
    "";
    var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
    xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);
    var resultXml = xmlHttpRequest.responseXML;
    //alert(resultXml.xml);

    // Save all entity nodes in an array.
    var entityNodes = resultXml.selectNodes("//RetrieveMultipleResult/BusinessEntities/BusinessEntity");
    var entityNode;
    var teamidNode;
    var teamNode;
    var teamid;
    var team;
    var result= new Array();

    for (var i = 0; i < entityNodes.length; i++) {
        // Access the current array element
        entityNode = entityNodes[i];
        teamidNode = entityNode.selectSingleNode("q1:teamid");
        teamNode = entityNode.selectSingleNode("q1:name");
        // Always check for null values.   
        teamid = (teamidNode == null) ? null : teamidNode.text;
        team = (teamNode == null) ? null : teamNode.text;
        result[i] = team;
    }

    return result;
    }

    How can I have this code executed when my users open a brand new opportunity??

    Friday, November 19, 2010 2:07 AM
  • Try changing the applicable section to:

    if(inTeam) {
     if(typeName == "FR") {
      if(crmForm.all.new_countrylist.DataValue == null) {
       crmForm.all.new_countrylist.DataValue =40;
      }
     }
    
     if(typeName == "UK") {
      if(crmForm.all.new_countrylist.DataValue == null) {
       crmForm.all.new_countrylist.DataValue =74;
      }
     }
    }
    
    

    --pogo (pat)
    Friday, November 19, 2010 2:47 AM
  • I think pogo is on to something.  I must say that bit of code escaped my ocular parser.
    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com
    Friday, November 19, 2010 3:43 AM
    Moderator