0

Optimize this Code is to large

let s1 := if Status = 1 then 1 else 0 end;
let s2 := if Status = 2 then 2 else 0 end;
let s3 := if Status = 3 then 3 else 0 end;
if s1 then
    let reply := dialog(" 🟢 Audit To History", " This Audit was Completed and will be archived and removed from Events", ["Confirmed", "No Confirmed"]);
    if reply = "Confirmed" then
        let me := this;
        let newR := (create 'History Completed');
        newR.('Events Audited' := me);
        newR.('Audit No' := me.'Audit Code');
        newR.('Project Audited' := me.'Projects_>'.'Project Name');
        newR.(Appointment := me.Appointment);
        newR.text(Status := me.text(Status));
        newR.(Auditor := me.'Auditor_>'.('First Name' + " " + 'Last Name'));
        newR.(Audited := me.'To Audit_>'.('First Name' + " " + 'Last Name'));
        alert(" Record Archived")
    end
else
    if s2 then
        let reply := dialog("  M A J O R - Non Conformities", "This Audit was not compliant. Therefore, major non-conformities will be reviewed within 14 days.", ["Confirmed", "No Confirmed"]);
        if reply = "Confirmed" then
            let today := Today + 14;
            let newRec := duplicate(this);
            openRecord(newRec);
            newRec.('Audit Code' := null);
            newRec.(Status := null);
            newRec.(From := null);
            newRec.(Appointment := null);
            newRec.('Plan within' := 14);
            newRec.('Re Schedule Date' := today);
            let me := this;
            newRec.('Audit Code' := me.'Audit Code' + "_" + 1)
        end
    else
        if s3 then
            let reply := dialog(" 🟠 M I N O R - Non Conformities", "This Audit was not compliant. Therefore, minor non-conformities will be reviewed within 7 days.", ["Confirmed", "No Confirmed"]);
            if reply = "Confirmed" then
                let today := Today + 7;
                let newRec := duplicate(this);
                openRecord(newRec);
                newRec.('Audit Code' := null);
                newRec.(Status := null);
                newRec.(From := null);
                newRec.(Appointment := null);
                newRec.('Plan within' := 7);
                newRec.('Re Schedule Date' := today);
                let me := this;
                newRec.('Audit Code' := me.'Audit Code' + "_" + 1)
            end
        end
    end
end

 

 

Copy

 

Is there a way to optimize this code? S2 and S3 are the same, the difference is that S2 is the next 14 days and S3 is the next 7 days.

 The code works but I would like to optimize it, maybe with mixed case?

19 replies

null
    • Fred
    • 1 mth ago
    • Reported - view

    You can by putting the if statements in the variables instead of at the top.

    Something like:

    let t := this;
    if Status = 1 then
        s1 code
    else
        let reply := if Status = 2 then
            dialog("  M A J O R - Non Conformities", "This Audit was not compliant. Therefore, major non-conformities will be reviewed within 14 days.", ["Confirmed", "No Confirmed"])
        else
            dialog(" 🟠 M I N O R - Non Conformities", "This Audit was not compliant. Therefore, minor non-conformities will be reviewed within 7 days.", ["Confirmed", "No Confirmed"])
        end;
        if reply = "Confirmed" then
                    let today := if Status = 2 then
                            Today + 7
                        else
                            Today + 14;
                    let newRec := duplicate(this);
                    openRecord(newRec);
                    newRec.('Audit Code' := null);
                    newRec.(Status := null);
                    newRec.(From := null);
                    newRec.(Appointment := null);
                    newRec.('Plan within' := if t.Status = 2 then 7 else 14 end);
                    newRec.('Re Schedule Date' := today);
                    let me := this;
                    newRec.('Audit Code' := me.'Audit Code' + "_" + 1)
                end
    end
    
      • Rafael Sanchis
      • Rafael_Sanchis
      • 1 mth ago
      • Reported - view

       Hi Fred.

      No works on MAJOR Non Conformities but yes for MINOR Non Conformities.

      • Fred
      • 1 mth ago
      • Reported - view

      Does today and 'Plan within' get set properly when Status =2?

      • Rafael Sanchis
      • Rafael_Sanchis
      • 1 mth ago
      • Reported - view

       

      Fred when select MINOR  is correct duplicate the record but when select MAJOR no duplicate the Record.

      • Fred
      • 1 mth ago
      • Reported - view

      can you upload a sample DB?

      • Rafael Sanchis
      • Rafael_Sanchis
      • 1 mth ago
      • Reported - view

       

      https://youtu.be/27SBOOIGnT8?feature=shared

      The example is the large code.

      The Table Audio Plan, first select Audit Code, next forecast day (1, 5 xx) next the the click on appointment next select Project, Auditor, To Audit and Domain. When the audit is complete I select the Status

      • Fred
      • 1 mth ago
      • Reported - view

      Are you talking about the Trigger after update for Status? The DB has your old code.

      • Rafael Sanchis
      • Rafael_Sanchis
      • 1 mth ago
      • Reported - view

       Yes Fred, in the Other Options button are your code Sorry The idea is you see how work.

    • Fred
    • 1 mth ago
    • Reported - view
     said:
    No works on MAJOR Non Conformities but yes for MINOR Non Conformities.

    I forgot an end. I also updated the code to use a switch since you have more than 3 Status choices. The else would run even if you selected anything else but 1. Now it will only run if you select 1, 2 or 3.

    let t := this;
    switch true do
    case t.Status = 1:
        (
            let reply := dialog(" 🟢 Audit To History", " This Audit was Completed and will be archived and removed from Events", ["Confirmed", "No Confirmed"]);
            if reply = "Confirmed" then
                let me := this;
                let newR := (create 'History Completed');
                newR.('Events Audited' := me);
                newR.('Audit No' := me.'Audit Code');
                newR.('Project Audited' := me.'Projects_>'.'Project Name');
                newR.(Appointment := me.Appointment);
                newR.text(Status := me.text(Status));
                newR.(Auditor := me.'Auditor_>'.('First Name' + " " + 'Last Name'));
                newR.(Audited := me.'To Audit_>'.('First Name' + " " + 'Last Name'));
                alert(" Record Archived")
            end
        )
    case t.Status = 2 or t.Status = 3:
        (
            let title := if t.Status = 2 then
                "  M A J O R - Non Conformities"
            else
                " 🟠 M I N O R - Non Conformities"
            end;
            let message := if t.Status = 2 then
                    "This Audit was not compliant. Therefore, major non-conformities will be reviewed within 14 days."
                else
                    "This Audit was not compliant. Therefore, minor non-conformities will be reviewed within 7 days."
                end;
            let reply := dialog(title, message, ["Confirmed", "No Confirmed"]);
            if reply = "Confirmed" then
                let today := if t.Status = 2 then Today + 7 else Today + 14 end;
                let newRec := duplicate(this);
                newRec.('Audit Code' := null;
                    Status := null;
                    From := null;
                    Appointment := null;
                    'Plan within' := if t.Status = 2 then 7 else 14 end;
                    'Re Schedule Date' := Today;
                    'Audit Code' := t.'Audit Code' + "_" + 1
                    )
            openRecord(newRec);
            end;
    
        )
    end

    It is 6 lines shorter than your original code, but it will be easier to update.

      • Rafael Sanchis
      • Rafael_Sanchis
      • 1 mth ago
      • Reported - view

       

      thanks Fred Tomrrow morning try it now to sleep 😂 Thanks for your time.

      • Rafael Sanchis
      • Rafael_Sanchis
      • 1 mth ago
      • Reported - view

       

      OK all work perfect excelent. 

      It is 6 lines shorter than your original code, but it will be easier to update.

      Yes definitely

      • Rafael Sanchis
      • Rafael_Sanchis
      • 1 mth ago
      • Reported - view

       

      Hi Fred.

      I find a problem. Today 18Mar Minor Non Conformity It should 7 day, and Mayor Non Conformity It should 14 It is calculating the inverse.

      • Fred
      • 1 mth ago
      • Reported - view

      just change the today variable and the Plan within code to what it should be.

      • Rafael Sanchis
      • Rafael_Sanchis
      • 1 mth ago
      • Reported - view

       

      I'm not clear, you mean lines 39 and 40 ? Sorry

      • Fred
      • 1 mth ago
      • Reported - view

      33 and 39. the lines that have the if statements and set the 7 or 14.

      • Rafael Sanchis
      • Rafael_Sanchis
      • 1 mth ago
      • Reported - view

       OK 👍

    • John_Halls
    • 1 mth ago
    • Reported - view

    I came up with this, which tests the dialog first before proceeding to the update

    let t := this;
    let title := [" 🟢 Audit To History","  M A J O R - Non Conformities"," 🟠 M I N O R - Non Conformities"];
    let message := [" This Audit was Completed and will be archived and removed from Events","This Audit was not compliant. Therefore, major non-conformities will be reviewed within 14 days.","This Audit was not compliant. Therefore, minor non-conformities will be reviewed within 7 days."];
    let days := [7,14];
    if dialog(item(title,t.status-1),item(message,t.status-1),["Confirmed", "No Confirmed"]) = "Confirmed" then
       if t.Status = 1:
          (create 'History Completed').(
          'Events Audited' := t;
          'Audit No' := t.'Audit Code';
          'Project Audited' := t.'Projects_>'.'Project Name'
          Appointment := t.Appointment;
          text(Status := t.text(Status);
          Auditor := t.'Auditor_>'.('First Name' + " " + 'Last Name');
          Audited := t.'To Audit_>'.('First Name' + " " + 'Last Name')
          );
          alert(" Record Archived")
       else
          let newRec := duplicate(this);
          newRec.(
          'Audit Code' := null;
          Status := null;
          From := null;
          Appointment := null;
          'Plan within' := item(days,t,Status-2);
          'Re Schedule Date' := today()+item(days,t.Status-2);
          'Audit Code' := t.'Audit Code' + "_" + 1
           );
           openRecord(newRec);
       end
    end
    
    

    Regards John

      • Rafael Sanchis
      • Rafael_Sanchis
      • 1 mth ago
      • Reported - view

       

      Hi John, Thanks, I'll try this too, it's very good to see different options.

      Appreciate

      • Rafael Sanchis
      • Rafael_Sanchis
      • 1 mth ago
      • Reported - view

       

      Hi John today I try your script give me and errormon line 4 Invalid operator: choice - number 

Content aside

  • Status Answered
  • 1 mth agoLast active
  • 19Replies
  • 70Views
  • 3 Following