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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Create a task and associate it with a custom entity

(0) ShareShare
ReportReport
Posted on by 50

I am trying to create a set of tasks when a custom entity reaches a certain stage. I want the task to allocated to the owner of the customer entity and also be associated with the customer entity so other users can  track the process of the tasks without knowing who is doing them.

I am retrieving multiple records from another table that store the task I need hence the result.entities[I] (this works)

This is what I have but I get the error message [object Object]

                        var vGoLiveDate = Xrm.Page.getAttribute("new_golivedate").getValue();
                        var date = new Date(vGoLiveDate);
                        var newdate = new Date(date);
                        var daysbeforgolive = result.entities[i].new_daysbeforegolive
                        var vStartDate = newdate.setDate(newdate.getDate() - daysbeforgolive);
                        var today = new Date()                            
                        if (vStartDate < today){
                            vNoTime = "Yes"
                        }
                        var sGUID = Xrm.Page.data.entity.getId();
                        var ID = "/clockworX_OnBoardings(" + sGUID .substring(1,37) + ")";
                        var lookupObj  = Xrm.Page.getAttribute("new_ownerid")
                        if (lookupObj != null) {
                            var lookupObjValue = lookupObj.getValue();
                            if (lookupObjValue != null) {
                                lookupRecordGuid = lookupObjValue[0].id
                                var ownerid = lookupRecordGuid.substring(1,37)
                            }
                        }
                        var sSystemUser = "/Users(" + ownerid + ")";
                        alert(ID);
                        alert(sSystemUser);

                        var entityName = "task";
                        var Data =
                         {
                            "subject": result.entities[i].new_name,
                            "ownerid@odata.bind":  sSystemUser,
                            "actualstart": vStartDate,
                            "category": "On Boarding",
                            "description": result.entities[i].new_notes,
                            "regardingobjectid@odata.bind": ID,
                        }


                        Xrm.WebApi.createRecord(entityName, Data).then(
                        function success(result) {
                            var accountId = result.id;
                            alert(accountid)     
                        },
                        function (error) {
                        var alertStrings = { confirmButtonLabel: "Yes", text: error };
                        var alertOptions = { height: 200, width: 350 };
                        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
                        }
                        );

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hello,

    I will suggest you to debug your code and add a watch windows to see result and what properties you are getting back. You can use debugger keyword in your code and can IE developer tool so simple put debugger keyword save and publish, press F12 in IE and initiate your code.

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Hi Frosty,

    As Mahen suggested, it is better if you debug your code (F12) with Internet Explorer or Chrome.

    The one thing that I quickly noticed is in the following line:

    var sSystemUser = "/Users(" + ownerid + ")";

    The name of the Entity is SystemUser not User

    You should change that line to:

    var sSystemUser = "/SystemUsers(" + ownerid + ")";

    You can also use CRM Rest Builder to help you generate some of the code. Although CRM Rest Builder does not provide results of the new Xrm.WebApi you can use the Create Record to generate part of the JavaScript that you might need.

    Hope this helps.

  • Frosty8467 Profile Picture
    50 on at

    Ok guys thanks for the response managed to figure out a few things and resolve them. I can add the task record if I leave the line "regardingobjectid@odata.bind": ID out this is the link to my custom entity.

    When I leave it in the data variable is this in debugger

    category "On Boarding"

    description ""

    ownerid@odata.bind "/Users(0EE49BE3-6717-E811-A835-000D3AE0EB6E)"

    regardingobjectid@odata.bind "/clockworX_OnBoardings(A6846936-CB37-E811-A832-000D3AE0A7F8)"

    scheduledend Tue Apr 10 2018 00:00:00 GMT+1200 (New Zealand Standard Time)

    subject "Receipt Bank (if using)"

    and the error returned is

    "An undeclared property 'RegardingObjectId' which only has property annotations in the payload but no property value was found in the payload. In OData, only declared navigation properties and declared named streams can be represented as properties without values."

    I need this link to work so what am I doing wrong?

  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at

    To set regarding object id you need to use something like below

    activity["regardingobjectid_contact@odata.bind"] = "/contacts(C41CE33F-D0A0-E611-811E-5065F38C8781)";

    refer my post: http://himbap.com/blog/?p=2557

  • Frosty8467 Profile Picture
    50 on at

    Mahen,

    You my friend are a legend. Thank you so much for your help.

    Frosty

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans