locked
set date field of new record from current form date attribute using REST RRS feed

  • Question

  • Hi 

    I have a web resource in a form that by clicking on a button inside that form, new record is created using REST. New records fields are filled from the form which contains the the web resource. Every thing is ok but date attributes. 

    Say The form has field 'npDate' and I want to copy the exact value of that to new record field 'np_2_date'. It is being copied but with one day less than npDate. I log the value of 'npDate' to browser and it shows :

    Thu Mar 20 2014 00:00:00 GMT+0430 (Iran Daylight Time)

    but when I open newly created record form, I see that 'np_2_date' is one day less, like below:


    Would you help me please ?

    Thursday, March 20, 2014 9:53 AM

All replies

  • can you share the javascript code where you perform the copy of this field?

    My blog: www.crmanswers.net - Rockstar 365 Profile

    Thursday, March 20, 2014 10:08 AM
  • I had to mention that I use this  library to work easier with CRM REST api

    Yes Here it is:


    function getItemValue(attname)
    {
    
    
      if(window.parent.Xrm.Page.data.entity.attributes.get(attname))
      {
    	return window.parent.Xrm.Page.data.entity.attributes.get(attname).getValue();
      }
      return null;
    
    }
    
    
    $(document).ready(function(){
    
    	var OpportunityID=null;
    	
    	
    	
    	$('#newLegalForm').click(function(e)
    	{
    			e.preventDefault();
    			
    		if(window.parent.Xrm.Page.data.entity.attributes.get("new_opportunuity"))
    		{
    			var lkopp=window.parent.Xrm.Page.data.entity.attributes.get("new_opportunuity").getValue();
    			OpportunityID=lkopp[0].id;
    			
    			
    		}
    		
    		var tenderID = window.parent.Xrm.Page.data.entity.getId();
    		
    		
    		
    		
    		
    		
    		
    		if(OpportunityID)
    		{
    			// make button active
    			
    			var oppname;
    			
    			var tenderName;
    			
    			CrmRestKit.Retrieve( 'Opportunity', OpportunityID.replace('{','').replace('}',''), ['Name'] ).fail(function(){console.log('error accoured');} ).done( function ( data ) {
    				// Assert
    				oppname=data.d.Name;
    			} );
    			
    			CrmRestKit.Retrieve( 'new_opportunitytender', tenderID.replace('{','').replace('}',''), ['new_name'] ).fail(function(){console.log('error accoured');} ).done( function ( data ) {
    				// Assert
    				tenderName=data.d.new_name;
    			} );
    			
    			
    		
    		    var np_2_date=getItemValue("npDate");
    		
    		    consoloe.log(np_2_date);
    			
    			var tenderlegal = {
    			
    			'np_2_date': np_2_date,
    			'new_name': 'Legal Part Review',
    			
    			
    			};
    	 
    	
    	 
    			var newlegalid;
    	  
    				CrmRestKit.Create( 'new_opportunitytenderlegal', tenderlegal, false )
    				.fail( function(){console.log('error accoured');} )
    				.done( function ( data, status, xhr ) { 
    				
    				
    				newlegalid=data.d.new_opportunitytenderlegalId;
    				
    				CrmRestKit.Associate(tenderID,"new_opportunitytender",newlegalid,"new_opportunitytenderlegal",
    				"new_new_opportunitytender_new_opportunitytenderlegal_Tender");
    				
    				CrmRestKit.Associate(OpportunityID,"Opportunity",newlegalid,"new_opportunitytenderlegal",
    				"new_opportunity_new_opportunitytenderlegal_Opportunity");
    
    				} );
    				
    				
    			//associate records
    
    			
    			
    		}else
    		{
    			 $("#newLegalForm").attr('href',"#");
    		}
    		
    	});
    	
    	
    	
    
    });
    	</SCRIPT>

    Thursday, March 20, 2014 10:22 AM
  • This is a Javscript issue. It starts Months from 0 therefore in any calculation we have to add 1 to months in order to get correct value. Please add 1 to months in you code.

    Regards Faisal

    Thursday, March 20, 2014 11:28 AM
  • But as it seems, it added one unit to day, not month....

    If this is the case, how could I add 1 to months ?

    Thursday, March 20, 2014 1:00 PM