please modify the code
function calculateAgeing() {
//Function to calculate Ageing On change of BPF Stage
try {
if (Xrm.Page.context.client.getClient() == "Web" || Xrm.Page.context.client.getClient() == "Outlook") {
if (Xrm.Page.getAttribute("statecode").getValue() == 0) {
var currentStage = Xrm.Page.data.process.getActiveStage();
var currStageCategory = currentStage.getCategory().getValue();
var dafaultStageCategory = defaultStageOnLoad.getCategory().getValue();
//Stage 1 Ageing
if ((dafaultStageCategory == 0 || dafaultStageCategory == 4) && (currStageCategory != dafaultStageCategory)) {
if (Xrm.Page.getAttribute("ac_stagemodifieddate").getValue() != null && Xrm.Page.getAttribute("ac_stagemodifieddate").getValue() != undefined) {
var s1ModifiedDate = Xrm.Page.getAttribute("ac_stagemodifieddate").getValue();
var s2Modifieddate = new Date();
var spanDiff = ((s2Modifieddate - s1ModifiedDate) / 1000) / 60;
Xrm.Page.getAttribute("ac_stagemodifieddate").setValue(s2Modifieddate);
if (Xrm.Page.getAttribute("ac_s1ageing").getValue() != null && Xrm.Page.getAttribute("ac_s1ageing").getValue() != undefined) {
Xrm.Page.getAttribute("ac_s1ageing").setValue(Xrm.Page.getAttribute("ac_s1ageing").getValue() + spanDiff);
}
else {
Xrm.Page.getAttribute("ac_s1ageing").setValue(spanDiff);
}
if (currStageCategory == 1 || currStageCategory == 5) {
Xrm.Page.getAttribute("ac_opportunitystage").setValue(948140001);
}
else if (currStageCategory == 2 || currStageCategory == 6) {
Xrm.Page.getAttribute("ac_opportunitystage").setValue(948140002);
}
else if (currStageCategory == 3 || currStageCategory == 7) {
Xrm.Page.getAttribute("ac_opportunitystage").setValue(948140003);
///change sold margin on change of opportunity stage to stage 4 and proposal status should be accepted
var proposalStatus = Xrm.Page.getAttribute("ac_proposalstatus").getValue();
if (proposalStatus != null && proposalStatus != undefined && proposalStatus == 948140001) {
var grossMargin = Xrm.Page.getAttribute("ac_grossmarginpercentage").getValue();
if (grossMargin != null && grossMargin != undefined) {
Xrm.Page.getAttribute("ac_soldmargin").setValue(grossMargin);
Xrm.Page.getAttribute("ac_soldmargin").setSubmitMode("always");
}
}
else {
Xrm.Page.getAttribute("ac_soldmargin").setValue(null);
Xrm.Page.getAttribute("ac_soldmargin").setSubmitMode("always");
}
}
Xrm.Page.data.entity.save();
defaultStageOnLoad = Xrm.Page.data.process.getActiveStage();
}
else {
var s1ModifiedDate = Xrm.Page.getAttribute("createdon").getValue();
var s2Modifieddate = new Date();
var spanDiff = ((s2Modifieddate - s1ModifiedDate) / 1000) / 60;
Xrm.Page.getAttribute("ac_stagemodifieddate").setValue(s2Modifieddate);
if (Xrm.Page.getAttribute("ac_s1ageing").getValue() != null && Xrm.Page.getAttribute("ac_s1ageing").getValue() != undefined) {
Xrm.Page.getAttribute("ac_s1ageing").setValue(Xrm.Page.getAttribute("ac_s1ageing").getValue() + spanDiff);
}
else {
Xrm.Page.getAttribute("ac_s1ageing").setValue(spanDiff);
}
if (currStageCategory == 1 || currStageCategory == 5) {
Xrm.Page.getAttribute("ac_opportunitystage").setValue(948140001);
}
else if (currStageCategory == 2 || currStageCategory == 6) {
Xrm.Page.getAttribute("ac_opportunitystage").setValue(948140002);
}
else if (currStageCategory == 3 || currStageCategory == 7) {
Xrm.Page.getAttribute("ac_opportunitystage").setValue(948140003);
///change sold margin on change of opportunity stage to stage 4 and proposal status should be accepted
var proposalStatus = Xrm.Page.getAttribute("ac_proposalstatus").getValue();
if (proposalStatus != null && proposalStatus != undefined && proposalStatus == 948140001) {
var grossMargin = Xrm.Page.getAttribute("ac_grossmarginpercentage").getValue();
if (grossMargin != null && grossMargin != undefined) {
Xrm.Page.getAttribute("ac_soldmargin").setValue(grossMargin);
Xrm.Page.getAttribute("ac_soldmargin").setSubmitMode("always");
}
}
else {
Xrm.Page.getAttribute("ac_soldmargin").setValue(null);
Xrm.Page.getAttribute("ac_soldmargin").setSubmitMode("always");
}
}
Xrm.Page.data.entity.save();
defaultStageOnLoad = Xrm.Page.data.process.getActiveStage();
}
}