Set Default Price list in MS CRM 2011 using Java Script

if you want to set default price list with help of java script, you can use Rest, below is the code to fetch pricelist id based on pricelist name and set it

function SetPriceList() {

var number = 1;
var filter = "/PriceLevelSet?$select=Name,PriceLevelId&$filter=Name eq 'Default'";
RetrieveUnit(filter);
}
function GetODataPath() {

return Xrm.Page.context.getServerUrl() + "/xrmservices/2011/organizationdata.svc";
}
function RetrieveUnit(filter) {

var retrieveRecordsReq = new XMLHttpRequest();
retrieveRecordsReq.open("GET", GetODataPath() + filter, false);
retrieveRecordsReq.setRequestHeader("Accept", "application/json");
retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
var temp= retrieveRecordsReq.send();
if (retrieveRecordsReq.readyState == 4 && retrieveRecordsReq.status == 200) {
var retrievedRecords = this.parent.JSON.parse(retrieveRecordsReq.responseText).d;
var Result = retrievedRecords.results[0];
var lookup = new Object();
var lookupValue = new Array();
lookup.id = Result.PriceLevelId;
lookup.entityType = "pricelevel";
lookup.name = Result.Name;
lookupValue[0] = lookup;
Xrm.Page.getAttribute("pricelevelid").setValue(lookupValue);
}
}

Note: We have created a Default pricelist.

17 thoughts on “Set Default Price list in MS CRM 2011 using Java Script

  1. Kathiravan

    Hi i used the same coding in my online CRM 2011 but it is working the price list is created but i am having an error in the java script. kindly guide me. (I used the above coding without modification) thanks in advance.

    Reply
  2. TheeNerd

    Hi, thanks for the Tutotrial was really helpful
    please can you tell me where you set the default name for the Lookup Field?
    and can you please explain this line
    retrieveRecordsReq.readyState == 4 && retrieveRecordsReq.status == 200

    Reply
    1. mahenderpal Post author

      retrieveRecordsReq.readyState == 4 && retrieveRecordsReq.status == 200 is basically to check fi asyn call is completed or not.
      to set default lookup you need to write code onload of form, you can javascript to set default lookup field you can use below code to set lookup field
      var _lookup = new Object();
      _lookup.id = “<>”;
      _lookup.entityType = “entityName”;
      _lookup.name = “default name”;
      var _lookupValue = new Array();
      _lookupValue[0] = _lookup;
      Xrm.Page.getAttribute(<>).setValue(_lookupValue);

      Reply
      1. TheeNerd

        Thanks for your reply, actually i was able to write a script t set the Default value of a lookup Field when the value of another lookup field change, but actually is not working, the value of the lookup is set but dissapear immediatly after it has been set. Do you have an idea what that’s could be? when after setting the value of the lookup field i put a breakpoint in my code like alert(Ok) instead of alert(“Ok”) i will have an error message and after cliking ok the value of the lookup field will be set.
        Question why is the lookup field value set when i have a breapoint in my code and why isn’t it set when i don’t
        Thanks in advance for your replyThanks for your reply

        Reply
      2. theenerd

        Hi please i have one question
        i change my code a bit but still not working, iVe call the methode Send after ready state and it tells me that retrieveRecordsReq.readyState isn’t equal 4, what does it mean?
        here is how i did:
        retrieveRecordsReq.onreadystatechange = function () {
        if (retrieveRecordsReq.readyState == 4) {
        var retrievedRecords = this.parent.JSON.parse(retrieveRecordsReq.responseText).d;
        var Result = retrievedRecords.results[0];
        var lookup = new Object();
        var lookupValue = new Array();
        SetLookup(“uomid”, Result.DefaultUoMId.Id, Result.DefaultUoMId.Name, “uom”);

        } else {
        alert(“EinFehler ist aufgetreten,Einheit wurde nicht gesetzt”);
        }
        };
        retrieveRecordsReq.send();

        Thanks in advance for your help

        Reply
        1. mahenderpal Post author

          Sorry for late response, if state is not 4 it means, there is something wrong in your code, did you get any error???

          Reply
          1. theenerd

            Hi
            actually there is nothing wrong with the code, i search a bit i noticed that after the Default value of the unit will set, another script will run after my and remove the value again, so i have delayed a bit before i called my function so that the crm script will run first and then my will run after, i ve not tried but i guess will work, keep you update
            Thanks

          2. theenerd

            Hi
            one question how do i filtered a Guid Feld in oData

            i’ve trieda lot of thing but with no succes

            i want to filter a productSet vi a specifi productId i’ve tried these but no working

            1. var filter = “/ProductSet?$select=DefaultUoMId&$filter=Id eq (guid’9B9F869E-531F-E111-BDF8-BC305BD0C8E3′)”;

            2. var filter = “/ProductSet?$select=DefaultUoMId&$filter=Id eq (guid’9B9F869E-531F-E111-BDF8-BC305BD0C8E3′)”;

            3. var filter = “/ProductSet?$select=DefaultUoMId&$filter=Id eq (guid’ ‘9B9F869E-531F-E111-BDF8-BC305BD0C8E3’ ‘)”;

            an idea?
            Thanks

          3. mahenderpal Post author

            you can try like below

            var filter = “/ProductSet(guid'” + Id + “‘)”, true);

          4. theenerd

            Hi Thanks for the message
            i’ve also tried ProductSet(guid'” + Id + “‘) but with no success. I m a bit lost. I can filter based on the Name but with the ID idon’t understand why is not working

  3. theenerd

    Hi,
    i tried to debugged my code and surprisingly for me , the Product was filter but and exception occurs due to this “var Result = retrievedRecords.results[0];” please what do you to select the first result?
    Thanks

    Reply
  4. theenerd

    Hi,
    i am happy to anouced you that i was able to solve the problem and now i can set the default unit for a specific product, the Debbugger helped me alot, Now there are still some things i don’t understand, for instance
    1 retrieveRecordsReq.open(“GET”, oDataPath + filter, true); is not working the Status will be 1 but retrieveRecordsReq.open(“GET”, oDataPath + filter, false ); is working which role false or true have?
    2.In this specific case i din’t do retrievedRecords.Results[0] because when i lokked at the parameter “retrievedRecords” there was not results , in other scripts retrievedRecords.Results[0] works, in this i directly was usind the attribute of retrievedRecords

    Once again thanks a lot for your advices, was really helpful
    keep it up

    Reply
  5. Visy

    Hi
    I have a smimilar Problem,

    We are trying to call WCF webservice. below is the code i am trying to write, I am getting Permission Denied Error.

    Please guide me on how to move forward.

    Note: While calliing a similar webservcie in Plugin we had created end points and passed the Credentials. any idea can we do something like that here in Jscript

    function GetEngineTransmissionDetails() {

    var xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);
    var sUrl = ‘http://msfordcrmqa01.hpuscrmpoc.com/Ford2/SerializedVehicleServices.svc’; // location of the svc
    var sBody = ”
    + ”
    + ” // method Name
    + ‘389100049965899900’ // param 1

    + ”
    + ”
    + ”;
    xmlHttp.open(“POST”, sUrl, false); // end false defines the call as asychronous
    // meaning caller does not wait for the Request handler to finish
    xmlHttp.setRequestHeader(‘SOAPAction’, ‘http://tempuri.org/ISearchResultPopulator/GetEngineTransCodesDetails’);
    xmlHttp.setRequestHeader(‘Content-type’, ‘text/xml’);
    xmlHttp.send(sBody);
    alert(xmlHttp.responseText); // get the response from the WCF
    }

    Reply

Leave a Reply to Visy Cancel reply

Your email address will not be published. Required fields are marked *