Quantcast
Channel: CRM Development 論壇
Viewing all articles
Browse latest Browse all 10280

Check if record exists else create a new record ,crm 2011 plugin

$
0
0

Hi Guys,

I have 2 entities.Entity A and Entity B.

I have written plugin to update a record in Entity A.This plugin also checks if there is a record with same name in Entity B.If yes it updates the record in Entity B also.

Problem

But if the record does not exists,i want  to create a new record with same name in Entity B.

I am struck here.Below is my code.Help me out in doing this.

Where should i insert the code for this.

Regards

Mrpathan

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
namespace Plugin2._1
{
    public class Class1:IPlugin
    {
        //opp
        const string _opp_name = "name";
        const string _opp_owner1 = "new_ownerr1";
        const string _opp_stdate = "new_startdate";
        const string _opp_edate = "new_projectenddate";
        const string _opp_estimatedvalue = "estimatedvalue";
        const string _opp_actualvalue = "actualvalue";
        const string _opp_logical = "opportunity";
        const string _opp_robname = "new_robname";

        //Entity: Account receivables
        const string _AcRec_logical = "new_receivable";
        const string _AcRec_PrjNm = "new_projectname";
        const string _AcRec_ARecv = "new_areceived";
        const string _AcRec_APend = "new_apending";

        //Entity:Project Resource
        const string _pr_logical = "new_projectstatus";
        const string _pr_createdon = "createdon";
        const string _pr_robname = "new_consultant2";
        const string _pr_pname = "new_exampleprojectid";

        // Entity: Pl
        const string _pl_logical = "new_pl";
        const string _pl_name = "new_name";
        const string _pl_acollected = "new_acollected";
        const string _pl_apending = "new_apending";
        const string _pl_ctc = "new_cctc";
        const string _pl_ownername = "new_ownername";
        const string _pl_tcost = "new_tcost";
        const string _pl_to = "new_to";
        const string _pl_from = "new_from";
        const string _pl_daysworked = "new_testint";
        const string _pl_mctc = "new_mctc";
        const string _pl_testdm = "new_testdm";

       
        public enum EntityFilters { }
         public void Execute(IServiceProvider serviceProvider)
        {
             IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
        IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        ITracingService itc = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
        IOrganizationService service = (IOrganizationService)factory.CreateOrganizationService(context.UserId);
            
            

            Entity retrive = null;


            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {

                if (context.Depth > 1)
                {
                    return;
                }

                  else if (context.PrimaryEntityName == _opp_logical && (context.MessageName == "Create" || context.MessageName == "Update"))
                {

                    try
                    {




                        ColumnSet col = new ColumnSet(true);

                        retrive = service.Retrieve(context.PrimaryEntityName, context.PrimaryEntityId, col);
var pname = retrive.Attributes[_opp_name];
                        var robname = retrive.Attributes[_opp_robname];

                        ConditionExpression ce1 = new ConditionExpression();
                        ce1.AttributeName = _pl_name;
                        ce1.Operator = ConditionOperator.Equal;
                        ce1.Values.Add(pname);

                        FilterExpression fe1 = new FilterExpression();
                        fe1.Conditions.Add(ce1);

                        QueryExpression qe1 = new QueryExpression(_pl_logical);
                        qe1.ColumnSet = col;

                        EntityCollection result1 = service.RetrieveMultiple(qe1);
var pname = retrive.Attributes[_opp_name];
                        var robname = retrive.Attributes[_opp_robname];

                        ConditionExpression ce1 = new ConditionExpression();
                        ce1.AttributeName = _pl_name;
                        ce1.Operator = ConditionOperator.Equal;
                        ce1.Values.Add(pname);

                        FilterExpression fe1 = new FilterExpression();
                        fe1.Conditions.Add(ce1);

                        QueryExpression qe1 = new QueryExpression(_pl_logical);
                        qe1.ColumnSet = col;

                        EntityCollection result1 = service.RetrieveMultiple(qe1);
if (newwPl[_pl_name].ToString() == pname.ToString())
                            {
 newwPl["new_name"] = retrive["name"];
                                newwPl["new_erevenue"] = retrive["estimatedvalue"];
                                // newwPl["new_from"] = retrive["new_oner1joined"];
                                newwPl["new_to"] = retrive["new_projectenddate"];
                                newwPl["new_arevenue"] = retrive["actualvalue"];
                                newwPl["new_rob2"] = retrive["new_robname2"];
                                newwPl["new_rob3"] = retrive["new_robname3"];
                                //newwPl["new_ownername"] = retrive["new_robname"]; 
                                if (retrive["new_robname"].ToString() == null)
                                { newwPl["new_ownername"] = "."; }
                                else
                                {
                                    newwPl["new_ownername"] = retrive["new_robname"];
                                    //newwPl["new_rob2"] = retrive["new_robname2"];
                                    // newwPl["new_rob3"] = retrive["new_robname3"];
                                }
                                ConditionExpression ce2 = new ConditionExpression();
                                ce2.AttributeName = _pl_name;
                                ce2.Operator = ConditionOperator.Equal;
                                ce2.Values.Add(_opp_name);

                                FilterExpression fe2 = new FilterExpression();
                                fe2.Conditions.Add(ce2);

                                QueryExpression qe2 = new QueryExpression(_pr_logical);
                                qe2.ColumnSet = col;

                                EntityCollection result2 = service.RetrieveMultiple(qe2);



                                int count = 0;
                                Entity pr = null;

                                for (int j = 0; j < result2.Entities.Count; j++)
                                {
                                    pr = result2.Entities[j];
                                    EntityReference ownerLookup = (EntityReference)pr.Attributes[_pr_pname];
                                    var _prpname = ownerLookup.Name;

                                    if (newwPl[_pl_name].ToString() == _prpname.ToString())
                                    {
                                        count++;
                                        if (count == 2)
                                        {
                                            newwPl["new_rob2joined"] = pr[_pr_createdon];
                                        }

                                        else
                                            //if(result2.Entities.coun
                                            //for (int o = 0; o < 3; o++)
                                            //{
                                            //    if (o <= 1)
                                            newwPl[_pl_from] = pr[_pr_createdon];
                                        //if (o > 1 && o < 3)

                                    }
                                    //if(newwPl[_pl_name].ToString ()==_prpname[j+1].ToString())
                                    //{

                                    //        newwPl["new_rob2joined"] = pr[_pr_createdon];

                                    //}
                                }

                                //}
                                service.Update(newwPl);
                            }
 }

                    catch (Exception ex)
                    {
                        throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
                    }
                }

            }

        }
}
}




Viewing all articles
Browse latest Browse all 10280

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>