web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Create custom phone call entity through scripting

(0) ShareShare
ReportReport
Posted on by

Hi experts, 

I have used the following code to deploy a timer on my case form to record how long customer service reps take on each of their calls. 

Now when I leave/save some case record, I would like for it to store the time as a phone call activity. How can I dynamically create an activity whenever a user accesses the case record?

//Initialize time attributes
var seconds = 0, minutes = 0, hours = 0,t;
function add() {
    seconds++;
    if (seconds >= 60) {
        seconds = 0;
        minutes++;
        if (minutes >= 60) {
            minutes = 0;
            hours++;
        }
    }
    
	//creating time value in hh:mm:ss formate
    var tmp = (hours ? (hours > 9 ? hours : "0" + hours) : "00") + ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);
	Xrm.Page.getAttribute("new_timer").setValue(tmp);

	//recursively calling timer
    timer();
}

//main timer method
function timer() {
    t = setTimeout(add, 1000);
}


Thanks!

Jon

*This post is locked for comments

I have the same question (0)
  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Create custom phone call entity through scripting

    Hi Jon ,

    Hope this helps .

    // JScript source code

    function createPhoneCall()

    {

    try

    {

    var serverURL = Xrm.Page.context.getClientUrl() + "/api/data/v8.1/phonecalls";

    var phonecallId = null;

    //create activity party collection

    var parties = [];

    //create JSON object

    var Phonecall = {};

    //set fields using JSON object

    //Single line of text

    Phonecall["subject"] = "Phone Call";

    //Single line of text & format of phone

    Phonecall["phonenumber"] = "07685124677";

    //Multiple Line of Text

    Phonecall["description"] = "Test xxxxxxxxxxxx xxxxxxxxxx xxxxxxxxxxxx";

    //Date and Time

    Phonecall["scheduledend"] = new Date();

    //Lookup

    Phonecall["regardingobjectid_account@odata.bind"] = "/accounts(475B158C-541C-E511-80D3-3863BB347BA8)"; //Regarding is an account

    //ActivityParty (From)

    var sender = {};

    sender["partyid_systemuser@odata.bind"] = "/systemusers(21102400-932F-E611-80E5-5065F38B2611)";

    sender["participationtypemask"] = 1; //From

    //ActivityParty (To)

    var receiver1 = {};

    receiver1["partyid_account@odata.bind"] = "/accounts(475B158C-541C-E511-80D3-3863BB347BA8)";

    receiver1["participationtypemask"] = 2; //To

    var receiver2 = {};

    receiver2["partyid_contact@odata.bind"] = "/contacts(59A0E5B9-88DF-E311-B8E5-6C3BE5A8B200)";

    receiver2["participationtypemask"] = 2; //To

    var receiver3 = {};

    receiver3["partyid_lead@odata.bind"] = "/leads(ED975EA3-531C-E511-80D8-3863BB3CE2C8)";

    receiver3["participationtypemask"] = 2; //To

    //add this to collection

    parties.push(sender);

    parties.push(receiver1);

    parties.push(receiver2);

    parties.push(receiver3);

    //pass parties[] to phonecall_activity_parties

    Phonecall["phonecall_activity_parties"] = parties;

    //Whole Number

    Phonecall["actualdurationminutes"] = 300; //Duration automatically conerts into Hours

    //Two Options

    Phonecall["directioncode"] = true; //Direction : 0-->False/Incomming, 1-->True/Outgoing

    //convert JSON object to string

    var JSstring = JSON.stringify(Phonecall);

    //ajax request to create phonecall activity; you have to add jquery library from SDK

    $.ajax({

    type: "POST",

    contentType: "application/json; charset=utf-8",

    dataType: "json",

    url: serverURL,

    data: JSstring,

    beforeSend: function (CreatePhoneCallActivityRequest) {

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

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

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

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

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

    },

    //Success Callback Function

    success: function (data, taxtStatus, getPhoneCallActivityResponse) {

    //get Response from Created Record

    phonecallId = getPhoneCallActivityResponse.getResponseHeader("OData-EntityId");

    //get EntityId from ResponseHeader of Created Record

    phonecallId = phonecallId.split(/[()]/);

    phonecallId = phonecallId[1];

    //Display Enttity ID of Created Record

    Xrm.Utility.alertDialog("Entity ID : " + phonecallId);

    },

    //Error Callback Function

    error: function (CreatePhoneCallActivityRequest, textStatus, errorThrown) {

    Xrm.Utility.alertDialog("Something Wrong in Script...:(");

    }

    });

    } catch (e) {

    Xrm.Utility.alertDialog(e.message + "\n" + e.description);

    }

    }

    Thanks

    Goutam

  • Verified answer
    Nithya Gopinath Profile Picture
    17,078 on at
    RE: Create custom phone call entity through scripting

    Hi Jon,

    The below link has the code to create phone call activity using javascript and web api in Dynamics CRM.

    acegcs.wordpress.com/.../creating-activities-using-web-api-in-dynamics-crm-2016

    Hope this helps.

  • Community Member Profile Picture
    on at
    RE: Create custom phone call entity through scripting

    Thank you so much guys. I'll try implementing it using the provided code.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans