Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

CRM 2011 - Plugins Code (need help) loop through child record

$
0
0

Hello Experts/Gurus,

Today is my first official attempt at Plugins and as expected i am struggling and really need you guys to help me through.

Here is my scenario:

I have two parent and child tables 1) Contact 2) Contact Address

  • A contact can specify multiple addresses (for example Contact: Alex has three addresses or three records: A, B, C)
  • On Contact Address table i have a boolean field "Primary Address", and as per business rule only address can be primary
  • Requirement is as soon as i click PrimaryAddress = Yes for Address B, system has to make sure the flag (Primary Address) is set false on all the other records and this one would be yes. So in other words i have to loop through all the Addresses for the Contact. set the flag = NO, and then set the new value.
  • And i think the plugin will be binded to "Contact Address" entity

 

Sorry to tell you but i don't know how to loop through all the contact address records :( here is my code (i have used SDK code as sample to get started):

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            Entity targetEntity = null; 
            if (context.InputParameters.Contains("Target") &&
              context.InputParameters["Target"] is Entity)
            {
                targetEntity = (Entity)context.InputParameters["Target"];
               if (targetEntity.LogicalName != "test_contactaddress")
                { return;  }
            }
            else { return; }

            try
            {
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);

                // field:test_contactaddressesid is basically contact id that will get me list of all the addreses
               Entity member = service.Retrieve("test_contactaddress", ((EntityReference)targetEntity["test_contactaddressesid"]).Id, new ColumnSet(true));
               

WHAT I WANT TO DO NEXT IS LOOP THROUGH THE CONTACT ADDRESS, SET THE FLAG 'PRIMARY ADDRESS' = NO, AND I DON'T KNOW HOW TO DO IT, i have checked the properties for member and can find any LENGTH or anything like that so i can setup the loop (hope i am explaining it correctly)

 

After the above loop part is done (hopefully you guys will point me right direction), i will do the following:

targetEntity.Attributes.Add("new_isprimaryaddress", true);

 

PLEASE GUIDE ME IN RIGHT DIRECTION, is there any resource only where i can get sample codes.

Look forward to hear from you.

Thank in advance.

 



Viewing all articles
Browse latest Browse all 8223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>