Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

Setting the regarding field on load with Javascript

Posted on by 2,068

Hi all,

I'm having issues with the set regarding field. I'm using the navigateto javascript method to open a dialogue box for a task activity when updating a case field. The I've console logged the id, Name and entityType and they are all correct but it keeps saying I've got a script error.

My question is: is it possible to update a regarding lookup field? I keep reading that it's not possible?

Below is my code:

function onloadRegarding(executionContext) {
    var formContext = executionContext.getFormContext();

    var caseIdPassed = formContext.data.attributes.get("case_id").getValue().slice(1,-1);
    var caseName = formContext.data.attributes.get("case_name").getValue();


    var caseLookup = new Array ();
    caseLookup[0] = new Object ();
    caseLookup[0].id = caseIdPassed;
    caseLookup[0].name = caseName;
    caseLookup[0].entityType = 'incident';

    formContext.getAttribute("regardingobjectid").setValue(caseLookup);
    
}

I've tried for the field name both regardingobjectid and just regarding but I keep getting a script error in UI which doesn't tell me anything.

Any help would be greatly appreciated.

  • MikeC282 Profile Picture
    MikeC282 2,068 on at
    RE: Setting the regarding field on load with Javascript

    Thanks guys for your suggestions. I also managed to fix it myself. I had a brain fart moment and forgot to pass the executionContext as the first parameter. Thanks for all your suggestions.

  • Verified answer
    TNS Profile Picture
    TNS 1,195 on at
    RE: Setting the regarding field on load with Javascript

    Hi,

    If you want to open a task activity form and set regarding field, from another entity you can use below code:

    function onsave(context)

    {

    var formContext = context.getFormContext();

    //Entity to open

    var entityFormOptions = {};

    entityFormOptions["entityName"] = "task";

     // Form parameters:

     recordId = formContext.data.entity.getId();

     recordEntityName = formContext.data.entity.getEntityName();

     recordName = formContext.getAttribute("fullname").getValue();

     var regardingValue = new Array();

     regardingValue[0] = new Object();

     regardingValue[0].id = recordId;

     regardingValue[0].name = recordName;

     regardingValue[0].entityType = recordEntityName;

     var formParameters = {};

     formParameters["regardingobjectid"] = regardingValue;

     Xrm.Navigation.openForm(entityFormOptions, formParameters).then(

       function (success) {

           console.log(success);

       },

       function (error) {

           console.log(error);

       });

    }

    Regards,

    TNS

  • Verified answer
    LeoAlt Profile Picture
    LeoAlt 16,327 on at
    RE: Setting the regarding field on load with Javascript

    Hi partner,

    1.This code "caseLookup[0 = new Object ();" is not necessary which may cause the problem.

    Please change your code about setting value to regarding as following.

        var caseLookup = new Array ();
        var regardingItem = new Object ();
        regardingItem.id = caseIdPassed;
        regardingItem.name = caseName;
        regardingItem.entityType = 'incident';
        caseLookup[0]=regardingItem;

    2.You could also use web api to update regarding field.

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/xrm-webapi/updaterecord

    pastedimage1590999123920v1.png

    Best Regards,

    Leo

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 288,768 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,985 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans