Joseph Michael Pesch
VP Programming

Sample Code Re-write with Generics

by 8. November 2010 15:18

    public string GetCampaign(string campaigncode)

    {

      System.Diagnostics.Debug.WriteLine("inside GetCampaign");

 

      // NEW

      List<campaign> camp = Crm.GetEntityList<campaign>(Crm.BuildStringQuery(

        EntityName.campaign.ToString(), "codename", ConditionOperator.Equal, campaigncode

        , LogicalOperator.And, new string[] { "campaignid", "codename", "name" }));

      return camp.Count == 1 ? camp[0].campaignid.Value.ToString() : "";

 

      // OLD

      ColumnSet cols = new ColumnSet();

      cols.Attributes = new string[] { "campaignid", "codename", "name" };

 

      ConditionExpression[] condition = new ConditionExpression[1];

      condition[0] = new ConditionExpression();

      condition[0].AttributeName = "codename";

      condition[0].Operator = ConditionOperator.Equal;

      condition[0].Values = new string[] { campaigncode };

 

      FilterExpression filter = new FilterExpression();

      filter.FilterOperator = LogicalOperator.And;

      filter.Conditions = condition; //new ConditionExpression[] {condition};

      QueryExpression query = new QueryExpression();

      query.EntityName = EntityName.campaign.ToString();

      query.ColumnSet = cols;

      query.Criteria = filter;

 

      // Retrieve the ServiceAppointments

      BusinessEntityCollection lines = Crm.GetCrmService().RetrieveMultiple(query);

 

      //retrieve into an object

 

      int NumOfLines = lines.BusinessEntities.Length;

      string campid = "";

      System.Diagnostics.Debug.WriteLine("numoflines = " + NumOfLines);

      campaign[] campretrieved = new campaign[NumOfLines];

 

      if (NumOfLines > 1 || NumOfLines < 1)

      {

        System.Diagnostics.Debug.WriteLine("more than one record matched");

        campid = "";

      }

      else

      {

        System.Diagnostics.Debug.WriteLine("found matching contact record");

        int i = 0;

        campid = "";

 

        while (i < 1)

        {

          campretrieved[i] = (campaign)lines.BusinessEntities[i];

          campid = campretrieved[i].campaignid.Value.ToString();

          i++;

        }

      }

      //System.Diagnostics.Debug.WriteLine("campid = " + campid);

      return campid;

    }

 

Tags:

[None]

Comments are closed