Quintiq file version 2.0
#parent: mbMainMenu/MenuFixKB
Response OnClick () id:Response_mbMainMenu_MenuFixKB_OnClick
{
  #keys: '[11660.0.239015373]'
  Body:
  [*
    //Edit a kb branch without active dataset
    editor := CKBEditor::Create();
    kbidset := editor.GetKBs();
    kbid := select( kbidset, Elements, e, true, e.Name() = ScenarioManager.CompanyKBName() );
    kbfullidset := editor.GetKBStructure( kbid );
    kbfullidsortedset := selectsortedset( kbfullidset, Elements.KBFullID, kbfullid, true, kbfullid.Branch(), - kbfullid.ChangeNr() )
    
    branch := '';
    traverse( kbfullidsortedset, Elements, kbfullid )
    {
      if ( branch <> kbfullid.Branch())
      {
        progress('Validating KB Name:' + kbfullid.Name() + ' branch:', kbfullid.Branch() + ' Change Nr:'+ [String]kbfullid.ChangeNr() );
    
        kbf := KBFullID::Create( kbfullid.Domain(), kbfullid.Name(), kbfullid.Branch(), kbfullid.Version(), kbfullid.ChangeNr() );
        modelkb := editor.GetModelKB(kbf.KBID());
    
        ckbhandle := editor.LoadKB( kbfullid );
        //ckbhandle := editor.LoadNewestKBChange( kbfullid );
        // Apply the KB Structure as defined in the Model to the current KB
        ckbhandle.CKB().Upgrade( modelkb.CKB() );
        if ( ckbhandle.CKB().ValidateAll() )
        {
          editor.LockKB( ckbhandle.CKB().ID());
          editor.SaveKB( ckbhandle, '0' );
          editor.UnlockKB( ckbhandle.CKB().ID() );
        }
        branch := kbfullid.Branch();
      }
    }
  *]
  DefinitionID: 'Responsedef_Menu_OnClick'
  UserRequiresReadWrite: true
}
