Get Merged Accounts/Contacts/Leads in Dynamics CRM 2011

Here is the SQL query and FetchXML to get merged Accounts.

--Get Merged Accounts
SELECT FA.name,fa.masterid AS 'SubOrdinate Account',FA.masteraccountidname AS 'Master Account' 
FROM FilteredAccount (NOLOCK) FA 
WHERE Fa.merged=1

<fetch version='1.0' output-format='xml-platform' mapping='logical' no-lock='true' distinct='false'>
  <entity name='account'>
    <attribute name='name' alias='SubOrdinate_Account' />
    <attribute name='masterid' alias='Master_Account' />
    <filter>
      <condition attribute='merged' operator='eq' value='1' />
    </filter>
  </entity>
</fetch>

Here is the SQL query and FetchXML to get merged Contacts.

--Get Merged Contacts
SELECT FC.fullname AS 'SubOrdinate Contact',FC.mergedname AS 'Master Contact' 
FROM FilteredContact (NOLOCK) FC 
WHERE FC.merged=1

<fetch version='1.0' output-format='xml-platform' mapping='logical' no-lock='true' distinct='false'>
  <entity name='contact'>
    <attribute name='name' alias='SubOrdinate_Contact' />
    <attribute name='masterid' alias='Master_Contact' />
    <filter>
      <condition attribute='merged' operator='eq' value='1' />
    </filter>
  </entity>
</fetch>

Here is the SQL query and FetchXML to get merged Leads.

--Get Merged Leads
SELECT FL.fullname AS 'SubOrdinate Lead',FL.mergedname AS 'Master Lead' 
FROM FilteredLead (NOLOCK) FL 
WHERE FL.merged=1

<fetch version='1.0' output-format='xml-platform' mapping='logical' no-lock='true' distinct='false'>
  <entity name='lead'>
    <attribute name='name' alias='SubOrdinate_Lead' />
    <attribute name='masterid' alias='Master_Lead' />
    <filter>
      <condition attribute='merged' operator='eq' value='1' />
    </filter>
  </entity>
</fetch>
Advertisements

3 thoughts on “Get Merged Accounts/Contacts/Leads in Dynamics CRM 2011

  1. This is the code i am using but not working for me..

    GetHeaderOnline ( $username, $password, $url );
    // End CRM Online

    // CRM On Premise – IFD
    // $url = “https://org.domain.com/”;
    // //Username format could be domain\\username or username in the form of an email
    // $username = “username”;
    // $password = “password”;

    // $crmAuth = new CrmAuth();
    // $authHeader = $crmAuth->GetHeaderOnPremise($username, $password, $url);
    // End CRM On Premise – IFD

    $userid = WhoAmI ( $authHeader, $url );
    if ($userid == null)
    return;

    $name = CrmGetUserName ( $authHeader, $userid, $url );

    print $name;

    function WhoAmI($authHeader, $url) {
    $xml = ““;
    $xml .= “”;
    $xml .= “”;
    $xml .= ““;
    $xml .= ““;
    $xml .= “WhoAmI“;
    $xml .= “”;
    $xml .= “”;
    $xml .= “
    “;

    $executeSoap = new CrmExecuteSoap ();
    $response = $executeSoap->ExecuteSOAPRequest ( $authHeader, $xml, $url );

    $responsedom = new DomDocument ();
    $responsedom->loadXML ( $response );

    $values = $responsedom->getElementsbyTagName ( “KeyValuePairOfstringanyType” );

    foreach ( $values as $value ) {
    if ($value->firstChild->textContent == “UserId”) {
    return $value->lastChild->textContent;
    }
    }

    return null;
    }
    function CrmGetUserName($authHeader, $id, $url) {
    $xml = ‘

    false

    leadid
    subject
    statuscode

    statuscode
    Equal

    1

    And

    false
    lead

    0
    0

    false

    false

    ‘;

    $executeSoap = new CrmExecuteSoap ();

    $response = $executeSoap->ExecuteSOAPRequest ( $authHeader, $xml, $url );
    echo “

    ";
            print_r($response);
    
        $responsedom = new DomDocument ();
        $responsedom->loadXML ( $response );
    
        $firstname = "";
        $lastname = "";
    
        $values = $responsedom->getElementsbyTagName ( "KeyValuePairOfstringanyType" );
    
    
        foreach ( $values as $value ) {
            if ($value->firstChild->textContent == "firstname") {
                $firstname = $value->lastChild->textContent;
            }
    
            if ($value->firstChild->textContent == "lastname") {
                $lastname = $value->lastChild->textContent;
            }
        }
    
        return $firstname . " " . $lastname;
    
    }
    
    ?>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s