How to populate a text box on a CRM form using a plug in

Posted on 2009-04-27
Last Modified: 2012-05-06
I am using the code below to retrieve an e mail address from the contactbase table of the CRM database. I want to populate "pt_email" attribute on the opportunity entity with the e mail that I have retrieved using SQL. Is there a way in a plug in where I can specify to fill a textbox with the value that I have.
if (context.MessageName == "Create")




Key opportunityKey = (Key)entity.Properties["opportunityid"];

opportunityID = opportunityKey.Value;

//get the account id

Lookup accountKey = (Lookup)entity.Properties["accountid"];

accountID = accountKey.Value;

straccountID = accountKey.Value.ToString();


SqlConnection crmconnection = new SqlConnection(CRM_SQL_CONNECTION_STRING);


manualpurchaseid = MANUAL_PURCHASE_GUID;

SqlCommand command = new SqlCommand("SELECT * from Pt_manual_purchaseExtensionBase where pt_accountid = '" + accountID + "' and pt_manualid = '" + manualpurchaseid + "'", crmconnection);


SqlDataReader reader = command.ExecuteReader();

while (reader.Read())


contactid = reader["pt_contactid"].ToString();




SqlCommand contactcommand = new SqlCommand("SELECT emailaddress1 from contactbase where contactid = '" + contactid + "'", crmconnection);


SqlDataReader Emailreader = contactcommand.ExecuteReader();

while (Emailreader.Read())


emailaddress = Emailreader["emailaddress1"].ToString();





/// Over here I am trying to put the emailaddress in the "pt_emaiid" attribute of opportunity. it throws an error saying cannot do it. can you tell me what am i doing wrong and what should i do to make it work.

// StringProperty MPemail = (StringProperty)entity.Properties["pt_emailid"];

//MPemail.Value = emailaddress;

Open in new window

Question by:pratikshahse
    1 Comment
    LVL 11

    Accepted Solution

    StringProperty name = new StringProperty();
    name.Name = "pt_emailid";
    name.Value = "";

     KeyProperty kp = new KeyProperty();
                    kp.Name = "opportunityid";
                    kp.Value = new Key(new Guid("opportunityid"));

    DynamicEntity stringentity = new DynamicEntity();
    stringentity.Name = "opportunity";
    stringentity.Properties.AddRange(kp, name)

     TargetUpdateDynamic targetCreate = new TargetUpdateDynamic();
                    targetCreate.Entity = "opportunity";

       UpdateRequest create = new UpdateRequest ();

                    // Set the properties of the request object.
                    create.Target = targetCreate;

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    This is a walkthrough guide I wrote whilst upgrading my on-premise MS Dynamics CRM 3.0 deployment to 4.0. This covers the actual installation of the product to a working level for my system, I ran into a lot of issues that the steps below fixed so h…
    Automatically creating a Trello card using data from a Microsoft Dynamics CRM record turned out to be an easy project that yielded great results.  Here's how I did this for an internal team at General Code.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now