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 :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Dynamically enable/disable button using information from related record

(0) ShareShare
ReportReport
Posted on by 230

Hi Guys, 

I am trying to hide the resolve incident button when a field is not populated on the incident form AND the customer contract type is not 7.

it almost works, but I can't figure out how to read related record information.

I used code i already had to display warnings but it does not seem to work for buttons...

Anyone can nudge me in the right direction?

function EnableIncidentButton() {   
    if (Xrm.Page.getAttribute("new_invoicenum").getValue()!=null) { 
		return true; //if Invoice num is not null enable button don't go any furthur
	}  
    var acct =   Xrm.Page.getAttribute('customerid').getValue();   
    if (acct[0].entityType == "account")
   {
	   var retrieveReq = new XMLHttpRequest();    
	   var ODataPath = "https://crm.cartierinfo.com:444/XRMServices/2011/OrganizationData.svc/AccountSet?$select=new_servicecontract&$filter=AccountId eq guid'"   acct[0].id   "'";
	   retrieveReq.open("GET", ODataPath, true);
	   retrieveReq.setRequestHeader("Accept", "application/json");
	   retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
	   retrieveReq.onreadystatechange = function () {
		  retrieveData(this);
	   };     
	   retrieveReq.send();
	}
return false;
}

function retrieveData(retrieveReq) {
    if (retrieveReq.readyState == 4) {
        if (retrieveReq.status == 200) {
            //Parse the result and set the field value on the form
            var retrieved = JSON.parse(retrieveReq.responseText).d;            
			// check if contract type is NOT 7
            var ContType = retrieved.results[0].new_servicecontract.Value;
            if (ContType != 7) {  
	            Xrm.Page.getAttribute("new_specialnotes").setValue('TEST TO SEE IF I CAN DO THIS');			
	            return true; //invoice num is not null						 		
			}
        } 		
    }
	return false;
}


I have the same question (0)
  • Suggested answer
    LeoAlt Profile Picture
    16,331 Moderator on at

    Hi partner,

    1.If you are using D365 V9.X, you could use Xrm.Webapi to get related record more convenient.

    function getValueFromOwnerLookUp(executionContext){
        var formContext=executionContext.getFormContext();
        //get lookup field
        var Lookup_field=formContext.getAttribute("accountid");
        if(Lookup_field!=null){
            //get lookup field record id
            var value=Lookup_field.getValue();
            var id=value[0].id;
            //use record id to retrieve other field value
            Xrm.WebApi.retrieveRecord("systemuser",id,"?$select=fieldname").then(
                function success(result){
                    //get the systemuser field value and set it to field on case
                    var fieldValue=result.fieldname;
                    formContext.getAttribute("fieldname").setValue(fieldValue);
                },
                function (error){
                    console.log(error.message);
                }
            )
    
        }
        
    }

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

    2.About showing warning message, if you are using formContext.ui.setFormNotification(message,level,uniqueId), you should pass "formContext" as parameter in RibbonWork Bench.

    https://www.magnetismsolutions.com/blog/alfwynjordan/2019/10/02/getting-dynamics-365-formcontext-from-ribbon-workbench

    Hope it helps.

    Best Regards,

    Leo

  • Anthony A Profile Picture
    230 on at

    I'm using on premise CRM2015 (V. 7.0) - I don't believe webapi is supported in this version.

  • LeoAlt Profile Picture
    16,331 Moderator on at

    Hi partner,

    Sorry for my misunderstanding, you could refer to this blog about odata rest api.

    www.inogic.com/.../

    Best Regards,

    Leo

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 April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

#2
ManoVerse Profile Picture

ManoVerse 62 Super User 2026 Season 1

#3
11manish Profile Picture

11manish 43

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans