locked
CRM 2011, Get all assigned Contacts from Account/Company Form RRS feed

  • Question

  • Hello,

    I need help with CRM 2011 and Jscript.

    In the account/company form is an existing grid which is called Contacts. In this field are some Contacts assigned (with the button "add existing contact").

    Now when I open some Contact there should be a box/grid/iframe with a list of all companies this contact is assigned too. But only when the Checkbox "Ekonomi" is active.

    This list should be linked to the Account/companies (When i click on them CRM should open the form).

    Maybe someone can give me a tip? My plan was first to look for all companies and then to compare the assigned contacts to the opened one with some Jscript. Then the script should list all matching contacts in the contact form.

    But this way is not really performant since the script needs to read all companies first.
    Maybe there is a better way but I don't know.

    This is my (not correct) working code so far:

    function listFinanceContacts()
    {
    	var chkEkonomi = Xrm.Page.getAttribute("testcrm_ekonomi").getValue();
    	
    	if (chkEkonomi === true) {
    		var grid = Xrm.Page.ui.controls.get('accountContactsGrid')._control;
    		
    		alert("Ekonomi is active: " + chkEkonomi);
    	}else {
    		alert("Ekonomi is not active: " + chkEkonomi);
    	}
    };

    It tells me the "accountContacsGrid" is not existing. This is because the field/grid just exists in the "account"(company) form. No idea how I can get the grid elements from an other form and list it in the current.



    • Edited by dr34mup Wednesday, April 8, 2015 1:16 PM
    Wednesday, April 8, 2015 1:12 PM

Answers


  • If this still doesn't work, just add your subgridcontrol to a section on your form and hide the section in stead of your subgrid.

    Xrm.Page.ui.tabs.get("yourtabname").sections.get("your section name").setVisible(true);

    All the other solutions I read to check whether the subgrid has been loaded successfully are unsupported. (Not much time to search a little deeper now).

    Kind Regards


    • Edited by Jochen- Thursday, April 9, 2015 8:06 AM Wrong Code
    • Marked as answer by dr34mup Thursday, April 9, 2015 2:33 PM
    Thursday, April 9, 2015 8:04 AM

All replies

  • I think you are searching a bit too far. I assume your relationship between Account and Contact is 1:N? Add a subgrid to your Contact form with the Related (Records) - Active (view) accounts (Entity). 'Ekonomi' is on your Contact form? if it is true, show the subgrid, if it is false, hide the subgrid:

    var chkEkonomi = Xrm.Page.getAttribute("testcrm_ekonomi").getValue();
    	
    	if (chkEkonomi) {		Xrm.Page.getControl("subgridName").setVisible(true);
    	}else {		Xrm.Page.getControl("subgridName").setVisible(false);
    	}

    Hope it helps,

    Kind Regards

    Wednesday, April 8, 2015 2:01 PM
  • Hello,

    Agree with Jochen, but you need to modify your code like below, because it may be that subgrid is not loaded fully so you may get error

    function HideSubgrid()
    {
    	if(Xrm.Page.getControl('subgridName')!=null)
    		{
    		  //subgrid is loaded
    		  if(Xrm.Page.getAttribute("testcrm_ekonomi")!=null)
    		  {
    			  var chkEkonomi = Xrm.Page.getAttribute("testcrm_ekonomi").getValue();
    			  if(chkEkonomi){
    				Xrm.Page.getControl("subgridName").setVisible(true); }
    			  else {		Xrm.Page.getControl("subgridName").setVisible(false);}
    			}
    		}
    	else
    	{
    	  setTimeout(HideSubgrid, 1000);
        }
    }
    	



    Microsoft Dynamics CRM Training|Our Blog | Follow US | Our Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, April 8, 2015 2:57 PM
    Moderator
  • Thanks for your answer.

    The "assigned contacts" are in a subgrid in the "account" form. The grid (id)"accountContactsGrid" should be 1:N but I am not sure. I looked in the 1:n, n:1 and n:n but didn't find it. But you can assign many contacts to 1 company so only 1:n would make sense.

    I tried your script but it seems I misunderstand something... Now when I open a contact entry I get the message Failure: The preference "setVisible" or a undefined nullpoint can not be called. (Translated)

    But I changed the "subgridName" to the name of the new subgrid "financeContactsGrid". Maybe the code is only working in higher CRM versions?

    Wednesday, April 8, 2015 3:12 PM
  • Not it should work for your environment, can share some screenshots of your environment so that it will me more clear??

    Microsoft Dynamics CRM Training|Our Blog | Follow US | Our Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, April 8, 2015 3:19 PM
    Moderator
  • The script is now working, but I got the same Failure as before. But when I uncheck "Ekonomi" the subgrid is now hidden.

    Here are the screenshots:

    In the contact form I have this "Assigned Contacts" subgrid.

    Now should when I open some contact every company in which the contact is assigned too be listed in the subgrid "financeContactsGrid" (Aktivity).

    I post the preferences in the next post since I am only allowed to attach 2 pictures in one post.

    Wednesday, April 8, 2015 3:49 PM
  • Here are the preferences from "financeContactsGrid".

    And the view "FinanceContacts"

    Wednesday, April 8, 2015 3:53 PM
  • And here the preferences of "accountContactsGrid"

    And the view


    Wednesday, April 8, 2015 3:57 PM
  • Sorry but I did not understand you "The script is now working, but I got the same Failure as before. But when I uncheck "Ekonomi" the subgrid is now hidden." Is script working for you now ?? what issue you are facing now ?

    Microsoft Dynamics CRM Training|Our Blog | Follow US | Our Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, April 8, 2015 4:47 PM
    Moderator
  • I don't understand either. You say: Now should when I open some contact every company in which the contact is assigned too be listed in the subgrid "financeContactsGrid" (Aktivity). And you add a subgrid to the Contact form to show Contacts? Am I wrong?
    Thursday, April 9, 2015 6:48 AM
  • Sorry for the confusion.

    I added the script in CRM and published it. When I open some contact now I get this error message:

    (Its german, since there is no english language installed)

    Failure in the user specified event of this field

    Field:window

    Event:onload

    Failure: The preferences "setVisible" of a undefined or nullreference can not be called.

    -----

    When I click "OK" the script hides the subgrid when "Ekonomi" is unchecked.

    Thursday, April 9, 2015 7:51 AM

  • If this still doesn't work, just add your subgridcontrol to a section on your form and hide the section in stead of your subgrid.

    Xrm.Page.ui.tabs.get("yourtabname").sections.get("your section name").setVisible(true);

    All the other solutions I read to check whether the subgrid has been loaded successfully are unsupported. (Not much time to search a little deeper now).

    Kind Regards


    • Edited by Jochen- Thursday, April 9, 2015 8:06 AM Wrong Code
    • Marked as answer by dr34mup Thursday, April 9, 2015 2:33 PM
    Thursday, April 9, 2015 8:04 AM
  • Sorry again I try to explain it better:

    In Account/Company (or whatever it's called in the english version) there is an existing subgrid. This subgrid is in the register card "contacts".

    It is existing and filled over a long time.

    Now when they open a existing contact form. They want to see which company/account the contact is assigned to. But only when "Ekonomi" is checked.

    In the picture you can see the "Ekonomi" column is on Yes(Ja). This subgrid is just a view of the contact form.

    The idea with the hiding of the new subgrid is really nice. But I also need to list all the contacts that are assigned(connected) in the subgrid to the company.

    As example:

    _Company Form: Company1_

    ->Contact "Assigned contacts":

    NAME           |     EKONOMI     | ....

    person1        |         Yes         | ....

    person2        |         No          | ...

    ------------------------------------------------------------------------------

    _Contact Form: person1_

    -> "Ekonomi" active

    subgrid "FinanceContacts"

    NAME        | Email            | Address

    Company1 | ........             | ......

    Company3 | ........             | ......

    Company4 | .........            | ......

    ------------------------------------------------------------------------------

    person1 is assigned in other account forms (like Company3 and 4) too.  All companies/accounts in which the contact is assigned (as contact), should be now listed.

    Hope it is now better to understand.





    • Edited by dr34mup Thursday, April 9, 2015 8:35 AM
    Thursday, April 9, 2015 8:27 AM
  • Please correct me if I am wrong but I think you're messing up now with relationships. What you need is a N:N Relationship between Account and Contact (A Contact can have multiple accounts and an Account can have multiple contacts). What you are doing now is, on your Contact form, you are showing a grid to show Accounts. This grid shows the accounts from the built in relationship contact_primarycontact, 1:N between Contact and Account), so every account you add there, check the primary contact value on the added account, it'll be the contact you added the account to. Now you want to show every contact on an account. I suppose you add a grid to the account form with contact related entity? Well the data you are showing here is the data from the relationship contact_customer_accounts. This is a 1:N between Account and Contact. This way you'll never have the correct data. I am 95% sure  this is the problem your data isn't shown well.  To solve this issue create a N:N relationship between accounts and contacts, add a grid to both forms and keep the script to hide/show the grid on the contact form.

    Kind Regards

    Thursday, April 9, 2015 9:40 AM
  • This is great, thanks for your help. The code is working without error.

    The only last thing I still need is the list. How can I list all the companies the contact is assigned to?

    Is it a good start to catch all the companies. Look in the subgrid "assigned contacts" for the name of the contact.
    And filter only lines/rows out where his name is shown and the "Ekonomi" is on yes?

    How should I get the info from the other form? Is there a XRM function or did I have to fetch all company forms and look for his name and Ekonomi value?

    The problem is, I tried it over the view -> add row. But it looks like you can not add a subgrid to it. I looked for it but I only saw fields.

    But there must be a file where you can see the linked contacts to the account. Or something like that?

    Thursday, April 9, 2015 10:05 AM
  • I am getting lost on this one :p. Let me say what I think you want to achieve and how I would fix it. You want a subgrid on your contact form to add accounts. The subgrid cannot be visible when Ekonimi = False. You want a subgrid on your account form that shows related contacts that have ekonomi = true. But you only want to add contacts to this account that ekonomi = true? Am I Right? If you say yes, I'll post you a possible solution
    Thursday, April 9, 2015 10:21 AM
  • Lost in (my) translation. :)

    I already have a subgrid in the company form. In this subgrid are many contacts assigned.

    Some of them are "Ekonimi"s and some of them are not.

    Then on the other side I have the contact form. In this form should be a list to which companies the contact is assigned.
    But only when he is "Ekonomi"(=true). What we already did with the function.

    • Edited by dr34mup Thursday, April 9, 2015 11:38 AM
    Thursday, April 9, 2015 11:18 AM
  • With a custom N:N Relation between Account and Contact it is possible? If Ekonomi attribute on the Contact Form is true, a Contact is Ekonomi on every account the contact is assigned to (in the N:N Relation). If the contact Ekonomi attribute = False, hide the subgrid. Below is an example with contact and ekonomi values

    CONTACT 1 TRUE
    CONTACT 2 FALSE
    CONTACT 3 FALSE
    ACCOUNT 1 CONTACT 1
    CONTACT 2
    CONTACT 3
    ACCOUNT 2 CONTACT 1
    CONTACT 2
    ACCOUNT 3  CONTACT 1
    CONTACT 3
    CONTACT 1 ACCOUNT 1 SHOW GRID
    ACCOUNT 2
    ACCOUNT 3
    CONTACT 2 ACCOUNT 1 HIDE GRID
    ACCOUNT 2
    CONTACT 3 ACCOUNT 1 HIDE GRID
    ACCOUNT 3

     
    Thursday, April 9, 2015 11:38 AM
  • Hi Jochen

    Thanks a lot for your help.

    My problem is know I don't really know how to build this subgrid/lookup field with the n:n relation.
    (Since I am new to this)

    But I think you already answered my question and I should ask this in the normal CRM forum. (not development)

    Thursday, April 9, 2015 2:33 PM