Tuesday 12 January 2016

how to fetch contacts from the EntityCollection in CRM plugin.

working with CRM Activity


Description:-


In this example we explain that how to fetch or get all the contacts from the EntityCollection in CRM plugin.

Suppose if you have requirement like when appointment is created in CRM at that time CRM plugin fire and insert that appointment into external SQL server database. So in which scenario that is possibility when appointment created with multiple Required Attendance or multiple Optional Attendances. So how to fetch how many person or contact are available for this particular appointment.

To fetch all contacts from required attendance fields of the appointment you have to use EntityCollection and then you can easily fetch through for each loop like below code.



Plugin Code:-


                    EntityCollection requiredAttendees = new EntityCollection();
                    requiredAttendees = entity.GetAttributeValue<EntityCollection>("requiredattendees"); // requiredattendees is name of fields in crm
                 
                    for (int i = 0; i < requiredAttendees.Entities.Count; i++)
                    {
                        requiredAttendeesName = ((EntityReference)((EntityCollection)entity["requiredattendees"]).Entities[i].Attributes["partyid"]).Name;
                        requiredAttendeesName += requiredAttendeesName;

                    }
                    EntityCollection optionalAttendees = new EntityCollection();
                    optionalAttendees = entity.GetAttributeValue<EntityCollection>("optionalattendees");
                  
                    for (int i = 0; i < optionalAttendees.Entities.Count; i++)
                    {
                        optionalAttendeesName = ((EntityReference)((EntityCollection)entity["optionalattendees"]).Entities[i].Attributes["partyid"]).Name;
                        optionalAttendeesName += optionalAttendeesName;

                    }


In above code we fetch all required attendees and store it in string variable.
This entry was posted in :

0 comments:

Post a Comment