Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Add all contacts in Required when create New Appointment.

Posted on by Microsoft Employee

Hello all,

when i create new appointment on account form its all contact add in 

Required party list. how i can solve this any Idea. using javascript .

1 On account 2017_2D00_12_2D00_25_5F00_22_2D00_50_2D00_17.png

2017_2D00_12_2D00_25_5F00_22_2D00_51_2D00_53.png

2017_2D00_12_2D00_25_5F00_22_2D00_52_2D00_46.png

my code is

on new appointment form load 

function newAppointment() {

var formType = Xrm.Page.ui.getFormType();
if (formType == 1) {
var lookupData = Xrm.Page.getAttribute("requiredattendees").getValue();
var accountId = lookupData[0].id;
var accountName = lookupData[0].name;
var accountName = lookupData[0].entity;
var guid = accountId.replace("{", "").replace("}", "");

var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts?$select=contactid,fullname&$filter=_parentcustomerid_value eq "+guid, false);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
debugger;
for (var i = 0; i < results.value.length; i++) {

var contactid = results.value[i]["contactid"];
var fullname = results.value[i]["fullname"];
var lookupData = new Array();
var lookupItem = new Object();

//Set the value of a Lookup field
lookupItem.id = contactid;
lookupItem.name = fullname;
lookupItem.entityType = "contact";
lookupData[0] = lookupItem;
Xrm.Page.getAttribute("requiredattendees").setValue(lookupData);
// Xrm.Page.getAttribute("requiredattendees").setValue(lookupData);
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
////////////////////////////////////////
} else {
alert(formType);
}
}

*This post is locked for comments

  • Verified answer
    Sid07 Profile Picture
    Sid07 285 on at
    RE: Add all contacts in Required when create New Appointment.

    This is because of the loop overwriting the 0th position record in the for loop so u need to start iteration from 1 n not 0. I.e. I=1... N I <= results.length.

    This will surely do.

    Please mark this as answer.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Add all contacts in Required when create New Appointment.

    Hi,

    I have set account outside the loop but account not set, all contacts set.

    the code is :

    var lookupData = new Array();

                       var lookupItem1 = new Object();

                       //Set the value of a Lookup field

                       lookupItem1.id = accountId;

                       lookupItem1.name = accountName;

                       lookupItem1.entityType = accountTypeName;

                       lookupData[0] = lookupItem1;

                       for (var i = 0; i < results.value.length; i++) {

                           var contactid = results.value[i]["contactid"];

                           var fullname = results.value[i]["fullname"];

                           var lookupItem = new Object();

                           //Set the value of a Lookup field

                           lookupItem.id = contactid;

                           lookupItem.name = fullname;

                           lookupItem.entityType = "contact";

                           lookupData[i] = lookupItem;

                       }

                       Xrm.Page.getAttribute("requiredattendees").setValue(lookupData);

  • Suggested answer
    Sid07 Profile Picture
    Sid07 285 on at
    RE: Add all contacts in Required when create New Appointment.

    Set the account first outside the loop. Currently u have set it inside d loop n that is wrong.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Add all contacts in Required when create New Appointment.

    Hi

    I add account first than added contacts but only accounts set not contacts.

    the code is

    var lookupData = new Array();

                       for (var i = 1; i < results.value.length; i++) {

                           var contactid = results.value[i]["contactid"];

                           var fullname = results.value[i]["fullname"];

                           var lookupItem = new Object();

                           //Set the value of a Lookup field

                           lookupItem.id = accountId;

                           lookupItem.name = accountName;

                           lookupItem.entityType = accountTypeName;

                           lookupData[0] = lookupItem;

                           lookupItem.id = contactid;

                           lookupItem.name = fullname;

                           lookupItem.entityType = "contact";

                           lookupData[i] = lookupItem;

                       }

  • Suggested answer
    Sid07 Profile Picture
    Sid07 285 on at
    RE: Add all contacts in Required when create New Appointment.

    Hi,

    Ok u can add that using the lookupData nly first add the account details n then the respective values in the array..

    That will help.

    Mark this as answer if this helped you.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Add all contacts in Required when create New Appointment.

    my code is

    function newAppointment() {

      // debugger;

       var formType = Xrm.Page.ui.getFormType();

       if (formType == 1) {

           var lookupData = Xrm.Page.getAttribute("requiredattendees").getValue();

           var accountId = lookupData[0].id;

           var accountName = lookupData[0].name;

           var accountName = lookupData[0].entity;

           var guid = accountId.replace("{", "").replace("}", "");

           var req = new XMLHttpRequest();

           req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts?$select=contactid,fullname&$filter=_parentcustomerid_value eq "+guid, false);

           req.setRequestHeader("OData-MaxVersion", "4.0");

           req.setRequestHeader("OData-Version", "4.0");

           req.setRequestHeader("Accept", "application/json");

           req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

           req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

           req.onreadystatechange = function () {

               if (this.readyState === 4) {

                   req.onreadystatechange = null;

                   if (this.status === 200) {

                       var results = JSON.parse(this.response);

                       debugger;

                       var lookupData = new Array();

                       for (var i = 0; i < results.value.length; i++) {

                           var contactid = results.value[i]["contactid"];

                           var fullname = results.value[i]["fullname"];

                           var lookupItem = new Object();

                           //Set the value of a Lookup field

                           lookupItem.id = contactid;

                           lookupItem.name = fullname;

                           lookupItem.entityType = "contact";

                           lookupData[i] = lookupItem;

                       }

                       Xrm.Page.getAttribute("requiredattendees").setValue(lookupData);

                   } else {

                       Xrm.Utility.alertDialog(this.statusText);

                   }

               }

           };

           req.send();

           //var lookupData = Xrm.Page.getAttribute("requiredattendees").getValue();

       } else {

           alert(formType);

       }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Add all contacts in Required when create New Appointment.

    Thanks its working now.

    2017_2D00_12_2D00_26_5F00_14_2D00_33_2D00_32.png

    and also i want to add account name first then all contact added.

    Thank you.

  • Suggested answer
    Sid07 Profile Picture
    Sid07 285 on at
    RE: Add all contacts in Required when create New Appointment.

    Please share it current code which u used. I will check n update u.

  • Suggested answer
    Sid07 Profile Picture
    Sid07 285 on at
    RE: Add all contacts in Required when create New Appointment.

    Will try to share the code wait

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Add all contacts in Required when create New Appointment.

    I use this but only one contact add no all contacts added.

    I want to add all contacts.

    2017_2D00_12_2D00_26_5F00_12_2D00_17_2D00_33.png

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans