Answered by:
Help with AutoNumber - Stopped working with the change of New Year

Question
-
Hello
We had autonumber working well but with the change of year it suddenly stopped working and does not generate an autonumber but only remains the same number for all orders. I am not too technical but I can see that the code was being generated Onsave through a form. Please have a look at below code and advise what to adjust. Now it only generates following sequence
DL-ORD-13-10001 (Prefix+Year+autnumber) but autnumber part does not work. Below is the code. thanks in advance.
function OnSaveUpdateAutonumber() { var Today = new Date(); var ThisYear = Today.getYear(); var ThisYear = parseFloat(ThisYear); var ThisYear = ThisYear - 2000; var OrderNumberYear = getautonumberyear(); var Autonumber = getautonumber(); var Prefix1 = Autonumber.substring(0, 7); var Prefix3 = Autonumber.substring(10, 15); var Prefix3 = parseFloat(Prefix3); if (ThisYear == OrderNumberYear) { Prefix3 = Prefix3+1; NewAutonumber = Prefix1+ThisYear+"-"+Prefix3; updateAutonumber(NewAutonumber); Xrm.Page.getAttribute('new_autonumber').setValue(NewAutonumber); window.parent.close(); } else { Prefix3 = 10001; NewAutonumber = Prefix1+ThisYear+"-"+Prefix3; updateAutonumber(NewAutonumber); Xrm.Page.getAttribute('new_autonumber').setValue(NewAutonumber); window.parent.close(); } } function getautonumber() { var Autonumber = "autonumber"; var authenticationHeader = GenerateAuthenticationHeader(); var xml = "<?xml version='1.0' encoding='utf-8'?>"+ "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"+ " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+ " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+ authenticationHeader+ "<soap:Body>"+ "<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'"+ " xsi:type='q1:QueryExpression'>"+ "<q1:EntityName>new_orderautonumber</q1:EntityName>"+ "<q1:ColumnSet xsi:type='q1:ColumnSet'>"+ "<q1:Attributes>"+ "<q1:Attribute>new_no</q1:Attribute>"+ "</q1:Attributes>"+ "</q1:ColumnSet>"+ "<q1:Distinct>false</q1:Distinct>"+ "<q1:Criteria>"+ "<q1:FilterOperator>And</q1:FilterOperator>"+ "<q1:Conditions>"+ "<q1:Condition>"+ "<q1:AttributeName>new_name</q1:AttributeName>"+ "<q1:Operator>Like</q1:Operator>"+ "<q1:Values>"+ "<q1:Value xsi:type='xsd:string'>"+Autonumber+"</q1:Value>"+ "</q1:Values>"+ "</q1:Condition>"+ "</q1:Conditions>"+ "</q1:Criteria>"+ "</query>"+ "</RetrieveMultiple>"+ "</soap:Body>"+ "</soap:Envelope>"; var xHReq = new ActiveXObject("Msxml2.XMLHTTP"); xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple"); xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xHReq.setRequestHeader("Content-Length", xml.length); xHReq.send(xml); var resultXml = xHReq.responseXML; var errorCount = resultXml.selectNodes('//error').length; if (errorCount != 0) { var msg = resultXml.selectSingleNode('//description').nodeTypedValue; alert(msg); } else { var results = resultXml.getElementsByTagName('BusinessEntity'); var c = 0; for (i=0;i < results.length;i++) { var OrderAutonumber = results[i].selectSingleNode('./q1:new_no').nodeTypedValue; return OrderAutonumber; } } } function getautonumberyear() { var Autonumber = "autonumber"; var authenticationHeader = GenerateAuthenticationHeader(); var xml = "<?xml version='1.0' encoding='utf-8'?>"+ "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"+ " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+ " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+ authenticationHeader+ "<soap:Body>"+ "<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ "<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'"+ " xsi:type='q1:QueryExpression'>"+ "<q1:EntityName>new_orderautonumber</q1:EntityName>"+ "<q1:ColumnSet xsi:type='q1:ColumnSet'>"+ "<q1:Attributes>"+ "<q1:Attribute>new_prefix2</q1:Attribute>"+ "</q1:Attributes>"+ "</q1:ColumnSet>"+ "<q1:Distinct>false</q1:Distinct>"+ "<q1:Criteria>"+ "<q1:FilterOperator>And</q1:FilterOperator>"+ "<q1:Conditions>"+ "<q1:Condition>"+ "<q1:AttributeName>new_name</q1:AttributeName>"+ "<q1:Operator>Like</q1:Operator>"+ "<q1:Values>"+ "<q1:Value xsi:type='xsd:string'>"+Autonumber+"</q1:Value>"+ "</q1:Values>"+ "</q1:Condition>"+ "</q1:Conditions>"+ "</q1:Criteria>"+ "</query>"+ "</RetrieveMultiple>"+ "</soap:Body>"+ "</soap:Envelope>"; var xHReq = new ActiveXObject("Msxml2.XMLHTTP"); xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple"); xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xHReq.setRequestHeader("Content-Length", xml.length); xHReq.send(xml); var resultXml = xHReq.responseXML; var errorCount = resultXml.selectNodes('//error').length; if (errorCount != 0) { var msg = resultXml.selectSingleNode('//description').nodeTypedValue; alert(msg); } else { var results = resultXml.getElementsByTagName('BusinessEntity'); var c = 0; for (i=0;i < results.length;i++) { var OrderAutonumberYear = results[i].selectSingleNode('./q1:new_prefix2').nodeTypedValue; return OrderAutonumberYear; } } } function updateAutonumber(NewAutonumber) { var OrderAutoNumberID = "{365bd9b5-5172-e111-86fa-005056996981}"; var authenticationHeader = GenerateAuthenticationHeader(); var xml = "<?xml version='1.0' encoding='utf-8'?>"+ "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'"+ " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+ " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+ authenticationHeader+ "<soap:Body>"+ "<Update xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>"+ "<entity xsi:type='new_orderautonumber'>"+ "<new_no>"+NewAutonumber+"</new_no>"+ "<new_orderautonumberid>"+OrderAutoNumberID+"</new_orderautonumberid>"+ "</entity>"+ "</Update>"+ "</soap:Body>"+ "</soap:Envelope>"; var xHReq = new ActiveXObject("Msxml2.XMLHTTP"); xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false); xHReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Update"); xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xHReq.setRequestHeader("Content-Length", xml.length); xHReq.send(xml); var resultXml = xHReq.responseXML; var errorCount = resultXml.selectNodes('//error').length; if (errorCount != 0) { var msg = resultXml.selectSingleNode('//description').nodeTypedValue; alert(msg); } else { return true; } }
Saturday, January 12, 2013 2:40 PM
Answers
-
Hi,
I don't have way to test your test but i think that you have to go database in table new_orderautonumber and update the column new_prefix2 with 13. Like that you in this if:
if (ThisYear == OrderNumberYear)
And the autonumber is incremented.
Hope this helps.
If i answered your question, please mark the response as an answer and also vote as helpful.
Pedro Azevedo Crm Specialist 4.0\2011
- Proposed as answer by Azevedo PedroMVP Saturday, January 12, 2013 3:27 PM
- Marked as answer by holograme2345 Sunday, January 13, 2013 2:21 AM
Saturday, January 12, 2013 3:27 PM
All replies
-
Hi,
I don't have way to test your test but i think that you have to go database in table new_orderautonumber and update the column new_prefix2 with 13. Like that you in this if:
if (ThisYear == OrderNumberYear)
And the autonumber is incremented.
Hope this helps.
If i answered your question, please mark the response as an answer and also vote as helpful.
Pedro Azevedo Crm Specialist 4.0\2011
- Proposed as answer by Azevedo PedroMVP Saturday, January 12, 2013 3:27 PM
- Marked as answer by holograme2345 Sunday, January 13, 2013 2:21 AM
Saturday, January 12, 2013 3:27 PM -
Your logic is flawed.
It does this:
Get the current year.
Get the latest "autonumber" year and value.
If autonumber year is <still the same> take the value, increment by 1, use this on the record and save back to the autonumber record for next time.
If autonumber year is not the same (because it is not the same as current year), start numbering again from 10001 and remember this for next time.
The second part also needs to update the autonumber new_prefix2 field to hold the new year, otherwise it will never match the current year. So in effect what should happen is that the first time the code is used in a new year the numbers start again AND the year is updated, then the second time it is back to business as usual. This is actually quite a neat mechanism, if only it were implemented robustly.
If you update the year to current year and the prefix to 10000 on the 1st Jan every year you won't have a problem, but when you don't, it will break.
Hope this helps.
Adam Vero, Microsoft Certified Trainer | Microsoft Community Contributor 2011
Blog: Getting IT RightSaturday, January 12, 2013 4:42 PM -
so what changes needs to be made in above code pls? can you let me know what needs to be added? so I can try to test it. thanks.Sunday, January 13, 2013 2:02 AM
-
This worked. thanks.
Referring to Azevedo's solution. I haven't tried other suggestion but thank you very much.- Edited by holograme2345 Sunday, January 13, 2013 3:35 AM
Sunday, January 13, 2013 2:21 AM