General Technical Discussions for Service

Get Involved. Join the Conversation.

Topic

    John L
    Get/Set Incident Custom Field by Field Name in Add-inAnswered
    Topic posted June 6, 2017 by John LGold Medal: 3,500+ Points 
    269 Views, 4 Comments
    Title:
    Get/Set Incident Custom Field by Field Name in Add-in
    Content:

    Hi all, 

    I don't have any experience in .net development, but am posting this on behalf of a fellow developer. Basically he has a custom field in Incident that he is getting and setting by the field's ID, however he'd like to accomplish this by the field name instead so that when this add-in is deployed in our other sites, it will function properly. Will he have to have some ROQL to query by the field by name to obtain the id or is there a way to use the field name directly to set the value of this custom field? I've included a code snippet below.

    Thanks in advance!

    Version:
    May 2016
    Code Snippet:

    Best Comment

    Anurag Mittal

    You can get a list of all custom fields by using GetOptlist function. Then loop over it to get ID of specific custom field by label and use that ID to set field value. See below code snippet

                IList<IOptlistItem> CustomFieldOptList = _globalContext.GetOptlist(92);//92 returns an OptList of custom fields in a hierarchy
                foreach (IOptlistItem CustomField in CustomFieldOptList)
                {
                    if (CustomField.Label == "POC Text")//Custom Field Name
                    {
                        MessageBox.Show(CustomField.ID.ToString());//Get Custom Field ID
                    }
                }

    Comment

     

    • Anurag Mittal

      You can get a list of all custom fields by using GetOptlist function. Then loop over it to get ID of specific custom field by label and use that ID to set field value. See below code snippet

                  IList<IOptlistItem> CustomFieldOptList = _globalContext.GetOptlist(92);//92 returns an OptList of custom fields in a hierarchy
                  foreach (IOptlistItem CustomField in CustomFieldOptList)
                  {
                      if (CustomField.Label == "POC Text")//Custom Field Name
                      {
                          MessageBox.Show(CustomField.ID.ToString());//Get Custom Field ID
                      }
                  }

    • John L

      That worked perfectly. Thanks, Anurag!

    • Danette Beal

      How do we get this Developer on the community??? John, be sure to pass along how fantastic the Service Cloud Community has so we can get some new people on here! 

      Glad Anurag could help! 

      Danette, Community Manager

    • Ryan Young

      @Anurag,

      This is excellent!  Is there another Optlist which lists the names/labels of the ParentID values returned in your example?

       

      Thank you,

      Ryan Young