locked
Plug-in for creating contact RRS feed

  • Question

  • Hi,

    I have created a plug-in which perform the below action.

    If i create a account a new contact will be automatically created with the default name. It's working fine for me.

    My question is i want the new  contact to be created with the same name i am creating the account. How can i do this??

     contact contactobj = new contact();

                    // Set the Property for the Entity
                    account accountobj = new account();
                    contactobj.lastname = "Kaleeswari";
                    contactobj.jobtitle = "Trainee";
                    contactobj.firstname = "Arumugam";

    i have used the above code. so if i create an account a new contact will be automatically created with the name ArumuganKaleeswari.

    But i want to create a contact with the same name as the account which i am creating.

    What can i do for that??

    Please any one help me.

    Wednesday, July 7, 2010 7:29 AM

Answers

  • Hi,

    you are creating contact two times change your code like below

     contact contactobj = new contact();
    
            if (entity.Properties.Contains("name"))
            {
              contactobj.lastname = entity.Properties["name"].ToString();
            }
            contactobj.jobtitle = "Trainer";
            contactobj.firstname = "Arumugam";
    
            ICrmService service = context.CreateCrmService(true);
    
            
            //Refer to the account in the contact
            if (context.OutputParameters.Properties.Contains("id"))
            {
              contactobj.parentcustomerid = new Customer();
              contactobj.parentcustomerid.type = EntityName.account.ToString();
              contactobj.parentcustomerid.Value = new Guid(context.OutputParameters.Properties["id"].ToString());
            }
              
              Guid contactid = service.Create(contactobj);
     appointment appobj = new appointment();
            appobj.regardingobjectid = new Lookup();
            appobj.regardingobjectid.type = EntityName.contact.ToString();
            appobj.regardingobjectid.Value = contactid;
            appobj.subject = "Wedding";
    
            CrmDateTime date = new CrmDateTime();
            date.Value = new DateTime(2010,5,6).ToString();
    
            CrmDateTime date1 = new CrmDateTime();
            date1.Value = new DateTime(2010, 5, 7).ToString();
    
            appobj.scheduledstart = date;
            appobj.scheduledend = date1;
    
             
              TargetCreateAppointment targetappointment = new TargetCreateAppointment();
              targetappointment.Appointment = appobj;
    
              CreateRequest createapp = new CreateRequest();
              createapp.Target = targetappointment;
    
              CreateResponse createdapp = (CreateResponse)service.Execute(createapp);
            }
    

     


    Mahain
    • Proposed as answer by HIMBAPModerator Thursday, July 8, 2010 10:23 AM
    • Marked as answer by Mathan2631 Thursday, July 8, 2010 11:09 AM
    Thursday, July 8, 2010 10:23 AM
    Moderator

All replies

  • Hi,

    you can write plugin on post create of account entity to create contact and to set contact name you can use below code

     DynamicEntity _Entity = new DynamicEntity();
    if (context.InputParameters.Properties.Contains("Target") &&
       context.InputParameters.Properties["Target"] is DynamicEntity)
       {
        // Obtain the target business entity from the input parmameters.
        _Entity = (DynamicEntity)context.InputParameters.Properties["Target"];
    
        // Verify that the entity represents an account.
        if (_Entity.Name != EntityName.account.ToString()) { return; }
       }
       else
       {
        return;
       }
    
       
              
        contact contactObj = new contact();
        if (_Entity.Properties.Contains("name"))
        {
         contactobj.firstname = _Entity.Properties["name"].ToString();
        }
        //other code
           

     

     


    Mahain
    Wednesday, July 7, 2010 7:39 AM
    Moderator
  • Hi,

    Thanks for your reply. That worked great.

    Thanks a lot.

    I have one more question.

    Account and contact entity are related. Like wise contact and appointment entity has relationship.

    My question when i create an account automatically a new contact should be created and as a result a new appointment should be created.

    Is the above task possible through plug-in??

    I think through workflow we can achieve this.

    Is it possible completely through plug-in??

    Wednesday, July 7, 2010 9:11 AM
  • Surely you can do this, you just need to set regardingobjectid of appointment with newly created contact ID.
    Mahain
    Wednesday, July 7, 2010 9:23 AM
    Moderator
  • hi,

    Should i use like this,

    appointment appobj=new appointment();

    appobj.regardingobjid.value=contactobj.contactid.tostring();

    then create the appointment with other code??

     How should i do that along with the below code.

     DynamicEntity _Entity = new
     DynamicEntity();
    if (context.InputParameters.Properties.Contains("Target" ) &&
    context.InputParameters.Properties["Target" ] is DynamicEntity)
    {
    // Obtain the target business entity from the input parmameters.
    _Entity = (DynamicEntity)context.InputParameters.Properties["Target" ];

    // Verify that the entity represents an account.
    if (_Entity.Name != EntityName.account.ToString()) { return ; }
    }
    else
    {
    return ;
    }



    contact contactObj = new contact();
    if (_Entity.Properties.Contains("name" ))
    {
    contactobj.firstname = _Entity.Properties["name" ].ToString();

    }


    Wednesday, July 7, 2010 9:41 AM
  • you need to do like below

    1. First you need to create contact.

    you will get contact guid when you will create contact

    for example

     Guid contactGuid = service.Create(contactobj);

    2. Then create appointment and set regardingobjectid with newly created contactGuid

     


    Mahain
    Wednesday, July 7, 2010 9:46 AM
    Moderator
  • Hi,

    I have retrieved the contactid that have created like below.

    if (entity.Properties.Contains("name"))
                    {
                        contactobj.lastname = entity.Properties["name"].ToString();
                    }
                    contactobj.jobtitle = "Trainee";
                    contactobj.firstname = "Arumugam";

     Guid contactid=new Guid(context.OutputParameters ["id"].ToString());

    contact conobj = (contact)service.Retrieve(EntityName.contact.ToString(), contactid, new ColumnSet(new string[] { " contactid" }));

    Is this correct??

    How to map this to the appointment regardingobjectid??

    I tried like this...

    appointment appobj=new appointment();

    appobj.regardingobjectid=conobj.value.tostring();

    But getting error ... how to do this...

    please help me???/

    Wednesday, July 7, 2010 10:14 AM
  • Hey,

    My First Question to you have you downloaded CRM SDK ???

    I could not understand how you will get contact ID in output parameter ??? you will only get accountid in output parameter.

    when you will use create method of CRM service it will return a Guid type that contains the ID of the newly created entity.

    There is not need of using retrieve function,

    you just need to follow steps properly and logically

    1. First you need to create contact.

    you will get contact guid when you will create contact

    for example

     Guid contactGuid = service.Create(contactobj);

    2. Then create appointment and set regardingobjectid with newly created contactGuid

     


    Mahain
    Wednesday, July 7, 2010 10:24 AM
    Moderator
  • Hi,

    Sorry for my mistakes,

    Now only i am trying crm concepts with the plug-in's.

    I wrote the code for creating contact when account is created with the help of account create plugin. Just altered the entity names.

    Now i have retrieved the created contactid. But i cant assign that id directly to regardingobjectid.

    I tried like this.

    appointment appobj=new appointment();

    appobj.regardingobjectid=conobj.value.tostring();

    Showing as Cannot implicitly convert string to Microsoft.crm.sdk.lookup..

    How should i do this...

    How should i set the created contactid to regardingobjectid??

    Wednesday, July 7, 2010 11:03 AM
  • you should be able to do this as below

    Guid contactGuid = service.Create(contactobj);    //Contact record created

         appointment appobj=new appointment();

        appobj.regardingobjectid = new Lookup();
        appobj.regardingobjectid.type = EntityName.contact.ToString();
        appobj.regardingobjectid.Value = contactGuid;

     

     


    Mahain
    • Marked as answer by Mathan2631 Thursday, July 8, 2010 9:46 AM
    • Unmarked as answer by Mathan2631 Thursday, July 8, 2010 10:16 AM
    Wednesday, July 7, 2010 11:14 AM
    Moderator
  • Thanks, this what i am asking for.

    Actually i already retrieved the contactid. for setting that to regardingobjectid only i changed the code like before.

    i will try with the above code.

    thanks for your reply.

    I will try and let you know whether it's working for me...

    Wednesday, July 7, 2010 11:27 AM
  • Hi,

    If i want to update the .DLL file in the assemblyfolder of the crm server i am unable to delete the old file unless i restart the system.

    why this happens??

    i can't delete the .DLL file from that directory with out restarting ah??

     

    Wednesday, July 7, 2010 12:04 PM
  • Hi i got the result.

    It's working fine for me. Thanks for your reply.

    But the contact s getting repeated. The contact is created twice with the same name as the account.

    What will be the reason??

    Thursday, July 8, 2010 9:47 AM
  • Hey,

    Paste your code here so that we can check it


    Mahain
    Thursday, July 8, 2010 9:54 AM
    Moderator
  • Hi,

    this is the code  which i used.

    try
                {
                    // Create the dynamic entity Object
                    contact contactobj = new contact();

                    // Set the Property for the Entity
                    account accountobj = new account();
                    if (entity.Properties.Contains("name"))
                    {
                        contactobj.lastname = entity.Properties["name"].ToString();
                    }
                    contactobj.jobtitle = "Trainer";
                    contactobj.firstname = "Arumugam";

                    ICrmService service = context.CreateCrmService(true);

                    Guid contactid = service.Create(contactobj);

                    appointment appobj = new appointment();
                    appobj.regardingobjectid = new Lookup();
                    appobj.regardingobjectid.type = EntityName.contact.ToString();
                    appobj.regardingobjectid.Value = contactid;
                    appobj.subject = "Wedding";

                    CrmDateTime date = new CrmDateTime();
                    date.Value = new DateTime(2010,5,6).ToString();

                    CrmDateTime date1 = new CrmDateTime();
                    date1.Value = new DateTime(2010, 5, 7).ToString();

                    appobj.scheduledstart = date;
                    appobj.scheduledend = date1;

                    //Refer to the account in the contact
                    if (context.OutputParameters.Properties.Contains("id"))
                    {
                        contactobj.parentcustomerid = new Customer();
                        contactobj.parentcustomerid.type = EntityName.account.ToString();
                        contactobj.parentcustomerid.Value = new Guid(context.OutputParameters.Properties["id"].ToString());

                        // Create the target for the Entity
                        TargetCreateContact targetCreate = new TargetCreateContact();
                        targetCreate.Contact = contactobj;

                        // Create the request object.
                        CreateRequest create = new CreateRequest();
                        create.Target = targetCreate;

                        // Execute the request.
                      
                        CreateResponse created = (CreateResponse)service.Execute(create);

                        TargetCreateAppointment targetappointment = new TargetCreateAppointment();
                        targetappointment.Appointment = appobj;

                        CreateRequest createapp = new CreateRequest();
                        createapp.Target = targetappointment;

                        CreateResponse createdapp = (CreateResponse)service.Execute(createapp);
                    }

     

    Thursday, July 8, 2010 9:59 AM
  • Hi,

    you are creating contact two times change your code like below

     contact contactobj = new contact();
    
            if (entity.Properties.Contains("name"))
            {
              contactobj.lastname = entity.Properties["name"].ToString();
            }
            contactobj.jobtitle = "Trainer";
            contactobj.firstname = "Arumugam";
    
            ICrmService service = context.CreateCrmService(true);
    
            
            //Refer to the account in the contact
            if (context.OutputParameters.Properties.Contains("id"))
            {
              contactobj.parentcustomerid = new Customer();
              contactobj.parentcustomerid.type = EntityName.account.ToString();
              contactobj.parentcustomerid.Value = new Guid(context.OutputParameters.Properties["id"].ToString());
            }
              
              Guid contactid = service.Create(contactobj);
     appointment appobj = new appointment();
            appobj.regardingobjectid = new Lookup();
            appobj.regardingobjectid.type = EntityName.contact.ToString();
            appobj.regardingobjectid.Value = contactid;
            appobj.subject = "Wedding";
    
            CrmDateTime date = new CrmDateTime();
            date.Value = new DateTime(2010,5,6).ToString();
    
            CrmDateTime date1 = new CrmDateTime();
            date1.Value = new DateTime(2010, 5, 7).ToString();
    
            appobj.scheduledstart = date;
            appobj.scheduledend = date1;
    
             
              TargetCreateAppointment targetappointment = new TargetCreateAppointment();
              targetappointment.Appointment = appobj;
    
              CreateRequest createapp = new CreateRequest();
              createapp.Target = targetappointment;
    
              CreateResponse createdapp = (CreateResponse)service.Execute(createapp);
            }
    

     


    Mahain
    • Proposed as answer by HIMBAPModerator Thursday, July 8, 2010 10:23 AM
    • Marked as answer by Mathan2631 Thursday, July 8, 2010 11:09 AM
    Thursday, July 8, 2010 10:23 AM
    Moderator
  • All of what you require you can do with a standard workflow without writing any code.

    Leon Tribe

    Want to hear me talk about all things CRM? Check out my blog

    http://leontribe.blogspot.com/

    or hear me tweet @leontribe

     


    Want to hear me talk about all things CRM? Check out my blog http://leontribe.blogspot.com/ or hear me tweet @leontribe
    • Proposed as answer by Leon TribeMVP Saturday, July 17, 2010 3:01 PM
    Saturday, July 17, 2010 3:01 PM