简体   繁体   中英

CRM Dynamics Get contacts of a related Account

I am creating phone call activities against accounts in CRM, I need to assign the to property of the phone call to the retrieved accounts related contact, how can I do this, please see my code below

public static void GetRelevantOutboundCallCenter(IOrganizationService service)
    {
        QueryExpression qe = new QueryExpression();
        qe.EntityName = "account";
        qe.ColumnSet = new ColumnSet("new_accountactionedstatus", "name", "telephone1", "primarycontactid");
        qe.Criteria.AddCondition("new_deliverystatus", ConditionOperator.Equal, 279640000);
        qe.Criteria.AddCondition("new_province", ConditionOperator.Equal, 100000018);
        qe.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);


        EntityCollection response = service.RetrieveMultiple(qe);

        var counter = 0;

        foreach (Entity account in response.Entities)
        {
            PhoneCall phone = new PhoneCall();

            ActivityParty from = new ActivityParty();
            phone.OwnerId = from.PartyId = new EntityReference("systemuser", new Guid("6DEFA813-56F9-E411-80CC-00155D0B0C2D"));

            ActivityParty to = new ActivityParty();
            to.PartyId = account.ToEntityReference();

            phone.From = new ActivityParty[] { from };
            phone.DirectionCode = true;
            phone.To = new ActivityParty[] { to };// I need to set the related Contact of the account here
            phone.PhoneNumber = account.Attributes["telephone1"].ToString();
            phone.Subject = "TEST FOR OUTBOUND";
            service.Create(phone);

            counter++;

            if (counter == 438)
                return;
        }
    }

This is where I set the lookup account, I need this to be the contact related to the retrieved account -- phone.To = new ActivityParty[] { to };

In case you want to assign your calls to primary contact of account change line

to.PartyId = account.ToEntityReference();

to line

to.PartyId = account.GetEntityAttribute<EntityReference>("primarycontactid");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM