Copy Records from an entity to another entity through Plugin in Dynamics CRM 2011
Requirement: Suppose you have to copy a record from one entity another, Lets say you have one custom entity called prospect with few attributes like Pipelinestatus, Rating, Firstname, Lastname. And you want to convert this Prospect Record to Lead means a new record in Lead entity copying over the all the attributes value from Prospect to Lead.
Following is the Plugin that can help you to create a copy of record:
Etity lead = new Entity("lead");
ColumnSet cols = new ColumnSet();
cols.AddColumns(new string[] { "new_pipelinestatus", "new_rating", "new_lastname", "new_firstname", "new_businessphone", "new_addressstreet1", "new_city", "new_stateprovince", "new_zippostalcode", "new_updatecounter" });
entity = service.Retrieve("new_prospect", entity.Id, cols);
string status = "";
int irating = 0;
int count = 0;
if (entity.Attributes.Contains("new_pipelinestatus"))
{
//rating Please Select One=0,10%=1,20%=2,30%=3,40%=4,50%=5
irating = Convert.ToInt16(((OptionSetValue)entity.Attributes["new_rating"]).Value.ToString());
status = ((OptionSetValue)entity.Attributes["new_pipelinestatus"]).Value.ToString();
count = Convert.ToInt16(entity.Attributes["new_updatecounter"]);
count = count + 1;
//status P=1,L=2,C=3
if (status == "1")
{
throw new InvalidPluginExecutionException("PipelineStatus Should be L or C to convert to Lead ");
}
if (irating == 0 || irating == 1)
{
throw new InvalidPluginExecutionException("Rating Should be greater than 10 To convert to Lead ");
}
}
string GlobaloptionsetText = GetCRMOptionSetLabel(service, entity.LogicalName, "new_rating", irating);
if (entity.Attributes.Contains("new_lastname"))
lead["lastname"] = entity.Attributes["new_lastname"];
if (entity.Attributes.Contains("new_firstname"))
lead["firstname"] = entity.Attributes["new_firstname"];
if (entity.Attributes.Contains("new_businessphone"))
lead["telephone1"] = entity.Attributes["new_businessphone"];
if (entity.Attributes.Contains("new_addressstreet1"))
lead.Attributes["address1_line1"] = entity["new_addressstreet1"];
if (entity.Attributes.Contains("new_city"))
lead.Attributes["address1_city"] = entity["new_city"];
if (entity.Attributes.Contains("new_stateprovince"))
lead.Attributes["address1_stateorprovince"] = entity["new_stateprovince"];
if (entity.Attributes.Contains("new_zippostalcode"))
lead.Attributes["address1_postalcode"] = entity["new_zippostalcode"];
Guid Lid= service.Create(lead);
No comments:
Post a Comment