locked
retrieve associated campaign for a contact RRS feed

  • Question

  • Hi,

    Is there a way to retrieve associated campaign's of a contact using JavaScript ?

    any suggestions would be helpful. 


    Thanks and Regards. Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Thursday, June 20, 2013 12:06 PM

All replies

  • any idea on this ?

    Thanks and Regards. Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Friday, June 21, 2013 3:22 AM
  • Hi Ravitheja,

    There are several ways to approach this challenge.  I would recommend making an oData call from JavaScript to retrieve the values you desire.  There are a variety of JavaScript syntax you could use to implement the oData call.  We tend to standardize on jQuery as it is well know and makes it easier on cross-browser support in UR12 environments.  The code could look something like below(assumes running from the contact entity to grab the original id, but just as easily could pass it into the function).  I didn't get a chance to load this up into CRM and test it, but it should get you pretty close...

    function GetCampaigns() {
        var nextQuery = null;
    
        if (Xrm.Page.data.entity.getId() != null) {
            var contactid = Xrm.Page.data.entity.getId();
            //odata calls limited to 50 records at a time (make subsequent calls for result sets that can be over 50)
            while (true) {
                var odataSelect;
                if (nextQuery == null)
                    odataSelect = Xrm.Page.context.prependOrgName("/XRMServices/2011/OrganizationData.svc/CampaignItemSet?$select=*&$filter=EntityId eq guid'" + contactid + "'");
                else {
                    odataSelect = Xrm.Page.context.prependOrgName(nextQuery);
                    nextQuery = null;
                }
    
                var jsonData = $.ajax({
                    type: "GET",
                    contentType: "application/json; charset=utf-8",
                    datatype: "json",
                    url: odataSelect,
                    beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
                    async: false
                }).responseText;
    
                var objData = $.parseJSON(jsonData);
    
                if (objData.d != null && objData.d.results != null) {
                    if (objData.d.__next != null) {
                        nextQuery = objData.d.__next;
                    }
    
                    $(objData.d.results).each(function() {
                        alert($(this).CampaignId.Name);
                        //do whatever else you want with the CampaignId
                    });
    
                    if (nextQuery == null)
                        break;
                }
            }
        }
    }

    I hope this helps...

    Rick Gipson
    Credera

    Friday, June 21, 2013 5:55 AM
  • Hi Rick Gipson,

    Thanks for the code snipped. I tried, but no success :( 

    that oData query is not working accordingly. it always gives zero records. 


    Thanks and Regards. Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Friday, June 21, 2013 6:31 AM
  • Have you referenced the required libraries

    Regards Faisal

    Friday, June 21, 2013 7:40 AM
  • hi,

    do u mean, json and jquery libraries ? yeah, i have referenced ! 

    the code is executing successfully. but objData.d.resutls is not giving anythiing if i use the above code.!  


    Thanks and Regards. Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Friday, June 21, 2013 8:36 AM