• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 682
  • Last Modified:

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

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")
{
try
{
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);
crmconnection.Open();
manualpurchaseid = MANUAL_PURCHASE_GUID;
SqlCommand command = new SqlCommand("SELECT * from Pt_manual_purchaseExtensionBase where pt_accountid = '" + accountID + "' and pt_manualid = '" + manualpurchaseid + "'", crmconnection);
command.ExecuteNonQuery();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
contactid = reader["pt_contactid"].ToString();
}
reader.Close();
command.Dispose();
SqlCommand contactcommand = new SqlCommand("SELECT emailaddress1 from contactbase where contactid = '" + contactid + "'", crmconnection);
contactcommand.ExecuteNonQuery();
SqlDataReader Emailreader = contactcommand.ExecuteReader();
while (Emailreader.Read())
{
emailaddress = Emailreader["emailaddress1"].ToString();
break;
}
Emailreader.Close();
contactcommand.Dispose();
 
/// 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

0
pratikshahse
Asked:
pratikshahse
1 Solution
 
rakeshAgarwalCommented:
StringProperty name = new StringProperty();
name.Name = "pt_emailid";
name.Value = "xyz@mtccrm.com";

 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);


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now