Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Get rows in a subgrid and updating field on form

Posted on by Microsoft Employee

Hello everyone!

I'm stuck on this issue . 

I have a case with a subgrid with service activities. Each Service Activitiy is considered a visit on my scenario. Whenever a Service Activity is completed i want to decrease by one a field on my form called Remainining Number of Visits

Here is the code i'm using :

function DecrementNumberOfVisits() {

    var packageGrid = Xrm.Page.getControl("ServiceActivities").getGrid();
	
    var packageRows = packageGrid.getRows();
	
    var rowCount = packageRows.getLength();
    
    var remainingVisits = Xrm.Page.getAttribute("ral_remainingnumberofvisits").getValue();


	

    if (rowCount != 0) {

      for (var i = 0; i < rowCount; i++) {

      var rowEntity = packageRows.get(i).getData().getEntity();
      var listStatus = rowEntity.getAttributes().get("statuscode").getValue();

      if (listStatus == "Closed")
	  {
	  
		remainingVisitsts = remainingVisits - 1;
	  
		
	  }
		Xrm.Page.getAttribute("ral_remainingnumberofvisits").setValue(remainingVisits);
		Xrm.Page.getAttribute("ral_remainingnumberofvisits").setSubmitMode("always");
    }
    }

    
   
  }


Whenever i try to run this code i get this message :

TypeError: Cannot read property 'getValue' of null at DecrementNumberOfVisits 

Can anyone help me why this is happening ? I tried to use another field other than statuscode and still doesn't work. I also tried to use getText and i again get the same message.

*This post is locked for comments

  • Suggested answer
    Ryan Maclean Profile Picture
    Ryan Maclean 3,070 on at
    RE: Get rows in a subgrid and updating field on form

    Could you not use a rollup field to count the number of related service activities?

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get rows in a subgrid and updating field on form

    Solution for those interested is to create a real time workflow and decrease the number of remaining visits on the related entity of Service Activity , no need to use custom code

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get rows in a subgrid and updating field on form

    Do you happen to have an example of code of how to do that? I will have to use a for loop in order to get all entries of service activities.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Get rows in a subgrid and updating field on form

    You can get and Id of a record so based on Id and schema name you can data from CRM using endpoint.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get rows in a subgrid and updating field on form

    I can't believe i missed that.

    I've checked the article , so how can i get the attribute status of the service activities subgrid?

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Get rows in a subgrid and updating field on form

    Hello,

    I believe you have that issue mostly because getAttributes method is not available for GridEntity. Check this article - msdn.microsoft.com/.../dn932126.aspx

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans