Important Update: Community URLs redirect issues are partially resolved. Learn More. .

cancel
Showing results for 
Search instead for 
Did you mean: 

Custom code to work on field rather than save

AmitMIshra
Contributor III

Hi Everyone,

 

I have one custom code which compares two date field and if one field is greater than the other we have to click on save or save and Apply and then it gives the popup

 

Rather than this i wanted my custom code to check on the date field itself and give popup instead of applying for save or Save and Apply and it should not allow to save the record.

 

Below is the custom code:- 

so when i click on varend date with id as 21321 it should give me popup if 21321 is greater than 21320.

Let me know what can i change in my code to make it work.

 

<script type="text/javascript">

var startDate = 21320, startDateValue;

var endDate = 21321, endDateValue;

var messageBoxText = "Please select a valid EFOD date. EFOD date cannot be greater than Identified date";

var messageBoxTitle = 'Warning';

var daysToCompare = -1;

var oldApplyClick, oldSaveClick

 

Sys.Application.add_load(function() {

// Hijack Save/Apply Buttons

$('#master_btnSave').removeAttr('onclick').click(function() { DateCheck('save'); return false; });

$('#master_btnApply').removeAttr('onclick').click(function() { DateCheck('apply'); return false; });

});

 

function DateCheck(type) {

//Get Date Values

startDateValue = new Date(String(ArcherTech.UI.GenericContent.GetInstance().getFieldValue(startDate, false)));

endDateValue = new Date(String(ArcherTech.UI.GenericContent.GetInstance().getFieldValue(endDate, false)));

if(startDateValue && endDateValue) {

if(daydiff(parseDate(startDateValue), parseDate(endDateValue)) <= daysToCompare) {

WarningAlert(messageBoxText,'',messageBoxTitle);

} else {

SaveApply(type)

}

} else {

SaveApply(type)

}

}

 

function SaveApply(action) {

if(action == 'save') {

ShowAnimationAndPostback('master$btnSave');

} else if(action == 'apply') {

ShowAnimationAndPostback('master$btnApply');

}

}

 

function parseDate(str) {

str = str.getMonth()+1 + '/' + str.getDate() + '/' + str.getYear();

var mdy = str.split('/')

return new Date(mdy[2], mdy[0]-1, mdy[1]);

}

 

function daydiff(first, second) {

return (second-first)/(1000*60*60*24)

}

</script>

 

Thanks and Regards
Amit Mishra

8 REPLIES 8

Ilya_Khen
Champion III

You need to stop hijacking Save and Apply buttons, and instead use change handler on the fields.

Use example from David Petty:

Date Validation Custom Object 

Hi llya,

Could you please provide me the line number from where to change and which handlers to use.

as i started chnaging it but now nothing works i have removed hijack save and apply button code.

Could you please help me with the same.

You need to change your whole Sys.Application.add_load() function.

And you need to add event handler to your date fields, so use proper IDs.

 

Surely if you remove hijacking, nothing would work, because function for validation is defined only during SaveApply

AmitMIshra
Contributor III

Hi Everyone,

I built this code but i don't know where the issue is 

MY EFOD Date should not be greater than Identified date but this doesn't seems to work properly, i don't know from where i am getting this error. 

Below screenshot shows my EFOD is smaller than Identified date then also i am getting Popup.

pastedImage_1.png

 

<script type="text/javascript">
var EFOD= 21320;
var IdentifiedDate= 21321;


Sys.Application.add_load(function() {
$('div[id*="f' + EFOD + 'cdp"]').change(function() {
checkDates(EFOD);
});
$('div[id*="f' + IdentifiedDate + 'cdp"]').change(function() {
checkDates(IdentifiedDate);
});

});
function checkDates(idToBlank) {
EFODVal=$('div[id*="f' + EFOD + 'cdp"]' + ' > input:nth-child(1)').val();
EFODDate=convertToDate(EFODVal);
IdentifiedDateVal=$('div[id*="f' + IdentifiedDate + 'cdp"]' + ' > input:nth-child(1)').val();
IdentifiedDateDate=convertToDate(IdentifiedDateVal);

if (EFODDate!=null) {
if (IdentifiedDateDate!=null) {
if (EFODDate>IdentifiedDateDate) {
alert("The EFOD date cannot be after the Identified date");
nullThisID(idToBlank)
}
}

}
}

function nullThisID(thisID) {
$('div[id*="f' + thisID + 'cdp"]' + ' > input:nth-child(1)').val('')
}

function convertToDate(aDate) {
dataSplit=aDate.split("/");
returnDate=null;
if (dataSplit.length==3) {
returnDate = new Date(dataSplit[2],dataSplit[1],dataSplit[0]);
}
return returnDate;
}

</script>

Well, you have that in the code:

if (IdentifiedDateDate!=null) {
if (EFODDate>IdentifiedDateDate) {
alert("The EFOD date cannot be after the Identified date");

 

So, I recommend to perform debugging using Dev Tools.

Amit,

I had this same problem.

I thought I had it working in my custom object until January of this year (as you're also seeing)

For some reason certain dates in January were evaluating to being older than the date selected in December.

 

I can't explain why, I just tried to find a solution.  So, my solution may look/may be wonky, but it works.

With both of your dates do something like this:

 

var tempDate = new Date(Date.parse(yourfirstDateValue));
var newDate = new Date(tempDate .getUTCFullYear(), tempDate .getUTCMonth(), tempDate .getUTCDate()).toLocaleDateString();

 

var tempDate2 = new Date(Date.parse(yousecondDateValue));
var newDate2 = new Date(tempDate2.getUTCFullYear(), tempDate2.getUTCMonth(), tempDate2.getUTCDate()).toLocaleDateString();

 

 

Then -- if (tempDate>tempDate2) or vice versa should work.

 

Maybe Ilya Khen‌ or David Petty‌ have an explanation and a more elegant solution, but this is what I had to do after multiple different attempts.

The reason why this is failing:

if (EFODDate>IdentifiedDateDate) {‍‍

because in the code provided by the author, the comparison done on Date objects. Different browsers perform differently the conversion of date object to serial or ms, thus I saw cases when on IE such comparison was failing. 

I would recommend to do explicit conversion on Date objects prior to comparison, either the way Jason Solar is performing or function:

getTime()‍‍

In any case, Jason is fully correct!

Hi Jason,

Thanks for the reply.

Can you send me your whole code please.

Thanks and Regards

Amit Mishra