Hello,
This is a follow on question to:
http://social.msdn.microsoft.com/Forums/en-US/4dfa157c-0310-44f5-873d-787857f155d1/help-adding-a-field-to-the-address-table-and-populating-it?forum=crmdevelopment
So I have created a plug-in to copy the extra address fields we have created
for our internal biz requirements. I register the plug-in, register the step, and register a PostImage. Specifically the Step is on the Create message and the primary entity is contact.
The image is a PostImage and all attributes for now.
So what I want to do is update the contact address fields with the values coming from the Opportunity. I am currently doing this and it works but now I am going back to refactor it and could use some help making it more professional.
if (ExecContext.MessageName == "Create")
{
Entity postEntityImg = ExecContext.PostEntityImages["PostAddyImage"];
So far so good. BTW addy1_city etc. are local string vars.
if (ExecContext.MessageName == "Create")
{
Entity postEntityImg = ExecContext.PostEntityImages["PostAddyImage"];
#region Check for atty and populate if exists
#region Addy1
if (postEntityImg.Attributes.Contains("address1_city"))
{
addy1_city = (string)postEntityImg.Attributes["address1_city"];
}
.....etc it keeps going through each field.
Once I have all my local variables populated I then do this bit of ugly stuff to update the entity.
//query to get address recorxds
var res = from c in SvcCtxt.CreateQuery("customeraddress")
where c["parentid"].Equals(AddyContactId)
select c;
foreach (var c in res)
{
Entity e = (Entity)c;
//we need to id which addy record we are updating and assign proper values
if (addy1_city.ToString() == c["city"].ToString() && addy1_pobox.ToString() == c["postofficebox"].ToString() && addy1_stateprov.ToString() == c["stateorprovince"].ToString() && addy1_Line1 == c["line1"].ToString() && addy1_postcode == c["postalcode"].ToString())
{
e["xyz_stateprovincenoniso"] = bmgi_stateprovinceid;
e["xyz_countryregionnoniso"] = bmgi_countryregionid;
}
if (addy2_city == c["city"].ToString() && addy2_pobox.ToString() == c["postofficebox"].ToString() && addy2_stateprov == c["stateorprovince"].ToString() && addy2_Line1 == c["line1"].ToString() && addy2_postcode == c["postalcode"].ToString())
{
e["xyz_stateprovincenoniso"] = bmgi_stateprovince2id;
e["xyx_countryregionnoniso"] = bmgi_countryregion2id;
}
//ServiceContext.Attach(e);
ServiceContext.UpdateObject(e);
}
ServiceContext.SaveChanges();
So this really needs to be written in a much cleaner and more effective manner but I'm afraid I don't know enough of the details.
For example is there a better way to access/loop the PostImage attributes?
What if there is only one addy filled out on the opportunity?
How can I discern which addy from the query projection I am working with. I.E. addy one or two?
Finally I am not asking you to do my work simply tell me what subjects / concepts to read up on and I'll take it from there.
Thank You