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)

How to setvalue in onload event

(0) ShareShare
ReportReport
Posted on by 645

I'm getting a record using fetch to client side, and I need to fill the data from the fetch into a field in a new record I create (fill field on client side), but when I use set value I get an error: Cannot read property 'setValue' of null, I don't understand why, the field is Null because the new record is empty, why can't I fill it ?

function OnLoad() {
                formType = Xrm.Page.ui.getFormType();      
      		 if(formType == formTypes.Create ){
      		  copyCatNewRecord();
      		 }
                }
function copyCatNewRecord(){
	 	var xml;
	            xml ="<fetch version='1.0' output-format='xml-platform' 
                    mapping='logical' distinct='false' count='1'>"
 		    xml += "<entity name='needs'>"
    		    xml += "<attribute name='needsid' />"
    		    xml += "<attribute name='needsname' />"   
    		    xml += "<order attribute='modifiedon' descending='true' />"    		       		         	 
                    xml +=  "</entity></fetch>"
	       
	 
	        var ret = frameworkGlobal.ExecuteFetchRequest(xml);	       	       
	        var currValue =  ret[0].needsid.Value;	
                //   this line returns an error  
	        Xrm.Page.getAttribute("needs").setValue(currValue );
	     	 	 
	 }


*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Hi,

    Not sure what Framework you are using, but there are a few things that I see in your code that you should check.

    1. Make sure you add a semicolon at the end of every line in xml = ...

    2. Make sure that currValue contains a value before trying to set is as a attribute. If needsid is a Guid make sure you are getting the Guid. Since entity needs is a custom entity, I believe the entity and attribute names that you have in your xml are incorrect.

    3. It seems like you are not using the the correct attribute name in the last line: Xrm.Page.getAttribute("???")...

    4. If your currValue is an Entity Reference, that is not the way to set the value. You will need to use something similar to what I have below. You would call it in the following way:

    setLookupField("new_needsid", "guidOfNeedsEntityRecord", "nameOfNeedsEntityRecord", "entityNameOfNeedsEntity");

    function setLookupField(fieldName, lookupId, lookupName, entityName) {

       var lookupData = new Array();

       var lookupItem = new Object();

       lookupItem.id = lookupId;

       lookupItem.name = lookupName;

       lookupItem.entityType = entityName;

       lookupData[0] = lookupItem;

       Xrm.Page.getAttribute(fieldName).setValue(lookupData);

    }

    Hope this helps.

  • sahara Profile Picture
    645 on at

    Hey Aric

    I get all the data I need, it works fine, the only problem is the specific line of setValue, but I 'll do a recheck

  • gdas Profile Picture
    50,091 Moderator on at

    What is the field type of "needs" ? Why it's does not have any publisher name . Not sure can you please confirm your fieldname is correct?

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

    Hi,

    In addition to Goutam's question which is what is the data type of the needsid field, can you share you modified code, so that we can see what is wrong.

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)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans